Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a partition key in DynamoDB?
A partition key is the primary key attribute that DynamoDB uses to distribute data across partitions. It uniquely identifies items within a table's partition.
Click to reveal answer
beginner
How do you query items by partition key in DynamoDB?
You use the Query operation specifying the partition key value. This returns all items with that partition key efficiently.
Click to reveal answer
intermediate
Why is Query by partition key faster than Scan in DynamoDB?
Query uses the partition key to directly find items, so it reads only relevant data. Scan reads the entire table, which is slower and more costly.
Click to reveal answer
beginner
Can you query DynamoDB without specifying a partition key?
No, Query requires a partition key value. Without it, you must use Scan, which reads the whole table.
Click to reveal answer
intermediate
What is the syntax to query a DynamoDB table by partition key using AWS CLI?
What must you always provide when using the Query operation in DynamoDB?
AIndex name only
BSort key value
CPartition key value
DTable name only
✗ Incorrect
Query requires the partition key value to find matching items.
Which operation reads the entire DynamoDB table?
AQuery
BScan
CGetItem
DUpdateItem
✗ Incorrect
Scan reads every item in the table, unlike Query which uses the partition key.
What does the Query operation return when multiple items share the same partition key?
AOnly one item
BAn error
CNo items
DAll items with that partition key
✗ Incorrect
Query returns all items that have the specified partition key.
Which of these is NOT required for a Query by partition key?
AFull table scan
BSort key value (if table has one)
CTable name
DPartition key value
✗ Incorrect
Query does not perform a full table scan; it uses the partition key to find items.
What is the main benefit of querying by partition key?
AFaster and cheaper data retrieval
BRetrieves all table data
CUpdates multiple items
DDeletes items automatically
✗ Incorrect
Querying by partition key is efficient and cost-effective because it targets specific data.
Explain how querying by partition key works in DynamoDB and why it is efficient.
Think about how DynamoDB organizes data and how Query uses that.
You got /4 concepts.
Describe the difference between Query and Scan operations in DynamoDB.
Focus on how much data each operation reads.
You got /4 concepts.
Practice
(1/5)
1. What does querying by partition key in DynamoDB do?
easy
A. Updates all items with the same partition key value
B. Deletes all items with the same partition key value
C. Fetches all items with the same partition key value
D. Creates a new item with the partition key value
Solution
Step 1: Understand partition key role
The partition key uniquely identifies the partition where items are stored.
Step 2: Query by partition key fetches items
Querying by partition key returns all items that share that key value.
Final Answer:
Fetches all items with the same partition key value -> Option C
Quick Check:
Query by partition key = fetch items [OK]
Hint: Partition key query fetches matching items only [OK]
Common Mistakes:
Thinking it deletes or updates items
Confusing partition key with sort key
Assuming it creates new items
2. Which of the following is the correct syntax to query items by partition key 'UserID' with value '123' in DynamoDB?
easy
A. Table.query(KeyConditionExpression: 'UserID = 123')
B. Table.query(KeyConditionExpression: 'UserID = :val', ExpressionAttributeValues: {':val': '123'})
C. Table.query(FilterExpression: 'UserID = 123')
D. Table.query(KeyConditionExpression: 'UserID == 123')
Solution
Step 1: Identify correct query syntax
DynamoDB requires KeyConditionExpression with placeholders and ExpressionAttributeValues.
Step 2: Check each option
Table.query(KeyConditionExpression: 'UserID = :val', ExpressionAttributeValues: {':val': '123'}) uses correct syntax with placeholder ':val' and value mapping. Others misuse operators or omit placeholders.
Final Answer:
Table.query(KeyConditionExpression: 'UserID = :val', ExpressionAttributeValues: {':val': '123'}) -> Option B
Quick Check:
Use placeholders and ExpressionAttributeValues [OK]
Hint: Use placeholders (:val) and ExpressionAttributeValues [OK]
Common Mistakes:
Using '==' instead of '=' in KeyConditionExpression
Not using ExpressionAttributeValues for values
Using FilterExpression instead of KeyConditionExpression
3. Given a DynamoDB table with items: {UserID: '123', Name: 'Alice'}, {UserID: '123', Name: 'Bob'}, {UserID: '456', Name: 'Carol'}, what will the query by partition key 'UserID' = '123' return?
medium
A. [{UserID: '123', Name: 'Alice'}, {UserID: '123', Name: 'Bob'}]
B. [{UserID: '456', Name: 'Carol'}]
C. [{UserID: '123', Name: 'Alice'}]
D. [] (empty list)
Solution
Step 1: Identify items with partition key '123'
Items with UserID '123' are Alice and Bob.
Step 2: Query returns all matching items
Query returns both Alice and Bob items as they share the partition key.
Hint: Query returns all items with matching partition key [OK]
Common Mistakes:
Expecting only one item returned
Confusing partition key with sort key filtering
Assuming query returns items with different partition keys
4. You wrote this query to get items with partition key 'OrderID' = '789': Table.query(KeyConditionExpression: 'OrderID = 789'). It returns an error. What is the problem?
medium
A. Partition key name is case-sensitive and should be 'orderid'
B. Using wrong operator '==' instead of '='
C. Query method does not support KeyConditionExpression
D. Missing ExpressionAttributeValues for the value '789'
Solution
Step 1: Check query syntax requirements
DynamoDB requires placeholders and ExpressionAttributeValues for values in KeyConditionExpression.
Step 2: Identify missing part
The query uses 'OrderID = 789' directly without placeholder or ExpressionAttributeValues, causing error.
Final Answer:
Missing ExpressionAttributeValues for the value '789' -> Option D
Quick Check:
Use placeholders and ExpressionAttributeValues [OK]
Hint: Always use placeholders and ExpressionAttributeValues [OK]
Common Mistakes:
Using raw values instead of placeholders
Assuming case-insensitivity for partition key
Using '==' operator instead of '='
5. You want to query a DynamoDB table for all items with partition key 'Category' = 'Books' and sort key 'Price' less than 20. Which approach correctly uses query by partition key with a condition on sort key?
hard
A. Use KeyConditionExpression: 'Category = :cat AND Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20}
B. Use FilterExpression: 'Category = :cat AND Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20}
C. Use KeyConditionExpression: 'Category = :cat' and FilterExpression: 'Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20}
D. Use FilterExpression: 'Category = :cat' and KeyConditionExpression: 'Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20}
Solution
Step 1: Understand query conditions
KeyConditionExpression must include partition key equality and supports sort key conditions like <. FilterExpression filters results after the query.
Step 2: Analyze options
Use KeyConditionExpression: 'Category = :cat AND Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20} correctly uses KeyConditionExpression: 'Category = :cat AND Price < :price', which efficiently queries the partition with a sort key range condition. Use KeyConditionExpression: 'Category = :cat' and FilterExpression: 'Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20} uses FilterExpression for Price (valid but less efficient). B lacks KeyConditionExpression. D omits partition key from KeyConditionExpression.
Final Answer:
Use KeyConditionExpression: 'Category = :cat AND Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20} -> Option A
Quick Check:
KeyConditionExpression: partition = AND sort < [OK]
Hint: KeyConditionExpression: partition = AND sort condition [OK]
Common Mistakes:
Using FilterExpression for partition key
Using FilterExpression instead of KeyConditionExpression for sort key range
Mixing KeyConditionExpression and FilterExpression incorrectly