0
0
DynamoDBquery~10 mins

Key condition expressions in DynamoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Key condition expressions
Start Query
Specify Partition Key Condition
Optionally Specify Sort Key Condition
Evaluate Conditions Against Partition Items
Return Matching Items
End Query
The query starts by specifying the partition key condition, optionally adds a sort key condition, then evaluates these conditions against items in the partition to find and return matching items.
Execution Sample
DynamoDB
KeyConditionExpression: "UserId = :uid AND CreatedAt > :date"
ExpressionAttributeValues: {":uid": "123", ":date": "2023-01-01"}
This query finds items where UserId equals '123' and CreatedAt is after '2023-01-01'.
Execution Table
StepActionCondition EvaluatedItem Attribute ValuesCondition ResultItems Returned
1Start QueryUserId = :uid AND CreatedAt > :dateUserId=123, CreatedAt=2023-02-01TrueItem included
2Evaluate next itemUserId = :uid AND CreatedAt > :dateUserId=123, CreatedAt=2022-12-31FalseItem excluded
3Evaluate next itemUserId = :uid AND CreatedAt > :dateUserId=123, CreatedAt=2023-01-02TrueItem included
4End QueryNo more itemsReturn 2 items
💡 All items evaluated; only those matching both conditions are returned.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Current ItemNoneUserId=123, CreatedAt=2023-02-01UserId=123, CreatedAt=2022-12-31UserId=123, CreatedAt=2023-01-02None
Items Returned Count01122
Key Moments - 2 Insights
Why does the item with UserId=123 and CreatedAt=2022-12-31 get excluded?
Because although the partition key UserId matches, the sort key condition CreatedAt > :date (2023-01-01) is false, so the whole condition fails (see execution_table row 2).
Can we query without specifying the sort key condition?
Yes, specifying only the partition key condition is valid and returns all items with that partition key (not shown in this trace but implied in concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, how many items matched both conditions?
A1
B3
C2
D4
💡 Hint
Check the 'Items Returned' column in rows 1, 2, 3, and 4.
At which step does the condition evaluate to false due to the sort key?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Condition Result' column and the item attributes in step 2.
If the sort key condition was removed, how would the number of returned items change?
AIt would stay the same
BIt would increase
CIt would decrease
DNo items would be returned
💡 Hint
Removing the sort key condition means all items with UserId=123 match, see concept_flow.
Concept Snapshot
Key Condition Expressions in DynamoDB:
- Must include partition key equality condition.
- Optional sort key conditions use operators (=, <, >, BETWEEN, etc.).
- Query returns items matching all key conditions.
- Used to efficiently find items by primary key.
- ExpressionAttributeValues provide actual values for placeholders.
Full Transcript
This visual trace shows how DynamoDB processes a query with key condition expressions. The query requires a partition key condition and optionally a sort key condition. Items in the partition matching the partition key are checked against the sort key condition. Items matching both conditions are returned. For example, items with UserId equal to '123' and CreatedAt after '2023-01-01' are included. Items failing any condition are excluded. The trace tracks each item evaluated, the condition result, and the count of items returned. Key moments clarify why some items are excluded and that the sort key condition is optional. The quiz tests understanding of how many items match, when conditions fail, and the effect of removing the sort key condition.