TExcelTAD
Home › SQL › SQL HAVING

SQL HAVING

WHERE cannot use aggregate functions, because it runs before the rows are grouped. To filter groups by an aggregate — "countries with more than one customer", "products sold more than 10 times" — use HAVING.

sql
SELECT column, AGGREGATE(…)
FROM table_name
WHERE row_condition
GROUP BY column
HAVING group_condition
ORDER BY …;

Groups with more than one row

sql · try it yourself
SELECT country, COUNT(*) AS customers
FROM customers
GROUP BY country
HAVING COUNT(*) > 1
ORDER BY country;
country | customers
--------+----------
Canada  | 2
France  | 2
Germany | 2
UK      | 2
USA     | 2
(5 rows)

Writing WHERE COUNT(*) > 1 instead would be an error:

sql · try it yourself
SELECT country, COUNT(*) FROM customers WHERE COUNT(*) > 1 GROUP BY country;
Error: misuse of aggregate: COUNT()

Best-selling products

Products with more than 15 units sold:

sql · try it yourself
SELECT p.name, SUM(oi.quantity) AS units_sold
FROM order_items AS oi
JOIN products AS p ON p.product_id = oi.product_id
GROUP BY p.product_id
HAVING SUM(oi.quantity) > 15
ORDER BY units_sold DESC;
name            | units_sold
----------------+-----------
Sparkling Water | 21
Power Bank      | 20
Cast Iron Pan   | 19
Cherry Tomatoes | 18
Bananas         | 16
(5 rows)

WHERE and HAVING together

Both can appear in one query. WHERE removes rows first, then the remaining rows are grouped, then HAVING removes groups. Customers with at least three orders that were not cancelled:

sql · try it yourself
SELECT c.name, COUNT(*) AS active_orders
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id
WHERE o.status <> 'cancelled'
GROUP BY c.customer_id
HAVING COUNT(*) >= 3
ORDER BY active_orders DESC, c.name;
name         | active_orders
-------------+--------------
Amara Costa  | 5
Bright Bytes | 4
Felix Reyes  | 4
Kai Moreau   | 3
(4 rows)

Conditions on several aggregates

HAVING can combine conditions. Categories that have at least 4 products and an average price below 15:

sql · try it yourself
SELECT category_id, COUNT(*) AS products, ROUND(AVG(price), 2) AS avg_price
FROM products
GROUP BY category_id
HAVING COUNT(*) >= 4 AND AVG(price) < 15
ORDER BY category_id;
category_id | products | avg_price
------------+----------+----------
1           | 5        | 10.41
2           | 4        | 9.66
4           | 4        | 3.03
(3 rows)

Which one should I use?

Question Clause
Only look at orders from 2025-06 onward WHERE order_date >= '2025-06-01'
Only customers with more than 2 orders HAVING COUNT(*) > 2
Only products in category 6 WHERE category_id = 6 (works in HAVING too, but filters later and slower)
Only categories whose total stock exceeds 200 HAVING SUM(stock) > 200

Tip: A condition that does not involve an aggregate belongs in WHERE. Filtering early means fewer rows to group.

Test yourself

Why can't you write WHERE COUNT(*) > 1?

  1. COUNT is not a function
  2. WHERE runs before grouping, so aggregates are not available
  3. WHERE only accepts text
  4. It works fine

In which order are these applied: GROUP BY, HAVING, WHERE?

  1. HAVING, WHERE, GROUP BY
  2. WHERE, GROUP BY, HAVING
  3. GROUP BY, WHERE, HAVING
  4. WHERE, HAVING, GROUP BY