DVA-C02 · Question #409
A developer creates an Amazon DynamoDB table. The table has OrderID as the partition key and NumberOfItemsPurchased as the sort key. The data type of the partition key and the sort key is Number…
The correct answer is C. In the Query operation, set the ScanIndexForward parameter to false. To reverse DynamoDB's default ascending sort order on query results, the ScanIndexForward parameter must be set to false in the Query operation.
Question
A developer creates an Amazon DynamoDB table. The table has OrderID as the partition key and NumberOfItemsPurchased as the sort key. The data type of the partition key and the sort key is Number. When the developer queries the table, the results are sorted by NumberOfItemsPurchased in ascending order. The developer needs the query results to be sorted by NumberOfItemsPurchased in descending order. Which solution will meet this requirement?
Options
- ACreate a local secondary index (LSI) on the NumberOfItemsPurchased sort key.
- BChange the sort key from NumberOfItemsPurchased to NumberOfItemsPurchasedDescending.
- CIn the Query operation, set the ScanIndexForward parameter to false.
- DIn the Query operation, set the KeyConditionExpression parameter to false.
How the community answered
(36 responses)- A3% (1)
- B8% (3)
- C83% (30)
- D6% (2)
Why each option
To reverse DynamoDB's default ascending sort order on query results, the ScanIndexForward parameter must be set to false in the Query operation.
A Local Secondary Index on the sort key allows querying on an alternate attribute but does not change the sort direction; results from an LSI are still returned in ascending order by default.
Renaming the sort key attribute does not alter DynamoDB's internal sort behavior; the data would still be stored and returned in ascending numeric order regardless of the attribute name.
The ScanIndexForward parameter controls the sort direction of DynamoDB Query results. When set to true (the default), results are returned in ascending order of the sort key. Setting it to false reverses this to descending order without any schema changes or additional indexes.
The KeyConditionExpression parameter defines the filter conditions for partition and sort key comparisons in a Query; setting it to false is not a valid sort direction control and would cause an API error.
Concept tested: DynamoDB Query ScanIndexForward for descending sort order
Source: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-ScanIndexForward
Community Discussion
No community discussion yet for this question.