Home / Learn Secure SQL / GROUP BY and HAVING
GROUP BY and HAVING
Summarising rows into groups.
Quick summarySummarising rows into groups.
How to think about this
In plain terms
GROUP BY collapses rows sharing a value into one group per value, usually paired with an aggregate like COUNT or SUM. HAVING filters groups, not individual rows.
Why we use it
Dashboards, reports, and "top N" features are almost always GROUP BY underneath.
Under the hood
Group by the column you want one row per, aggregate the rest, and use HAVING for a condition on the aggregate.
Real-world use
- Web application code review
- Backend development
- Database administration
Spend per userSQL: edit and run
HAVING filters on the aggregate (SUM), which WHERE cannot do.
Quick quiz
1. HAVING filters...
WHERE is for rows, HAVING is for groups.