0
0
DynamoDBquery~20 mins

Single-table design methodology in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Single-Table Design Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Single-Table Design Basics

In DynamoDB single-table design, what is the main reason to use composite primary keys (partition key + sort key)?

ATo allow automatic scaling of read and write capacity without manual configuration
BTo store multiple unrelated tables in one physical table without any key constraints
CTo uniquely identify each item and enable efficient querying of related items using sort key ranges
DTo enforce foreign key constraints between items in the table
Attempts:
2 left
💡 Hint

Think about how DynamoDB retrieves items efficiently when you query by partition key and sort key.

query_result
intermediate
2:00remaining
Query Result with Composite Keys

Given a DynamoDB table with partition key 'PK' and sort key 'SK', which query will return all orders for customer 'C123' sorted by order date?

Assume items have PK='CUSTOMER#C123' and SK='ORDER#2023-01-01', 'ORDER#2023-02-01', etc.

DynamoDB
Query:
{
  TableName: 'Orders',
  KeyConditionExpression: 'PK = :pk and begins_with(SK, :skprefix)',
  ExpressionAttributeValues: {
    ':pk': 'CUSTOMER#C123',
    ':skprefix': 'ORDER#'
  }
}
AReturns all customers with orders starting with 'ORDER#' prefix
BReturns all orders for customer C123 sorted by date ascending
CReturns all orders for all customers sorted by order date
DReturns no items because begins_with cannot be used with sort key
Attempts:
2 left
💡 Hint

Remember that begins_with works on the sort key to filter items within a partition.

📝 Syntax
advanced
2:00remaining
Identify the Syntax Error in a DynamoDB Update Expression

Which option contains a syntax error in the DynamoDB UpdateExpression?

UpdateExpression examples:

DynamoDB
A: "SET #status = :newStatus, #count = #count + :inc"
B: "SET #status = :newStatus ADD #count :inc"
C: "SET #status = :newStatus, ADD #count :inc"
D: "SET #status = :newStatus, #count = #count + :inc"
ASyntax error: mixing SET and ADD without separating them properly
BValid syntax: sets status and increments count with comma separator
CValid syntax: sets status and adds increment to count using ADD keyword
DValid syntax: same as A, sets status and increments count
Attempts:
2 left
💡 Hint

Check how SET and ADD clauses are combined in UpdateExpression.

optimization
advanced
2:00remaining
Optimizing Access Patterns in Single-Table Design

You want to efficiently query all comments for a blog post and also fetch all posts by an author. Which single-table design pattern helps achieve both with minimal queries?

AUse a composite key with PK='POST#postId' and SK='COMMENT#commentId' for comments, and a GSI with PK='AUTHOR#authorId' and SK='POST#postId' for posts
BCreate separate tables for posts and comments to avoid complex keys
CStore all comments as a list attribute inside the post item to fetch in one read
DUse a single partition key for all items and filter by type in queries
Attempts:
2 left
💡 Hint

Think about how GSIs can support different query patterns in single-table design.

🔧 Debug
expert
2:00remaining
Debugging a Query Returning No Results

A query on a DynamoDB single-table design returns no results when trying to fetch all orders for customer 'C789'. The query is:

{
  TableName: 'Sales',
  KeyConditionExpression: 'PK = :pk and SK BETWEEN :start and :end',
  ExpressionAttributeValues: {
    ':pk': 'CUSTOMER#C789',
    ':start': 'ORDER#2023-01-01',
    ':end': 'ORDER#2023-12-31'
  }
}

What is the most likely reason for no results?

AExpressionAttributeValues keys must start with a letter, so ':start' and ':end' are invalid
BThe partition key 'CUSTOMER#C789' does not exist in the table
CBETWEEN operator cannot be used with sort keys in DynamoDB
DThe SK values are stored as timestamps without the 'ORDER#' prefix, so BETWEEN does not match
Attempts:
2 left
💡 Hint

Check how the sort key values are stored and how BETWEEN compares strings.