dynamodb how to query by sort key only?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In DynamoDB, a Query operation is designed around a partition key. That is the core rule to remember. If you only know the sort key value, you cannot query the base table directly unless you have modeled an index where that attribute becomes the partition key for that index.
Why the Base Table Cannot Query by Sort Key Alone
DynamoDB stores items by partition key so the service can distribute data efficiently. A sort key only has meaning inside a single partition. Because of that layout, the Query API requires an equality condition on the partition key, and only then can it apply conditions to the sort key.
So this is not valid on a table whose primary key is pk plus sk:
The missing piece is pk. Without it, DynamoDB does not know which partition to search.
The Correct Fix: Model an Index for the Access Pattern
If you need to look up items by what is currently the sort key, create a global secondary index, or GSI, where that attribute becomes the index partition key.
For example, suppose the base table uses:
- '
pkas the tenant or user id' - '
skas an order id'
If you also need to fetch by order id alone, add a GSI such as:
- '
gsi1pk = sk' - '
gsi1sk = created_at'
Then query the index instead of the base table.
That is a true query because the index now has a partition key that matches your access pattern.
What If You Cannot Add an Index
The fallback is Scan with a filter expression. That can work functionally, but it is not equivalent to a query. DynamoDB still reads the table or index pages and then filters after the read.
Use this only for small tables, admin tools, or one-off maintenance jobs. It does not scale well for request-path traffic.
Data Modeling Guidance
The real lesson is that DynamoDB schema design starts from access patterns, not from a normalized relational shape. Before you create the table, list the questions the application must answer. Each important lookup path should have a direct primary-key or index design behind it.
Also watch for hot partitions. If the attribute you promote into a GSI partition key has low cardinality, many requests may pile onto the same key. In that case, add write sharding or a more selective key design.
A good DynamoDB model often duplicates key attributes on purpose. That is normal in DynamoDB and usually cheaper than forcing the application into scans.
Common Pitfalls
A common mistake is thinking FilterExpression makes Query work without the partition key. It does not. Filters are applied after DynamoDB has already identified the items to read.
Another mistake is creating a GSI with the same lookup problem moved one level over. If you still need to search the index by its sort key alone, you have not fixed the access pattern.
Developers also sometimes try to solve this with PartiQL. PartiQL changes the syntax, not the storage model. The underlying rule about key-based access still applies.
Summary
- You cannot query a DynamoDB table by sort key alone.
- A
Queryrequires equality on the partition key of the table or selected index. - If you need that lookup, create a
GSIwhere the desired attribute is the index partition key. - '
Scanwith a filter can work, but it is a last resort and not a scalable query strategy.' - Design DynamoDB tables from access patterns first, even if that means duplicating key attributes.
Related reading
- DynamoDb How to retrieve the first item by sort key for each of a given list of partition keys
- DynamoDB how to use index in PartiQL queries?
- DynamoDB if_not_exists on UpdateItem
- DynamoDB increment a key/value
- DynamoDB Is adding an item using list_append atomic?
- DynamoDB Last Evaluated Key Expiration?
- DynamoDB JsonMarshaller cannot Deserialize List of Object
- DynamoDB Limit on query

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.