We order query results to see data in a specific sequence, like oldest to newest or highest to lowest.
Query result ordering (ascending, descending) in DynamoDB
Introduction
Syntax
DynamoDB
const QueryInput = {
TableName: 'YourTableName',
KeyConditionExpression: 'PartitionKeyName = :value',
ExpressionAttributeValues: {
':value': { S: 'YourPartitionKeyValue' }
},
ScanIndexForward: true_or_false
};ScanIndexForward controls order: true means ascending, false means descending.
Ordering works only on the sort key of the table or index.
Examples
DynamoDB
ScanIndexForward: true
DynamoDB
ScanIndexForward: false
Sample Program
This query fetches orders for customer 'C123' and shows the order dates from newest to oldest.
DynamoDB
const { DynamoDBClient, QueryCommand } = require('@aws-sdk/client-dynamodb');
const client = new DynamoDBClient({ region: 'us-east-1' });
async function runQuery() {
const params = {
TableName: 'Orders',
KeyConditionExpression: 'CustomerId = :cid',
ExpressionAttributeValues: {
':cid': { S: 'C123' }
},
ScanIndexForward: false
};
const command = new QueryCommand(params);
const data = await client.send(command);
return data.Items.map(item => item.OrderDate.S);
}
runQuery().then(console.log).catch(console.error);Important Notes
If you don't set ScanIndexForward, the default is true (ascending order).
Ordering only applies when you query by partition key and have a sort key defined.
Summary
Use ScanIndexForward to control ascending or descending order.
Ordering works on the sort key, not the partition key.
Default order is ascending if you don't specify.
Practice
1. In DynamoDB, which parameter controls whether query results are returned in ascending or descending order based on the sort key?
easy
Solution
Step 1: Understand query ordering in DynamoDB
DynamoDB orders query results based on the sort key, and the order can be controlled.Step 2: Identify the controlling parameter
The parameterScanIndexForwardcontrols ascending (true) or descending (false) order.Final Answer:
ScanIndexForward-> Option CQuick Check:
Ordering parameter = ScanIndexForward [OK]
Hint: Remember: ScanIndexForward controls ascending/descending order [OK]
Common Mistakes:
- Confusing ScanIndexForward with ReturnConsumedCapacity
- Thinking ordering applies to partition key
- Assuming default order is descending
2. Which of the following is the correct syntax to query a DynamoDB table named
Orders with results in descending order on the sort key OrderDate?easy
Solution
Step 1: Identify the parameter for descending order
To get descending order,ScanIndexForwardmust be set tofalse.Step 2: Check the syntax correctness
client.query({ TableName: 'Orders', KeyConditionExpression: 'CustomerId = :id', ExpressionAttributeValues: { ':id': '123' }, ScanIndexForward: false }) usesScanIndexForward: falsecorrectly; other options use invalid or wrong parameters.Final Answer:
Option B syntax with ScanIndexForward false -> Option BQuick Check:
Descending order = ScanIndexForward false [OK]
Hint: Use ScanIndexForward false for descending order [OK]
Common Mistakes:
- Using ScanIndexForward true for descending order
- Using non-existent parameters like Descending or OrderBy
- Confusing partition key with sort key ordering
3. Given a DynamoDB table with partition key
UserId and sort key Timestamp, what will be the order of results returned by this query?client.query({
TableName: 'UserActivity',
KeyConditionExpression: 'UserId = :uid',
ExpressionAttributeValues: { ':uid': 'user123' },
ScanIndexForward: false
})medium
Solution
Step 1: Understand ScanIndexForward effect
SettingScanIndexForward: falsereturns results in descending order of the sort key.Step 2: Identify the sort key
The sort key isTimestamp, so results are ordered newest to oldest.Final Answer:
Results ordered by Timestamp descending (newest first) -> Option DQuick Check:
ScanIndexForward false = descending order [OK]
Hint: ScanIndexForward false means newest items first [OK]
Common Mistakes:
- Assuming ScanIndexForward false orders by partition key
- Thinking default order is descending
- Confusing ascending and descending meanings
4. You wrote this DynamoDB query but the results are always in ascending order, even though you want descending order:
What is the error?
client.query({
TableName: 'Sales',
KeyConditionExpression: 'StoreId = :sid',
ExpressionAttributeValues: { ':sid': 'store1' },
ScanIndexForward: 'false'
})What is the error?
medium
Solution
Step 1: Check ScanIndexForward data type
ScanIndexForward expects a boolean true or false, not a string.Step 2: Identify impact of wrong type
Passing 'false' as a string is truthy, so DynamoDB treats it as true (ascending order).Final Answer:
ScanIndexForward must be boolean false, not string 'false' -> Option AQuick Check:
Boolean type needed for ScanIndexForward [OK]
Hint: Use boolean false, not string 'false' for ScanIndexForward [OK]
Common Mistakes:
- Passing 'false' as a string instead of boolean
- Misunderstanding KeyConditionExpression syntax
- Ignoring data types in parameters
5. You want to retrieve the 5 most recent orders for customer
cust123 from a DynamoDB table Orders with partition key CustomerId and sort key OrderDate. Which query will correctly return these orders in descending order by OrderDate?hard
Solution
Step 1: Set descending order for most recent first
UseScanIndexForward: falseto get descending order byOrderDate.Step 2: Limit results to 5
UseLimit: 5to get only the top 5 recent orders.Final Answer:
Query with ScanIndexForward false and Limit 5 -> Option AQuick Check:
Descending + Limit 5 = ScanIndexForward false + Limit 5 [OK]
Hint: Use ScanIndexForward false with Limit 5 for recent 5 items [OK]
Common Mistakes:
- Using ScanIndexForward true returns oldest first
- Omitting Limit returns all items
- Not combining descending order with limit
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
