Key condition expressions help you find items in a DynamoDB table by specifying which keys to look for. They make searching fast and easy.
Key condition expressions in DynamoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
DynamoDB
partitionKeyName = :partitionKeyVal [AND sortKeyName operator :sortKeyVal]
The partition key condition is required and uses '='.
The sort key condition is optional and can use operators like =, <, <=, >, >=, BETWEEN, or begins_with.
Examples
DynamoDB
UserId = :userId
DynamoDB
UserId = :userId AND CreatedAt >= :startDate
DynamoDB
UserId = :userId AND CreatedAt BETWEEN :startDate AND :endDate
DynamoDB
UserId = :userId AND begins_with(Title, :prefix)
Sample Program
This example shows how to write a key condition expression to get orders for a customer starting from a certain date.
DynamoDB
QueryInput = {
TableName: "Orders",
KeyConditionExpression: "CustomerId = :cid AND OrderDate >= :date",
ExpressionAttributeValues: {
":cid": { S: "C123" },
":date": { S: "2023-01-01" }
}
}
// This query finds all orders for customer 'C123' from January 1, 2023 onwards.Important Notes
Always specify the partition key in your key condition expression.
Sort key conditions help narrow down results but are optional.
Use ExpressionAttributeValues to safely pass values and avoid injection.
Summary
Key condition expressions let you quickly find items by their keys.
Partition key condition is required; sort key condition is optional.
Use operators like =, BETWEEN, and begins_with for flexible queries.
Practice
1. What is the required part of a key condition expression in DynamoDB?
easy
Solution
Step 1: Understand key condition expression components
Key condition expressions must always include the partition key condition to identify the partition to query.Step 2: Identify optional parts
The sort key condition is optional and used to refine the query within the partition.Final Answer:
Partition key condition -> Option BQuick Check:
Partition key condition = required [OK]
Hint: Partition key condition is always required in key condition expressions [OK]
Common Mistakes:
- Thinking sort key condition is mandatory
- Using only sort key condition without partition key
- Confusing key condition with filter expressions
2. Which of the following is the correct syntax for a key condition expression to find items with partition key 'UserID' equal to '123'?
easy
Solution
Step 1: Identify correct key condition syntax
In DynamoDB, key condition expressions use attribute names and operators like '=' with values in quotes for strings.Step 2: Evaluate each option
UserID = '123'uses correct syntax:UserID = '123'.partition_key = 'UserID' AND sort_key = '123'uses wrong attribute names and syntax.UserID == 123uses '==' which is invalid.Key('UserID').eq('123')is not valid DynamoDB syntax.Final Answer:
UserID = '123' -> Option AQuick Check:
Correct syntax uses '=' and quotes for strings [OK]
Hint: Use single '=' and quotes for string values in key condition expressions [OK]
Common Mistakes:
- Using '==' instead of '='
- Confusing attribute names with keywords
- Using function syntax not supported in key conditions
3. Given a DynamoDB table with partition key 'UserID' and sort key 'Timestamp', what will this key condition expression return?
UserID = 'user1' AND Timestamp BETWEEN 100 AND 200medium
Solution
Step 1: Analyze partition key condition
The expression requires UserID to be 'user1', so only items with this partition key are considered.Step 2: Analyze sort key condition with BETWEEN
The sort key Timestamp must be between 100 and 200 inclusive, filtering items within that range.Final Answer:
All items with UserID 'user1' and Timestamp between 100 and 200 inclusive -> Option CQuick Check:
Partition key + BETWEEN on sort key filters correctly [OK]
Hint: BETWEEN filters sort key range within partition key [OK]
Common Mistakes:
- Ignoring partition key condition
- Thinking BETWEEN applies to partition key
- Assuming syntax error with BETWEEN
4. You wrote this key condition expression:
But your query returns an error. What is the likely cause?
begins_with(UserID, :uid) AND Timestamp = :tsBut your query returns an error. What is the likely cause?
medium
Solution
Step 1: Identify key condition expression rules
Partition key condition must use '=' operator; functions like begins_with are not allowed on partition key.Step 2: Check usage of begins_with
begins_with is valid only on sort key, not partition key. If used on partition key, it causes an error.Final Answer:
Using begins_with on the partition key -> Option AQuick Check:
begins_with only valid on sort key [OK]
Hint: begins_with only works on sort key, not partition key [OK]
Common Mistakes:
- Using begins_with on partition key
- Omitting partition key condition
- Misusing AND operator syntax
5. You want to query a DynamoDB table with partition key 'Category' and sort key 'Date'. You need all items where Category is 'Books' and Date starts with '2023-06'. Which key condition expression should you use?
hard
Solution
Step 1: Identify partition key condition
Category must be exactly 'Books', so use equality operator on partition key.Step 2: Use begins_with on sort key
Date starts with '2023-06' means use begins_with function on sort key Date.Step 3: Evaluate options
Category = 'Books' AND begins_with(Date, '2023-06')correctly combines equality on partition key and begins_with on sort key.Category = 'Books' AND Date BETWEEN '2023-06-01' AND '2023-06-30'uses BETWEEN but requires exact date range, which is more complex.Category = 'Books' OR begins_with(Date, '2023-06')uses OR which is invalid in key condition expressions.begins_with(Category, 'Books') AND Date = '2023-06'incorrectly uses begins_with on partition key.Final Answer:
Category = 'Books' AND begins_with(Date, '2023-06') -> Option DQuick Check:
Partition key = value AND begins_with on sort key [OK]
Hint: Use '=' for partition key and begins_with() for sort key prefix [OK]
Common Mistakes:
- Using OR instead of AND
- Using begins_with on partition key
- Using BETWEEN without exact date range
