AWS Certified Developer - Associate (DVA-C03) free question 3 / 10 · Domain 1: Development with AWS Services
A DynamoDB table has the partition key CustomerId and the sort key OrderDate. Which Query key condition expression is valid for retrieving a customer's orders in a date range?
- OrderDate BETWEEN :start AND :end, leaving CustomerId out of the condition
- CustomerId > :c AND OrderDate BETWEEN :start AND :end
- CustomerId = :c AND OrderDate BETWEEN :start AND :end
- Status = :s AND OrderDate > :start, with Status as a non-key attribute
Show the answer and explanation
Answer: C
Why this is correct
A key condition expression selects the items that Query reads. It must contain the partition key as an equality condition, and it may add one condition on the sort key. The sort key condition can use the comparison operators =, <, <=, >, >= and BETWEEN, or the begins_with function. Non-key attributes cannot appear in it. CustomerId = :c AND OrderDate BETWEEN :start AND :end follows these rules, and DynamoDB returns the matching items in sort key order.
Why the other options are wrong
"CustomerId > :c AND OrderDate BETWEEN :start AND :end" … The partition key must be specified as an equality condition, so a comparison such as greater than is not valid for CustomerId in a key condition expression.
"Status = :s AND OrderDate > :start, with Status as a non-key attribute" … A non-key attribute cannot be used in a key condition expression. A condition on such an attribute belongs in a filter expression, which is applied after the items are read.
"OrderDate BETWEEN :start AND :end, leaving CustomerId out of the condition" … A key condition expression has to include the partition key name and value as an equality condition. The sort key condition is optional and works only together with it.
Key points
Key condition: partition key equality, plus one optional sort key condition. Non-key attributes belong in a filter expression.