0
0
DynamoDBquery~20 mins

Composite primary key in DynamoDB - Practice Problems & Coding Challenges

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

Given a DynamoDB table with a composite primary key consisting of PartitionKey and SortKey, what will be the output of the following query?

Query: Get all items where PartitionKey = 'User#123' and SortKey begins with 'Order#'
DynamoDB
Table: Orders
PartitionKey: User#123
SortKey: Order#001, Order#002, Profile#001

Query: PartitionKey = 'User#123' AND begins_with(SortKey, 'Order#')
AReturns all items with PartitionKey 'User#123' including 'Profile#001'
BReturns items with SortKey 'Order#001' and 'Order#002' only
CReturns no items because begins_with is not valid in DynamoDB
DReturns items with SortKey 'Profile#001' only
Attempts:
2 left
💡 Hint

Think about how the sort key condition works with begins_with in DynamoDB queries.

🧠 Conceptual
intermediate
1:30remaining
Understanding Composite Primary Key Components

Which two components make up a composite primary key in DynamoDB?

APartition Key and Sort Key
BPartition Key and Global Secondary Index
CHash Key and Range Key
DPrimary Key and Secondary Key
Attempts:
2 left
💡 Hint

One key decides the partition, the other orders items within that partition.

📝 Syntax
advanced
2:00remaining
Correct Syntax for Querying Composite Primary Key

Which of the following DynamoDB query expressions correctly uses the composite primary key to get all items with PartitionKey 'User#456' and SortKey between 'Order#100' and 'Order#200'?

AKeyConditionExpression: 'PartitionKey = :pk AND SortKey IN (:start, :end)'
BKeyConditionExpression: 'PartitionKey = :pk OR SortKey BETWEEN :start AND :end'
CKeyConditionExpression: 'PartitionKey = :pk AND SortKey BETWEEN :start AND :end'
DKeyConditionExpression: 'PartitionKey = :pk AND SortKey > :start AND SortKey < :end'
Attempts:
2 left
💡 Hint

Remember the correct operator to specify a range in DynamoDB queries.

🔧 Debug
advanced
1:30remaining
Identify the Error in Composite Key Query

Consider this DynamoDB query snippet:

KeyConditionExpression: 'PartitionKey = :pk AND SortKey = :sk'
ExpressionAttributeValues: { ':pk': 'User#789' }

What error will this cause?

ANo error, query runs successfully
BSyntax error: Invalid KeyConditionExpression format
CQuery returns no results because ':sk' is undefined
DRuntime error: Missing ExpressionAttributeValue for ':sk'
Attempts:
2 left
💡 Hint

Check if all placeholders in the expression have matching values.

optimization
expert
2:30remaining
Optimizing Access Patterns with Composite Primary Keys

You have a DynamoDB table with composite primary key (UserID, Timestamp). You want to efficiently retrieve the latest 5 records for a user. Which approach is best?

AQuery with PartitionKey = UserID and SortKey in descending order, limit 5
BScan the entire table and filter by UserID, then sort by Timestamp
CUse a Global Secondary Index on Timestamp only and filter by UserID
DQuery with PartitionKey = UserID and SortKey in ascending order, limit 5
Attempts:
2 left
💡 Hint

Think about how DynamoDB stores items sorted by sort key within a partition.