0
0
DynamoDBquery~20 mins

Query with sort key conditions in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DynamoDB Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Query with sort key condition using BETWEEN

Given a DynamoDB table Orders with CustomerID as the partition key and OrderDate as the sort key (stored as ISO date strings), which query will return all orders for CustomerID = 'C123' placed between 2023-01-01 and 2023-01-31 inclusive?

AKeyConditionExpression = 'CustomerID = :cid AND OrderDate > :start AND OrderDate < :end', ExpressionAttributeValues = {':cid': 'C123', ':start': '2023-01-01', ':end': '2023-01-31'}
BKeyConditionExpression = 'CustomerID = :cid AND OrderDate >= :start AND OrderDate <= :end', ExpressionAttributeValues = {':cid': 'C123', ':start': '2023-01-01', ':end': '2023-01-31'}
CKeyConditionExpression = 'CustomerID = :cid AND OrderDate BETWEEN :start AND :end', ExpressionAttributeValues = {':cid': 'C123', ':start': '2023-01-01', ':end': '2023-01-31'}
DKeyConditionExpression = 'CustomerID = :cid AND OrderDate = :start', ExpressionAttributeValues = {':cid': 'C123', ':start': '2023-01-01'}
Attempts:
2 left
💡 Hint

Use the BETWEEN operator to include both start and end dates.

query_result
intermediate
2:00remaining
Query with sort key condition using begins_with

In a DynamoDB table Messages with UserID as partition key and MessageID as sort key, which query will return all messages for UserID = 'U456' where MessageID starts with '2023-06-'?

AKeyConditionExpression = 'UserID = :uid AND begins_with(MessageID, :prefix)', ExpressionAttributeValues = {':uid': 'U456', ':prefix': '2023-06-'}
BKeyConditionExpression = 'UserID = :uid AND MessageID = :prefix', ExpressionAttributeValues = {':uid': 'U456', ':prefix': '2023-06-'}
CKeyConditionExpression = 'UserID = :uid AND MessageID BETWEEN :prefix AND :prefixEnd', ExpressionAttributeValues = {':uid': 'U456', ':prefix': '2023-06-', ':prefixEnd': '2023-06-99'}
DKeyConditionExpression = 'UserID = :uid AND contains(MessageID, :prefix)', ExpressionAttributeValues = {':uid': 'U456', ':prefix': '2023-06-'}
Attempts:
2 left
💡 Hint

Use the begins_with function to filter sort keys starting with a prefix.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in sort key condition

Which of the following KeyConditionExpression strings will cause a syntax error when querying a DynamoDB table with partition key PK and sort key SK?

APK = :pk AND SK > :sk
BPK = :pk AND SK BETWEEN :start AND :end
CPK = :pk AND begins_with(SK, :prefix)
DPK = :pk AND SK IN (:sk1, :sk2)
Attempts:
2 left
💡 Hint

Check which operators are allowed in KeyConditionExpression.

optimization
advanced
2:00remaining
Optimize query to reduce read capacity usage

You want to query a DynamoDB table Events with partition key EventType and sort key EventTimestamp. You want to get all events of type 'click' after 2024-01-01T00:00:00Z. Which query condition is most efficient to minimize read capacity units consumed?

AKeyConditionExpression = 'EventType = :et AND EventTimestamp >= :ts', ExpressionAttributeValues = {':et': 'click', ':ts': '2024-01-01T00:00:00Z'}
BFilterExpression = 'EventTimestamp >= :ts', KeyConditionExpression = 'EventType = :et', ExpressionAttributeValues = {':et': 'click', ':ts': '2024-01-01T00:00:00Z'}
CKeyConditionExpression = 'EventType = :et', ExpressionAttributeValues = {':et': 'click'}
DFilterExpression = 'EventType = :et AND EventTimestamp >= :ts', ExpressionAttributeValues = {':et': 'click', ':ts': '2024-01-01T00:00:00Z'}
Attempts:
2 left
💡 Hint

Use key conditions to reduce data scanned, not filters.

🧠 Conceptual
expert
3:00remaining
Understanding limitations of sort key conditions

Which statement about DynamoDB KeyConditionExpression for sort keys is FALSE?

AThe <code>begins_with</code> function only works on string sort keys.
BYou can combine multiple sort key conditions with <code>AND</code> to form complex filters.
CYou can use <code>=</code>, <code>&gt;</code>, <code>&lt;</code>, <code>BETWEEN</code>, and <code>begins_with</code> operators on sort keys.
DSort key conditions must be part of the <code>KeyConditionExpression</code> and cannot be in <code>FilterExpression</code>.
Attempts:
2 left
💡 Hint

Consider how many conditions on sort key are allowed in KeyConditionExpression.