SQL HAVING Clause
WHERE filters rows; HAVING filters groups. Use it when the condition is about an aggregate: departments with more than two people, customers who spent over 10,000, emails that appear more than once. It runs after GROUP BY, so it can see COUNT, SUM and the other aggregates.
- Filter groups with
HAVING - Understand exactly when to use
WHEREand whenHAVING - Combine both in one query
- Find duplicate values with
GROUP BY ... HAVING COUNT(*) > 1
HAVING syntax
SELECT group_column, AGGREGATE(column)FROM table_nameWHERE row_conditionGROUP BY group_columnHAVING group_condition;Sample data
| order_id | customer_id | order_date | status |
|---|---|---|---|
| 1001 | 1 | 2025-01-05 | Delivered |
| 1002 | 2 | 2025-01-17 | Delivered |
| 1003 | 1 | 2025-02-02 | Delivered |
| 1004 | 3 | 2025-02-14 | Cancelled |
| 1005 | 4 | 2025-03-08 | Delivered |
| 1006 | 5 | 2025-03-22 | Shipped |
| 1007 | 2 | 2025-04-10 | Delivered |
| 1008 | 1 | 2025-04-28 | Pending |
| 1009 | 3 | 2025-05-15 | Delivered |
| 1010 | 5 | 2025-06-01 | Delivered |
Filter groups by COUNT
Customers with more than one order:
SELECT customer_id, COUNT(*) AS ordersFROM ordersGROUP BY customer_idHAVING COUNT(*) > 1ORDER BY customer_id;| customer_id | orders |
|---|---|
| 1 | 3 |
| 2 | 2 |
| 3 | 2 |
| 5 | 2 |
GROUP BY customer_idmakes one group per customer: five groups.COUNT(*)counts the orders in each group.HAVING COUNT(*) > 1keeps the groups with 2 or more orders. Customer 4 has only one and is dropped.
Filter groups by SUM
Customers who spent more than 5,000 on non-cancelled orders:
SELECT o.customer_id, SUM(oi.quantity * p.price) AS spentFROM orders oJOIN order_items oi ON oi.order_id = o.order_idJOIN products p ON p.product_id = oi.product_idWHERE o.status <> 'Cancelled'GROUP BY o.customer_idHAVING spent > 5000ORDER BY spent DESC;| customer_id | spent |
|---|---|
| 1 | 30038 |
| 2 | 9798 |
| 5 | 6642 |
- The joins give every order line its customer and price.
WHERE o.status <> 'Cancelled'removes the cancelled order’s line before grouping.GROUP BY o.customer_idtotals each customer’s lines.HAVING spent > 5000keeps only the big spenders. MySQL allows the alias here; standard SQL would repeatSUM(oi.quantity * p.price).
WHERE vs HAVING
| WHERE | HAVING | |
|---|---|---|
| Filters | individual rows | groups |
| Runs | before GROUP BY | after GROUP BY |
| Can use aggregates | no | yes |
| Can use plain columns | yes | only grouped columns |
Using an aggregate in WHERE is an error:
SELECT dept_id, COUNT(*)FROM employeesWHERE COUNT(*) > 2GROUP BY dept_id;ERROR 1111 (HY000): Invalid use of group function
If a condition does not need an aggregate, put it in WHERE. Filtering rows early means fewer rows to group, which is faster.
WHERE and HAVING together
Departments where at least two employees earn 50,000 or more:
SELECT dept_id, COUNT(*) AS well_paidFROM employeesWHERE salary >= 50000GROUP BY dept_idHAVING COUNT(*) >= 2ORDER BY dept_id;| dept_id | well_paid |
|---|---|
| 1 | 4 |
| 2 | 2 |
WHERE salary >= 50000removes the three salaries below 50,000 first.- The remaining rows are grouped by department and counted.
HAVING COUNT(*) >= 2keeps departments with two or more such people: Engineering (4) and Sales (2).
Find duplicates
GROUP BY ... HAVING COUNT(*) > 1 is the standard way to find duplicate values:
SELECT salary, COUNT(*) AS people, GROUP_CONCAT(first_name) AS namesFROM employeesGROUP BY salaryHAVING COUNT(*) > 1;| salary | people | names |
|---|---|---|
| 72000 | 2 | Priya,Karan |
- Rows are grouped by salary.
- Only salaries shared by more than one employee survive: 72,000 (Priya and Karan).
HAVING with AVG
Product categories whose average price is above 3,000:
SELECT category, ROUND(AVG(price)) AS avg_price, COUNT(*) AS productsFROM productsGROUP BY categoryHAVING AVG(price) > 3000;| category | avg_price | products |
|---|---|---|
| Furniture | 8074 | 4 |
Exercise: Popular products
List the product_ids that appear in two or more order lines, with the number of lines.
| product_id | order_lines |
|---|---|
| 2 | 3 |
| 6 | 3 |
| 1 | 2 |
| 4 | 2 |
| 7 | 2 |
| 8 | 2 |
Hint 1
Group order_items by product_id and filter with HAVING COUNT(*) >= 2.
SELECT product_id, COUNT(*) AS order_linesFROM order_itemsGROUP BY product_idHAVING COUNT(*) >= 2ORDER BY order_lines DESC, product_id;| product_id | order_lines |
|---|---|
| 2 | 3 |
| 6 | 3 |
| 1 | 2 |
| 4 | 2 |
| 7 | 2 |
| 8 | 2 |
Exercise: Cities with one employee
Find the cities where exactly one employee works.
| city | staff |
|---|---|
| Kolkata | 1 |
| Delhi | 1 |
Hint 1
HAVING COUNT(*) = 1.
SELECT city, COUNT(*) AS staffFROM employeesGROUP BY cityHAVING COUNT(*) = 1;| city | staff |
|---|---|
| Kolkata | 1 |
| Delhi | 1 |
Quick quiz
1. When does HAVING run?
HAVING filters the groups GROUP BY produced.
2. Which clause can use COUNT(*) > 2?
Aggregates are allowed in HAVING, not in WHERE.
3. How do you find duplicate emails in a users table?
Group by the value and keep the groups with more than one row.
HAVING FAQ
What is the difference between WHERE and HAVING in SQL?
WHERE filters rows before they are grouped and cannot use aggregate functions. HAVING filters groups after GROUP BY and can use aggregates such as COUNT(*) or SUM(amount).
Can HAVING be used without GROUP BY?
Yes. Without GROUP BY the whole table is one group, so SELECT COUNT() FROM orders HAVING COUNT() > 5 returns one row or none.
Can I use a column alias in HAVING?
In MySQL, yes. Standard SQL and some other databases require you to repeat the aggregate expression.
How do I find duplicate records in SQL?
Group by the columns that should be unique and keep groups with HAVING COUNT(*) > 1.