Bird
0
0

Given a DynamoDB table with partition key PK and sort key SK, which query will fetch all orders for user with ID 'user123' if orders are stored with PK = 'USER#user123' and SK = 'ORDER#orderId'?

medium📝 query result Q13 of 15
DynamoDB - Access Patterns and Query Optimization
Given a DynamoDB table with partition key PK and sort key SK, which query will fetch all orders for user with ID 'user123' if orders are stored with PK = 'USER#user123' and SK = 'ORDER#orderId'?
AQuery with KeyConditionExpression: PK = 'ORDER#orderId' AND SK = 'USER#user123'
BQuery with KeyConditionExpression: PK = 'USER#user123' AND SK = 'ORDER#orderId'
CScan with FilterExpression: PK = 'USER#user123'
DQuery with KeyConditionExpression: PK = 'USER#user123' AND SK begins_with 'ORDER#'
Step-by-Step Solution
Solution:
  1. Step 1: Understand key structure for orders

    Orders have PK as 'USER#user123' and SK starting with 'ORDER#', so all orders share the same PK but different SKs.
  2. Step 2: Identify query to fetch all orders for user

    Query with KeyConditionExpression: PK = 'USER#user123' AND SK begins_with 'ORDER#' queries PK equal to 'USER#user123' and SK that begins with 'ORDER#', which fetches all orders for that user.
  3. Final Answer:

    Query with KeyConditionExpression: PK = 'USER#user123' AND SK begins_with 'ORDER#' -> Option D
  4. Quick Check:

    PK fixed, SK prefix filter = all user orders [OK]
Quick Trick: Use begins_with on sort key to get related items [OK]
Common Mistakes:
MISTAKES
  • Swapping PK and SK in query
  • Using scan instead of query
  • Filtering SK with exact match instead of prefix

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More DynamoDB Quizzes