GROUP BY turns many rows into one row per group, and HAVING filters those groups. The most common confusion is the difference between HAVING and WHERE, which comes down to when each one runs.
What GROUP BY does
GROUP BY collects rows that share the same value in one or more columns and lets you compute an aggregate, such as a count, sum or average, for each group.
SELECT region, COUNT(*) AS orders, SUM(amount) AS revenue
FROM orders
GROUP BY region;
This returns one row per region. You can group by several columns, such as region and month, and you get one row for each distinct combination.
The rule for selected columns
Every column in the SELECT list must either appear in the GROUP BY clause or be wrapped in an aggregate function. Selecting a plain column that is neither is an error in standard SQL, because the database does not know which of the group’s values to show. Some databases are more permissive here, which can hide mistakes, so write queries that follow the strict rule.
WHERE versus HAVING
WHERE filters individual rows before grouping. HAVING filters whole groups after the aggregates are calculated. The two answer different questions.
SELECT region, SUM(amount) AS revenue
FROM orders
WHERE order_date >= DATE '2026-01-01' -- filter rows first
GROUP BY region
HAVING SUM(amount) > 10000; -- then filter groups
The first condition keeps only orders from 2026. The second keeps only regions whose total passes 10,000. You cannot use an aggregate such as SUM(amount) in a WHERE clause, because the groups do not exist yet when WHERE runs.
The order SQL runs in
Reading a query top to bottom is not how it executes. Logically, the database handles FROM and joins first, then WHERE, then GROUP BY, then HAVING, then SELECT, and finally ORDER BY. This explains why a column alias defined in SELECT cannot be used in WHERE, and why support for aliases in HAVING varies by database. If you are unsure, repeat the expression or move the aggregate into a named step, as described in SQL CTEs explained.
Putting a filter in the right place
If a condition can be checked on a single row, put it in WHERE, since it reduces the data before grouping and can use an index, as explained in SQL indexes. Use HAVING only for conditions on aggregates. Moving row-level filters out of HAVING is a common way to speed up a slow query.
Counting distinct values and conditional counts
COUNT(DISTINCT customer_id) counts unique customers per group instead of rows, which is the fix when joined data makes totals too high. To count only some rows within each group, use a conditional expression inside the aggregate.
SELECT region,
COUNT(*) AS orders,
SUM(CASE WHEN amount > 100 THEN 1 ELSE 0 END) AS large_orders
FROM orders
GROUP BY region;
Things that surprise people
- NULLs in the grouping column form a single group, and aggregates skip NULL values, as covered in SQL NULL explained.
- Joining before grouping can multiply rows, so totals double count, a problem described in SQL joins explained.
- Without an
ORDER BY, the order of the groups in the result is not guaranteed.
A real example
Grouping is the first step of most reports, including the retention table built in cohort analysis in SQL, which counts customers by cohort and age.
A quick checklist
- Is every selected column either grouped or aggregated?
- Are row-level conditions in WHERE and aggregate conditions in HAVING?
- Are you counting distinct values where joins could duplicate rows?
- Did you check how NULLs affect your groups and totals?
- Did you add ORDER BY if the order matters?
If you want reporting queries and dashboards built on your data, see my analytics dashboards service.
FAQ
What is the difference between WHERE and HAVING?
WHERE filters rows before they are grouped. HAVING filters groups after the aggregates are calculated, so it is the place for conditions on counts, sums and averages.
Can I use HAVING without GROUP BY?
Yes in many databases, where it treats the whole result as a single group, but it is rarely useful. Normally HAVING is paired with GROUP BY.
Why does my GROUP BY query fail with a column error?
A selected column is neither in the GROUP BY clause nor inside an aggregate function. Add it to the grouping or aggregate it.

