SQL-01-008-P · Primer — Filtering Groups with HAVING
The idea in one sentence
WHERE throws out individual rows before they're grouped; HAVING throws out whole groups after they've been grouped and summed — so when your condition is about an aggregate ("customers who spent more than $100 total"), you can't use WHERE, because at WHERE time the total doesn't exist yet.
Why it exists
You already know how to filter rows with WHERE and how to collapse rows into groups with GROUP BY plus an aggregate like SUM or COUNT. But sooner or later you'll want to filter on the result of that collapse: only the departments with more than 10 employees, only the products that sold fewer than 5 units, only the customers whose total spend crossed a threshold. Reach for WHERE and you hit a wall — most databases will reject it with an error like "aggregate functions are not allowed in WHERE."
That error isn't the database being fussy; it's a consequence of when each clause runs. WHERE is evaluated row by row, before any grouping happens. At that moment SUM(amount) is meaningless — there's no group to sum over yet, just one lonely row at a time. The total only comes into existence after GROUP BY has gathered the rows and the aggregate has run over each bucket.
HAVING exists to give you a filter that runs at that later moment. It's the "second filter" of a query: WHERE decides which rows are even allowed into the grouping, and HAVING decides which of the resulting groups survive. Both are filters — they just fire at two different stages of the pipeline, and knowing which stage your condition belongs to is the whole skill.
How it works
A grouped query runs in a fixed order: FROM (get the rows) → WHERE (drop rows) → GROUP BY (bucket the survivors) → aggregate (compute SUM, COUNT, ... per bucket) → HAVING (drop buckets) → SELECT → ORDER BY. Because HAVING runs after the aggregates are computed, it's the one place you're allowed to write conditions like SUM(amount) > 100 — the total already exists by then. Use WHERE for conditions on raw column values (they're checked before grouping) and HAVING for conditions on aggregates (checked after). A well-tuned query often uses both: WHERE first to shrink the data, then HAVING to filter the summaries. Here we ask a single question — which customers spent more than $100 in total? — and only HAVING can express it:
CREATE TABLE orders (id INTEGER, customer TEXT, amount INTEGER);
INSERT INTO orders VALUES
(1,'Ana', 40), (2,'Ana', 70), (3,'Ana', 30),
(4,'Ben', 90), (5,'Ben', 20),
(6,'Cleo',150),
(7,'Dara', 10);
SELECT customer, SUM(amount) AS total
FROM orders
GROUP BY customer
HAVING SUM(amount) > 100 -- filter GROUPS by their total
ORDER BY total DESC;
-- customer total
-- -------- -----
-- Cleo 150
-- Ana 140 (40 + 70 + 30)
-- Ben 110 (90 + 20)
-- Dara (total 10) is dropped: its group failed HAVING
Ana's three separate orders (40, 70, 30) never each cleared 100 on their own, yet her group totals 140 and survives — that's the point of filtering after the sum. Dara's single 10-dollar order forms a group that totals 10, fails HAVING SUM(amount) > 100, and vanishes entirely.
The one gotcha
HAVING and WHERE are not interchangeable, and the deep mistake is putting an aggregate in WHERE (or a plain row filter in HAVING) out of habit. If you write WHERE SUM(amount) > 100, the database errors out, because at WHERE time there is no sum — grouping hasn't happened. The subtler, quieter bug goes the other way: filtering that could go in WHERE gets stuffed into HAVING. Say you only care about orders since January. Writing HAVING SUM(amount) > 100 with a date test bolted on is wrong-headed — the date is a property of individual rows, so it belongs in WHERE date >= '2026-01-01', which drops old rows before the totals are computed. Push it into HAVING and old orders still inflate every customer's SUM before the filter looks at it, so your totals are computed over data you meant to exclude — no error, just wrong numbers. The rule that keeps you safe: row-level conditions go in WHERE; aggregate conditions go in HAVING. Filter as early as you can — WHERE shrinks the data cheaply before grouping — and save HAVING for the questions that can only be answered once the groups exist.
See it — where WHERE and HAVING sit in the pipeline
flowchart TB
A["FROM orders<br/>(all rows)"] --> B["WHERE<br/>drop individual rows<br/>(before grouping)"]
B --> C["GROUP BY customer<br/>bucket the rows"]
C --> D["Aggregate<br/>SUM(amount) per group"]
D --> E["HAVING SUM > 100<br/>drop whole groups<br/>(after grouping)"]
E --> F["SELECT + ORDER BY<br/>final rows"]
B -. "row filter<br/>needs raw columns" .-> B
E -. "group filter<br/>needs the aggregate" .-> E
The one-line takeaway
WHERE filters rows before the group-by; HAVING filters groups after it — so any condition that mentions SUM, COUNT, AVG, MIN, or MAX belongs in HAVING, and everything about a raw column belongs in WHERE.
Where you'll meet it
You'll meet HAVING the first time a report asks for "only the busy somethings" — customers with more than N orders, products with total sales over a threshold, days that saw more than X signups, error codes that appeared more than 100 times in the logs. It's the natural next breath after GROUP BY: you group to summarize, then HAVING to keep only the summaries worth showing. It's a staple of data-analyst and backend interviews, usually phrased as a trap: "find customers who spent more than $500" or "list the tags used on at least 10 posts" — and the interviewer is quietly watching whether you reach for WHERE (wrong, it errors on the aggregate) or HAVING (right). The strongest answers go one step further and use both clauses on purpose — WHERE to trim the rows before grouping (say, only this year's orders), then HAVING to filter the totals — and can say out loud why the date filter belongs in WHERE and the total filter belongs in HAVING.
Named concepts: HAVING clause, HAVING vs WHERE, filtering on aggregates
