DynamoDB: Query Group By (Guide w/ Code Examples)

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
DynamoDB does not implement SQL GROUP BY. For a small dataset, scan and aggregate in application code; for recurring reports, maintain counters with Streams or query an exported dataset.
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, ScanCommand } from "@aws-sdk/lib-dynamodb"; const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); const counts = new Map(); let lastKey; do { const page = await ddb.send(new ScanCommand({ TableName: "Orders", ProjectionExpression: "#status", ExpressionAttributeNames: { "#status": "status" }, ExclusiveStartKey: lastKey, })); for (const item of page.Items ?? []) { if (typeof item.status === "string") counts.set(item.status, (counts.get(item.status) ?? 0) + 1); } lastKey = page.LastEvaluatedKey; } while (lastKey && Object.keys(lastKey).length > 0); console.log(Object.fromEntries(counts));
This counts items by a String status attribute. It reads every item and does not produce a snapshot under concurrent writes.
Reference: AWS Scan API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- DynamoDB: Create if Not Exists
- DynamoDB: Query Global Secondary Index in Nodejs
- DynamoDB: Like
- DynamoDB: Get Last 10 Records
- DynamoDB: Query KeyConditionExpression
- DynamoDB: Batch Get
- DynamoDB: Query Items
- DynamoDB: Get Random Item
- DynamoDB: Delete Multiple Items in Javascript
- DynamoDB: Delete Table
- DynamoDB Get in Typescript
- DynamoDB BatchWriteItem in Typescript
- DynamoDB: Get Unique Values
- DynamoDB: Query Ends With
- DynamoDB: Query JSON
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today
Product Features
DynamoDB Tools
- SSH GUI Client
- Chat With Cloud
- MongoDB GUI Client
- AWS Cost Monitoring
- DynamoDB Query Builder
- DynamoDB Code Examples
- DynamoDB Node.js Queries
- DynamoDB JSON Converter
- DynamoDB UUID Generator
- DynamoDB Local Admin GUI
- DynamoDB Pricing Calculator
- DynamoDB Single-Table Design
- Best DynamoDB GUI Clients
- Dynobase vs DynoTable