How to sort DynamoDB query results in descending order?
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?
Community Votes
100% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
This question tests knowledge of the DynamoDB Query API ScanIndexForward parameter, which defaults to true for ascending order, and traps users who think schema changes are required for sorting.
To sort Amazon DynamoDB query results by the sort key in descending order, developers should utilize the ScanIndexForward parameter. The community consensus confirms that setting this parameter to false is the correct and most efficient solution.
A common mistake is selecting Option A, believing a Local Secondary Index is necessary to change sort order, or Option B, thinking the sort order is defined by the attribute name.
Community Discussion (4 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
TheScanIndexForward parameter in the DynamoDB Query API specifically controls the sort order of the results. By default, it is set to true, which returns items in ascending order based on the sort key. Setting this parameter to false instructs DynamoDB to return the results in descending order, directly meeting the requirement without any table modifications.Why the Other Options Are Wrong
Option A is incorrect because a Local Secondary Index (LSI) is used to query with an alternative sort key, not to reverse the sort order of the existing primary key. Option B is incorrect because renaming the sort key attribute does not inherently change the sorting behavior of the query operation. Option D is incorrect becauseKeyConditionExpression is used to filter items based on primary key values, not to control sorting logic.Community Comment Notes
Community members confirmed that this question appeared on recent exams, validating Option C as the solution. One user provided a Python Boto3 code snippet demonstrating the implementation ofScanIndexForward=False, which helps visualize the correct syntax. Another commenter noted that this approach is optimal because it avoids the overhead and complexity of creating additional indexes or modifying the table schema. Official Reference
Exam Strategy
Remember that ScanIndexForward defaults to true (ascending). When a question asks for descending order in a DynamoDB query, immediately look for the option that sets this parameter to false.
Related Analysis
Practice All DVA-C02 Questions
Access 100 questions with complete answers and detailed explanations.
View Full DVA-C02 Practice Test →