Before continuing: This entry is part of the WHERE guide. If you are new to the topic, start with WHERE.
Examples
SELECT city,
COUNT(*) AS active_customers
FROM customers
WHERE active = 1
GROUP BY city
HAVING COUNT(*) >= 2
ORDER BY active_customers DESC, city;Differences and trade-offs
Think of two separate questions
- WHERE: which individual rows take part?
- HAVING: which calculated groups remain?
That distinction is more reliable than memorising which words “go with” each clause. First work out whether the condition can be checked against a single row or needs a total, an average, a maximum or another aggregate.
| Aspect | WHERE | HAVING |
|---|---|---|
| What it filters | Rows | Groups |
| Logical stage | Before grouping | After grouping |
| Typical condition | active = 1 | COUNT(*) >= 2 |
| Effect | Changes which rows feed the calculation | Hides groups that are already calculated |
How to decide, clause by clause
- Write the condition in plain language.
- Ask whether it can be answered by looking at a single row.
- If the answer is yes, start with
WHERE. - If it needs a calculation over the group, use
HAVING. - When you need both, apply
WHEREfollowed byHAVING.
Common mix-ups with WHERE vs HAVING
Trying to use an aggregate in WHERE
SELECT city, COUNT(*)
FROM customers
WHERE COUNT(*) >= 2
GROUP BY city;COUNT(*) does not exist yet at the logical stage of WHERE. That condition belongs to HAVING.
Using HAVING for a row condition
Some engines accept extensions, but filtering earlier with WHERE states the intent better and avoids grouping rows that should not take part.
Forgetting that WHERE changes the calculation
Excluding cancelled orders before adding up gives a different total from adding up every order and filtering groups afterwards.
WHERE decides which rows go in; HAVING decides which groups come out
The practical difference is timing. WHERE works on individual rows before grouping: if a row fails it, the row takes part in no COUNT nor SUM. HAVING works afterwards, on each group's result, which is why it can use aggregate functions. A filter such as status = 'paid' belongs to WHERE; one such as “more than 50 pounds per customer” belongs in HAVING.
Putting a row filter in HAVING is possible when the column is in the GROUP BY, but it mixes two different questions and forces more rows to be grouped than necessary. Conversely, writing WHERE SUM(total) > 50 fails on mainstream engines, because the sum does not exist yet when the row filter runs. If the metric depends on rows that get discarded, the order changes what the result means.
To check a query, compute the summary first without HAVING and see which groups disappear when you add it. Test a customer whose total only clears the threshold if cancelled orders are counted: they should be left out when WHERE excludes those orders. A group that fails HAVING does not leave an empty row; it simply does not appear.
Self-contained case to check in SQLite
Amy has two paid orders and one cancelled order of 100; Liam and Eva have only paid orders.
WITH orders(customer, status, total) AS (
VALUES ('Amy', 'paid', 30), ('Amy', 'paid', 20), ('Amy', 'cancelled', 100),
('Liam', 'paid', 15), ('Eva', 'paid', 60), ('Eva', 'paid', 45)
)
SELECT customer, COUNT(*) AS paid_orders, SUM(total) AS amount
FROM orders
WHERE status = 'paid'
GROUP BY customer
HAVING SUM(total) >= 50
ORDER BY customer;| customer | paid_orders | amount |
|---|---|---|
| Amy | 2 | 50 |
| Eve | 2 | 105 |
Amy's cancelled order is discarded before adding up, so her total is 50 and she reaches the threshold. Liam's orders add up to 15, so his group is left out.
Check your interpretation: What would Amy's total be if the status filter were not in WHERE?
Show answer
150: the sum would include the cancelled order of 100 (30 + 20 + 100).
Technical questions: PostgreSQL: SELECT, columns and aliases (opens in a new tab).