AWS Certified Developer – Associate / Question #952 of 557

Question #952

A developer designs an Amazon DynamoDB table with CustomerID as the partition key and OrderDate as the sort key, both of type Number. When querying the table, results are sorted by OrderDate in ascending order. The developer needs the results sorted in descending order. What solution meets this requirement?

A

Create a global secondary index (GSI) with OrderDate as the sort key.

B

Modify the sort key to be OrderDateDescending and update the data type to String.

C

In the Query operation, set the ScanIndexForward parameter to false.

D

In the Query operation, set the KeyConditionExpression to use a descending sort.

Explanation

The correct answer is C because DynamoDB's Query API includes the ScanIndexForward parameter, which controls the sort order of results based on the sort key. By default, ScanIndexForward is set to 'true', returning results in ascending order. Setting it to 'false' reverses the order to descending. This approach requires no schema changes or additional indexes.

Why other options are incorrect:
- A: Creating a GSI with OrderDate as the sort key would not change the sort order; it would still default to ascending.
- B: Changing the sort key's name or data type does not inherently reverse the order and complicates data management.
- D: KeyConditionExpression defines query conditions, not the sort order. The sort direction is controlled by ScanIndexForward.

Key Takeaway: Use ScanIndexForward=false in Query operations to reverse sort order in DynamoDB.

Answer

The correct answer is: C