DynamoDB: Query Date Range (Guide w/ Code Examples)

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
Query a timestamp sort key with BETWEEN inside one partition-key value. The bounds are inclusive; the example uses fixed-width UTC timestamp strings and reads every page.
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 BETWEEN :start AND :end", ExpressionAttributeNames: { "#pk": "pk", "#sk": "sk" }, ExpressionAttributeValues: { ":pk": "CUSTOMER#123", ":start": "2026-10-01T00:00:00.000Z", ":end": "2026-10-31T23:59:59.999Z", }, ExclusiveStartKey: lastKey, })); for (const item of page.Items ?? []) console.log(item); lastKey = page.LastEvaluatedKey; } while (lastKey && Object.keys(lastKey).length > 0);
Normalize time zones and precision before writing. A timestamp stored only as a non-key attribute cannot narrow the items read by a key condition.
Reference: AWS Query API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- DynamoDB: BatchGetItem
- DynamoDB: Query Count
- DynamoDB: Delete All Items With Partition Key
- DynamoDB: Create if Not Exists
- DynamoDB: Attribute Not Null
- DynamoDB: Get Last Inserted Item
- DynamoDB: Like
- DynamoDB: Get By ID
- DynamoDB: GetItem
- DynamoDB: Query Items
- DynamoDB: Query Group By
- DynamoDB: Get Multiple Items
- DynamoDB: Get Last 10 Records
- DynamoDB: Query Greater Than
- DynamoDB: Query Global Secondary Index in Nodejs
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today