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#'
Table: Orders PartitionKey: User#123 SortKey: Order#001, Order#002, Profile#001 Query: PartitionKey = 'User#123' AND begins_with(SortKey, 'Order#')
Think about how the sort key condition works with begins_with in DynamoDB queries.
The query filters items by PartitionKey 'User#123' and only includes those where the SortKey starts with 'Order#'. So only 'Order#001' and 'Order#002' are returned.
Which two components make up a composite primary key in DynamoDB?
One key decides the partition, the other orders items within that partition.
A composite primary key consists of a Partition Key (also called Hash Key) and a Sort Key (also called Range Key). Together they uniquely identify an item.
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'?
Remember the correct operator to specify a range in DynamoDB queries.
The BETWEEN operator is used in KeyConditionExpression to specify a range for the sort key. AND is required to combine partition key equality and sort key range.
Consider this DynamoDB query snippet:
KeyConditionExpression: 'PartitionKey = :pk AND SortKey = :sk'
ExpressionAttributeValues: { ':pk': 'User#789' }What error will this cause?
Check if all placeholders in the expression have matching values.
The query uses ':sk' in the expression but does not provide a value for it in ExpressionAttributeValues, causing a runtime error.
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?
Think about how DynamoDB stores items sorted by sort key within a partition.
Querying by PartitionKey and sorting SortKey descending allows efficient retrieval of latest records without scanning the whole table.