0
0
DynamoDBquery~20 mins

Sort key purpose and usage in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sort Key Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of a Sort Key in DynamoDB
What is the main purpose of a sort key in a DynamoDB table?
ATo allow multiple items with the same partition key and sort them within that partition
BTo uniquely identify each item in the table by itself
CTo encrypt the data stored in the table
DTo define the read capacity units for the table
Attempts:
2 left
💡 Hint
Think about how DynamoDB organizes items when they share the same partition key.
query_result
intermediate
2: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}
)
AReturns all items with UserID '123' and Timestamp greater than 1000
BReturns all items with UserID '123' regardless of Timestamp
CReturns all items with Timestamp greater than 1000 regardless of UserID
DReturns no items because the query syntax is invalid
Attempts:
2 left
💡 Hint
Check how the KeyConditionExpression uses both partition and sort keys.
📝 Syntax
advanced
2: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}
)
AKeySchema includes CustomerID as RANGE and OrderDate as HASH; AttributeDefinitions define both as type String
BKeySchema includes OrderDate as HASH and CustomerID as RANGE; AttributeDefinitions define both as type Number
CKeySchema includes only CustomerID as HASH; AttributeDefinitions define OrderDate as type String
DKeySchema includes CustomerID as HASH and OrderDate as RANGE; AttributeDefinitions define both as type String
Attempts:
2 left
💡 Hint
Remember HASH is partition key and RANGE is sort key.
optimization
advanced
2: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?
AUse a Query operation with only the partition key CustomerID and filter dates after fetching
BUse a Scan operation filtering by CustomerID and date range
CUse a Query operation with partition key as CustomerID and a sort key condition between the start and end dates
DUse a GetItem operation with CustomerID and date range as keys
Attempts:
2 left
💡 Hint
Consider how Query and Scan differ and how sort key conditions help.
🔧 Debug
expert
3: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?
AResourceNotFoundException because table 'Users' does not exist
BValidationException due to missing ExpressionAttributeValue for ':date'
CNo error; query returns items with UserID 'u123' and any CreatedAt
DSyntaxError due to incorrect KeyConditionExpression format
Attempts:
2 left
💡 Hint
Check if all placeholders in the expression have matching values.