0
0
DynamoDBquery~20 mins

One-to-many relationship patterns in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
One-to-many DynamoDB Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Querying all orders for a single customer

You have a DynamoDB table named Orders with a partition key CustomerID and a sort key OrderID. Which query will return all orders for customer with ID 123?

AQuery with KeyConditionExpression: OrderID = '123'
BScan with FilterExpression: CustomerID = '123'
CScan with FilterExpression: OrderID = '123'
DQuery with KeyConditionExpression: CustomerID = '123'
Attempts:
2 left
💡 Hint

Remember, queries use the partition key to efficiently find items.

schema
intermediate
2:00remaining
Designing a one-to-many pattern with composite keys

You want to store blog posts and their comments in one DynamoDB table. Each post has many comments. Which key design best supports efficient retrieval of all comments for a post?

APartition key: PostID, Sort key: CommentID
BPartition key: CommentID, Sort key: PostID
CPartition key: PostID, Sort key: PostID
DPartition key: CommentID only
Attempts:
2 left
💡 Hint

Think about grouping comments under the post they belong to.

optimization
advanced
2:00remaining
Optimizing queries for recent comments retrieval

You want to get the 5 most recent comments for a post. Comments have a Timestamp attribute. How should you design the sort key to optimize this query?

ASort key: Timestamp (descending order)
BSort key: Timestamp (ascending order)
CSort key: CommentID
DSort key: PostID
Attempts:
2 left
💡 Hint

DynamoDB sort keys are stored in ascending order by default.

🔧 Debug
advanced
2:00remaining
Why does this query return no results?

You run this DynamoDB query to get comments for post p1:

KeyConditionExpression: "PostID = :pid AND CommentID > :cid"
ExpressionAttributeValues: {":pid": "p1", ":cid": "c0"}

But it returns no items, even though comments exist. What is the likely cause?

AExpressionAttributeValues syntax is wrong
BPostID value is incorrect
CCommentID is not the sort key, so condition on it fails
DKeyConditionExpression cannot use > operator
Attempts:
2 left
💡 Hint

KeyConditionExpression conditions apply only to partition and sort keys.

🧠 Conceptual
expert
3:00remaining
Modeling many comments per post with single table design

You want to store posts and their comments in one DynamoDB table using a single table design. Which pattern correctly models the one-to-many relationship?

AUse partition key as PostID, sort key as 'POST#' for posts and 'COMMENT#CommentID' for comments
BUse partition key as CommentID, sort key as PostID
CUse partition key as PostID only for posts and separate table for comments
DUse partition key as CommentID, sort key as 'POST#PostID'
Attempts:
2 left
💡 Hint

Single table design uses composite sort keys to distinguish item types.