0
0
DynamoDBquery~20 mins

Composite sort key pattern in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composite Sort Key Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Querying items with composite sort key prefix

Given a DynamoDB table with a composite sort key in the format TYPE#ID, which query will return all items where the sort key starts with ORDER# for a given partition key USER#123?

DynamoDB
PartitionKey = 'USER#123'
SortKey begins with 'ORDER#'
AKeyConditionExpression: 'PartitionKey = :pk AND SortKey = :prefix', ExpressionAttributeValues: { ':pk': 'USER#123', ':prefix': 'ORDER#' }
BFilterExpression: 'begins_with(SortKey, :prefix)', ExpressionAttributeValues: { ':prefix': 'ORDER#' }
CKeyConditionExpression: 'PartitionKey = :pk AND begins_with(SortKey, :prefix)', ExpressionAttributeValues: { ':pk': 'USER#123', ':prefix': 'ORDER#' }
DKeyConditionExpression: 'PartitionKey = :pk AND SortKey > :prefix', ExpressionAttributeValues: { ':pk': 'USER#123', ':prefix': 'ORDER#' }
Attempts:
2 left
💡 Hint

Use begins_with in the KeyConditionExpression to filter sort keys by prefix.

🧠 Conceptual
intermediate
1:30remaining
Understanding composite sort key structure

Why do we use a composite sort key like TYPE#ID in DynamoDB tables?

ATo combine multiple attributes into one key to enable efficient querying and sorting by type and id.
BTo store multiple values in a single attribute to save storage space.
CTo encrypt the sort key for security reasons.
DTo avoid using partition keys altogether.
Attempts:
2 left
💡 Hint

Think about how combining values helps with queries.

📝 Syntax
advanced
2:00remaining
Correct syntax for querying composite sort key with range

Which of the following DynamoDB query snippets correctly retrieves items with partition key USER#456 and sort keys between ORDER#100 and ORDER#200 inclusive?

AFilterExpression: 'SortKey BETWEEN :start AND :end', ExpressionAttributeValues: { ':start': 'ORDER#100', ':end': 'ORDER#200' }
BKeyConditionExpression: 'PartitionKey = :pk AND SortKey BETWEEN :start AND :end', ExpressionAttributeValues: { ':pk': 'USER#456', ':start': 'ORDER#100', ':end': 'ORDER#200' }
CKeyConditionExpression: 'PartitionKey = :pk AND SortKey >= :start AND SortKey <= :end', ExpressionAttributeValues: { ':pk': 'USER#456', ':start': 'ORDER#100', ':end': 'ORDER#200' }
DKeyConditionExpression: 'PartitionKey = :pk AND SortKey > :start AND SortKey < :end', ExpressionAttributeValues: { ':pk': 'USER#456', ':start': 'ORDER#100', ':end': 'ORDER#200' }
Attempts:
2 left
💡 Hint

Use BETWEEN in KeyConditionExpression for inclusive range queries.

🔧 Debug
advanced
2:00remaining
Debugging incorrect query results with composite sort key

A developer queries a DynamoDB table with partition key USER#789 and sort key prefix INVOICE# using this expression:

KeyConditionExpression: 'PartitionKey = :pk AND SortKey = :prefix', ExpressionAttributeValues: { ':pk': 'USER#789', ':prefix': 'INVOICE#' }

Why does this query return no results even though items with sort keys like INVOICE#001 exist?

ABecause the query uses equality on the sort key instead of a prefix match, so it looks for an exact match to 'INVOICE#'.
BBecause the partition key value is incorrect and does not match any items.
CBecause the sort key attribute name is misspelled in the query.
DBecause DynamoDB does not support querying by sort key.
Attempts:
2 left
💡 Hint

Think about how equality works versus prefix matching.

optimization
expert
3:00remaining
Optimizing queries with composite sort keys for multiple types

You have a DynamoDB table where the sort key combines type and id like TYPE#ID. You want to efficiently query all items of types ORDER and INVOICE for partition key USER#999. Which approach is best?

AUse a single query with KeyConditionExpression 'PartitionKey = :pk' and FilterExpression 'begins_with(SortKey, :order) OR begins_with(SortKey, :invoice)'.
BCreate a Global Secondary Index with partition key as type and sort key as id to query by type directly.
CScan the entire table and filter items with sort keys starting with 'ORDER#' or 'INVOICE#'.
DRun two separate queries with KeyConditionExpression using begins_with for 'ORDER#' and 'INVOICE#' respectively, then merge results in application code.
Attempts:
2 left
💡 Hint

Consider query efficiency and DynamoDB limitations on OR conditions in KeyConditionExpression.