Group By & Having in Drizzle ORM
From the Drizzle ORM cheat sheet · Aggregations · verified Jul 2026
Group By & Having
Group results and filter aggregated data.
typescript
import { eq, gt, count } from 'drizzle-orm';
// Group by with count
const postsByAuthor = await db.select({
authorId: posts.authorId,
postCount: count(),
}).from(posts)
.groupBy(posts.authorId);
// Having — filter groups
const prolificAuthors = await db.select({
authorId: posts.authorId,
postCount: count(),
}).from(posts)
.groupBy(posts.authorId)
.having(gt(count(), 5));💡 groupBy groups rows before aggregation
⚡ having filters after aggregation (vs where)
📌 groupBy all non-aggregate selected columns
🟢 Combine with joins for cross-table aggregates
aggregationgroup-byhaving