DynamoDB: Query Greater Than (Guide w/ Code Examples)

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
A greater-than comparison can narrow a Query on the sort key after equality on the partition key. This example compares String sort keys lexicographically.
Install @aws-sdk/client-dynamodb and @aws-sdk/lib-dynamodb, use an ES module on a supported Node.js runtime, and configure an AWS Region and credentials. The examples use an existing Orders table with String keys pk and sk unless an index or timestamp key is described.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb"; const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); let lastKey; do { const page = await ddb.send(new QueryCommand({ TableName: "Orders", KeyConditionExpression: "#pk = :pk AND #sk > :after", ExpressionAttributeNames: { "#pk": "pk", "#sk": "sk" }, ExpressionAttributeValues: { ":pk": "CUSTOMER#123", ":after": "ORDER#010" }, ExclusiveStartKey: lastKey, })); for (const item of page.Items ?? []) console.log(item); lastKey = page.LastEvaluatedKey; } while (lastKey && Object.keys(lastKey).length > 0);
Use a Number sort key for numeric comparisons. If the compared attribute is not a key, a FilterExpression filters after reading and does not reduce read capacity.
Reference: AWS Query API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- DynamoDB: Attribute Not Null
- DynamoDB: Get Multiple Items
- DynamoDB: Query Date Range
- DynamoDB: Query Global Secondary Index in Nodejs
- DynamoDB: Get Unique Values
- DynamoDB BatchWriteItem in Typescript
- DynamoDB: Query Group By
- DynamoDB: Batch Get
- DynamoDB: Query Ends With
- DynamoDB: Delete Multiple Items in Javascript
- DynamoDB: Like
- DynamoDB: Delete All Items With Partition Key
- DynamoDB: Get Random Item
- DynamoDB: Query Items
- DynamoDB: Create if Not Exists
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today