A. ExpressionAttributeValues is missing a value for ':cat'.
B. The partition key is not named 'Category', so the query fails to match items.
C. Query cannot filter by partition key, only by sort key.
D. The table name 'Products' is incorrect.
Solution
Step 1: Check the partition key name used in Query
Query requires the exact partition key name in KeyConditionExpression. If 'Category' is not the partition key, no items match.
Step 2: Verify ExpressionAttributeValues and table name
ExpressionAttributeValues has ':cat' defined, and table name is assumed correct, so these are not the issue.
Final Answer:
The partition key is not named 'Category', so the query fails to match items. -> Option B
Quick Check:
Partition key name must match = A [OK]
Hint: Always use correct partition key name in Query [OK]
Common Mistakes:
Using attribute names that are not partition keys
Forgetting to define ExpressionAttributeValues
Assuming Query filters all attributes
5. You want to efficiently retrieve all orders for a user placed in 2023 from a DynamoDB table with UserId as partition key and OrderDate as sort key. Which Query approach is best?
hard
A. Use Scan with FilterExpression: 'UserId = :uid AND OrderDate BETWEEN :start AND :end'.
B. Use Query with KeyConditionExpression: 'UserId = :uid' only, then filter results in application code.
C. Use Query with KeyConditionExpression: 'UserId = :uid AND begins_with(OrderDate, :year)' and ExpressionAttributeValues for user and '2023'.
D. Use GetItem for each order separately by specifying UserId and OrderDate.
Solution
Step 1: Identify efficient Query usage with partition and sort keys
Using KeyConditionExpression with partition key and a condition on sort key (begins_with) efficiently filters orders in 2023.
Step 2: Compare with other options
Scan reads entire table (slow), filtering in app wastes resources, GetItem for each order is inefficient for multiple items.
Final Answer:
Use Query with KeyConditionExpression: 'UserId = :uid AND begins_with(OrderDate, :year)' and ExpressionAttributeValues for user and '2023'. -> Option C
Quick Check:
Query with partition and sort key prefix = B [OK]
Hint: Use Query with partition key and begins_with on sort key [OK]