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?

  1. Create a local secondary index (LSI) on the NumberOfItemsPurchased sort key.
  2. Change the sort key from NumberOfItemsPurchased to NumberOfItemsPurchasedDescending.
  3. In the Query operation, set the ScanIndexForward parameter to false. Source Reference Answer
  4. In the Query operation, set the KeyConditionExpression parameter to false.

Community Votes

C
100%

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)

albert_kuo 👍 1 Selected: C
import 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)
ahadh7621 👍 2 Selected: C
This question was on my exam, July 23rd, 2024. Answer is C.
komorebi 👍 1
C. In the Query operation, set the ScanIndexForward parameter to false.
rdiaz 👍 1 Selected: C
This 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.

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

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

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 →

← Back to DVA-C02 Study Guide