Comparison · Grouping and calculating

WHERE vs HAVING

WHERE filters rows before grouping; HAVING filters groups after the aggregates are calculated.

Before continuing: This entry is part of the WHERE guide. If you are new to the topic, start with WHERE.

Examples

SQL
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.

WHERE vs HAVING compared
AspectWHEREHAVING
What it filtersRowsGroups
Logical stageBefore groupingAfter grouping
Typical conditionactive = 1COUNT(*) >= 2
EffectChanges which rows feed the calculationHides groups that are already calculated

How to decide, clause by clause

  1. Write the condition in plain language.
  2. Ask whether it can be answered by looking at a single row.
  3. If the answer is yes, start with WHERE.
  4. If it needs a calculation over the group, use HAVING.
  5. When you need both, apply WHERE followed by HAVING.

Common mix-ups with WHERE vs HAVING

Trying to use an aggregate in WHERE

Incorrect
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.

Data and query · SQLite
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;
Verified result of the self-contained case
customerpaid_ordersamount
Amy250
Eve2105

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).