How to sort DynamoDB query results in descending order?
Short answer
C In the Query operation, set the ScanIndexForward parameter to false.
Question
Key Takeaways
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.
Common mistake: 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.
Expert Analysis
Why the Answer Is Correct
The ScanIndexForward 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 because KeyConditionExpression 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 of ScanIndexForward=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
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html
This link opens the official vendor documentation in a new tab.
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.
Community Votes
100% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 4 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 4
-
ahadh7621 👍 2 Answered CThis question was on my exam, July 23rd, 2024. Answer is C.
-
albert_kuo 👍 1 Answered Cimport boto3 dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('YourTableName') response = table.query( KeyConditionExpression=boto3.dynamodb.conditions.Key('OrderID').eq(123), ScanIndexForward=False # decending ) items = response['Items'] for item in items: print(item)
-
rdiaz 👍 1 Answered CThis approach directly addresses the requirement without the need for creating additional indexes or modifying the table schema. It leverages the existing sorting capabilities of DynamoDB and provides a straightforward solution.
-
komorebi 👍 1C. In the Query operation, set the ScanIndexForward parameter to false.
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.
Related Analysis
Practice All DVA-C02 Questions
Access 100 questions with complete answers and detailed explanations.
View Full DVA-C02 Practice Test →