Aggregate Functions in Drizzle ORM

From the Drizzle ORM cheat sheet · Aggregations · verified Jul 2026

Aggregate Functions

Use count, sum, avg, min, and max for data aggregation.

typescript
import { eq, count, sum, avg, min, max } from 'drizzle-orm';

// Count all rows
const [{ total }] = await db.select({
  total: count(),
}).from(users);

// Sum
const [{ totalRevenue }] = await db.select({
  totalRevenue: sum(orders.amount),
}).from(orders);
💡 count() with no args counts all rows
⚡ count(column) counts non-null values only
📌 sum/avg return string — cast if needed
🟢 Use sql tag for count(distinct ...) queries
aggregationcountsumavg

More Drizzle ORM tasks

Back to the full Drizzle ORM cheat sheet