Challenge - 5 Problems
Sort Key Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Purpose of a Sort Key in DynamoDB
What is the main purpose of a sort key in a DynamoDB table?
Attempts:
2 left
💡 Hint
Think about how DynamoDB organizes items when they share the same partition key.
✗ Incorrect
The sort key allows multiple items to share the same partition key but be uniquely identified and sorted within that partition. It helps organize data and enables range queries.
❓ query_result
intermediate2:00remaining
Query Result Using Sort Key Condition
Given a DynamoDB table with partition key 'UserID' and sort key 'Timestamp', which query will return all items for UserID '123' with Timestamp greater than 1000?
DynamoDB
Query( TableName='UserActivity', KeyConditionExpression='UserID = :uid AND Timestamp > :ts', ExpressionAttributeValues={':uid': '123', ':ts': 1000} )
Attempts:
2 left
💡 Hint
Check how the KeyConditionExpression uses both partition and sort keys.
✗ Incorrect
The query filters items by partition key 'UserID' equal to '123' and sort key 'Timestamp' greater than 1000, returning only matching items.
📝 Syntax
advanced2:30remaining
Correct Syntax for Defining Sort Key in Table Creation
Which option shows the correct syntax to define a sort key named 'OrderDate' when creating a DynamoDB table?
DynamoDB
CreateTable( TableName='Orders', KeySchema=[ {'AttributeName': 'CustomerID', 'KeyType': 'HASH'}, {'AttributeName': 'OrderDate', 'KeyType': 'RANGE'} ], AttributeDefinitions=[ {'AttributeName': 'CustomerID', 'AttributeType': 'S'}, {'AttributeName': 'OrderDate', 'AttributeType': 'S'} ], ProvisionedThroughput={'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5} )
Attempts:
2 left
💡 Hint
Remember HASH is partition key and RANGE is sort key.
✗ Incorrect
The partition key must be defined as 'HASH' and the sort key as 'RANGE' in KeySchema. AttributeDefinitions must match the attribute names and types.
❓ optimization
advanced2:30remaining
Optimizing Queries Using Sort Key
You want to efficiently retrieve all orders for a customer placed within a specific date range. Which approach best uses the sort key to optimize this query?
Attempts:
2 left
💡 Hint
Consider how Query and Scan differ and how sort key conditions help.
✗ Incorrect
Using Query with partition key and sort key range condition efficiently fetches only relevant items without scanning the whole table.
🔧 Debug
expert3:00remaining
Debugging a Query with Sort Key Condition Error
A developer runs this query on a DynamoDB table with partition key 'UserID' and sort key 'CreatedAt':
Query(
TableName='Users',
KeyConditionExpression='UserID = :uid AND CreatedAt = :date',
ExpressionAttributeValues={':uid': 'u123', ':date': '2023-01-01'}
)
What error will this query produce?
Attempts:
2 left
💡 Hint
Check if all placeholders in the expression have matching values.
✗ Incorrect
The query references ':date' in the KeyConditionExpression but does not provide a value for it in ExpressionAttributeValues, causing a validation error.