IN statement in dynamodb
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
DynamoDB does support an IN operator, but only in expressions such as filter expressions and condition expressions. It is not a SQL-style general-purpose IN clause, and it cannot be used inside a KeyConditionExpression. That distinction matters because many developers expect IN to narrow the read before it happens, while in DynamoDB it often filters results after the key lookup or scan has already read them.
Where IN Is Supported
The IN operator can be used in filter expressions and condition expressions. For example, a Scan or Query may apply a filter that keeps only matching values:
That works, but it does not mean DynamoDB is doing an efficient keyed lookup on arbitrary values. A filter expression is applied after the items are read.
IN Is Not for Key Conditions
This is the limitation that usually matters most. You cannot write a key condition like:
A Query still requires an exact partition key equality condition, plus an optional sort-key condition using the operators allowed for key conditions.
So if you need items for several partition key values, the usual options are:
- issue multiple
Queryrequests - redesign the access pattern
- use
BatchGetItemif you already know exact keys
That is a very different model from a relational database WHERE key IN (...) query.
Use IN Carefully with Query
You can combine a valid KeyConditionExpression with a filter expression that uses IN:
This is valid, but remember what it means operationally: DynamoDB first reads the items that match the key condition, then applies the filter. Read capacity is consumed before the filter removes non-matching items.
When IN Is the Wrong Tool
If your main access pattern is "get items by one of several values", model the table or an index for that access path instead of relying on Scan plus IN.
For example, if you often need all products by category, a better schema might make category part of a key design or expose it through a GSI. Then you can query efficiently instead of reading a broad set of items and filtering afterward.
IN is fine for refining a reasonably small result set. It is a poor substitute for access-pattern-driven schema design.
Condition Expressions Also Support IN
IN can be useful on write conditions too, for example when you want to allow an update only if a status is one of a small approved set.
That is often a clean way to enforce state-transition rules.
Common Pitfalls
The biggest mistake is assuming IN in DynamoDB behaves like SQL and can be used freely against key attributes in a single efficient query. It cannot.
Another issue is forgetting that filter expressions run after items are read. A query or scan with IN in a filter can still be expensive even if the final returned item count is small.
People also often reach for Scan plus IN before thinking about data modeling. In DynamoDB, access patterns should drive schema and index design. Expression tricks are rarely a substitute for that.
Finally, remember that reserved attribute names may need aliases through ExpressionAttributeNames, even when the logic itself is correct.
Summary
- DynamoDB supports
INin filter expressions and condition expressions. - '
INcannot be used in aKeyConditionExpression.' - Filter expressions with
INdo not reduce read cost before the read happens. - For multi-key lookup patterns, use multiple queries,
BatchGetItem, or better schema design. - Treat
INas an expression tool, not as a replacement for DynamoDB access-pattern modeling.
Related reading
- In Terraform, how do you specify an API Gateway endpoint with a variable in the request path?
- Inconsistent cache values using Zend Cache with AWS ElastiCache across multiple servers
- Incorporate existing AWS resources into a CloudFormation stack
- Increase number of shards in DynamoDB to spin up more lambdas in parallel
- In Tensorflow, how to assign values in Tensor according to the indices?
- In which case do you use the JPA JoinTable annotation?
- In what order should we tune hyperparameters in Neural Networks?
- In what order should you insert a set of known keys into a B-Tree to get minimal height?

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.