SQL GROUP BY statement

Here is a clear, practical, and complete guide to the SQL GROUP BY statement, with examples, rules, and common pitfalls.
What SQL GROUP BY statement Does
GROUP BY groups rows that share the same values so you can apply aggregate functions such as:
COUNT()SUM()AVG()MAX()
It turns many rows into summary rows.
Basic Syntax
Basic Examples
1. Count employees per department
2. Total sales per customer
3. Average score per class
GROUP BY with Multiple Columns
This groups first by country, then city.
GROUP BY + HAVING (Filter Aggregated Results)
HAVING is used after aggregation.WHERE filters rows before grouping.
Example: Departments with more than 10 employees
GROUP BY with ORDER BY
Sort aggregated results:
Real-World Examples
Monthly revenue
(PostgreSQL example using DATE_TRUNC)
Count logins per user
Maximum order per customer
Important Rules & Pitfalls
1. Every non-aggregated column MUST be in GROUP BY
Invalid:
Valid:
2. Aggregate functions ignore NULLs
COUNT(column) ignores NULLSUM, AVG, MIN, MAX ignore NULL
3. Use HAVING for aggregated filters
WHERE → before groupingHAVING → after grouping
4. GROUP BY with DISTINCT isn’t usually needed
This:
Is equal to:
Performance Tips
- Index columns used in GROUP BY
- Use GROUP BY on low-cardinality columns carefully
- Avoid grouping on expressions unless necessary
- In large datasets, consider materialized views
