SQL Tutorial › Functions & Grouping › HAVING

SQL HAVING Clause

IntermediateLesson 25 of 699 min read6 queries run on MySQL 8.4

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.

Advertisement
In this lesson
  • Filter groups with HAVING
  • Understand exactly when to use WHERE and when HAVING
  • Combine both in one query
  • Find duplicate values with GROUP BY ... HAVING COUNT(*) > 1

HAVING syntax

MySQL
SELECT group_column, AGGREGATE(column)FROM table_nameWHERE row_conditionGROUP BY group_columnHAVING group_condition;

Sample data

orders · 10 rows
order_idcustomer_idorder_datestatus
100112025-01-05Delivered
100222025-01-17Delivered
100312025-02-02Delivered
100432025-02-14Cancelled
100542025-03-08Delivered
100652025-03-22Shipped
100722025-04-10Delivered
100812025-04-28Pending
100932025-05-15Delivered
101052025-06-01Delivered

Filter groups by COUNT

Customers with more than one order:

SQL
SELECT customer_id, COUNT(*) AS ordersFROM ordersGROUP BY customer_idHAVING COUNT(*) > 1ORDER BY customer_id;
ResultMySQL 8.4
customer_idorders
13
22
32
52
4 rows in set
How this query works, step by step
  1. GROUP BY customer_id makes one group per customer: five groups.
  2. COUNT(*) counts the orders in each group.
  3. HAVING COUNT(*) > 1 keeps the groups with 2 or more orders. Customer 4 has only one and is dropped.
Advertisement

Filter groups by SUM

Customers who spent more than 5,000 on non-cancelled orders:

SQL
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;
ResultMySQL 8.4
customer_idspent
130038
29798
56642
3 rows in set
How this query works, step by step
  1. The joins give every order line its customer and price.
  2. WHERE o.status <> 'Cancelled' removes the cancelled order’s line before grouping.
  3. GROUP BY o.customer_id totals each customer’s lines.
  4. HAVING spent > 5000 keeps only the big spenders. MySQL allows the alias here; standard SQL would repeat SUM(oi.quantity * p.price).

WHERE vs HAVING

WHEREHAVING
Filtersindividual rowsgroups
Runsbefore GROUP BYafter GROUP BY
Can use aggregatesnoyes
Can use plain columnsyesonly grouped columns

Using an aggregate in WHERE is an error:

MySQL
SELECT dept_id, COUNT(*)FROM employeesWHERE COUNT(*) > 2GROUP BY dept_id;
ResultMySQL 8.4
ERROR 1111 (HY000): Invalid use of group function
Tip

If a condition does not need an aggregate, put it in WHERE. Filtering rows early means fewer rows to group, which is faster.

Advertisement

WHERE and HAVING together

Departments where at least two employees earn 50,000 or more:

SQL
SELECT dept_id, COUNT(*) AS well_paidFROM employeesWHERE salary >= 50000GROUP BY dept_idHAVING COUNT(*) >= 2ORDER BY dept_id;
ResultMySQL 8.4
dept_idwell_paid
14
22
2 rows in set
How this query works, step by step
  1. WHERE salary >= 50000 removes the three salaries below 50,000 first.
  2. The remaining rows are grouped by department and counted.
  3. HAVING COUNT(*) >= 2 keeps 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:

SQL
SELECT salary, COUNT(*) AS people, GROUP_CONCAT(first_name) AS namesFROM employeesGROUP BY salaryHAVING COUNT(*) > 1;
ResultMySQL 8.4
salarypeoplenames
720002Priya,Karan
1 row in set
How this query works, step by step
  1. Rows are grouped by salary.
  2. 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:

SQL
SELECT category, ROUND(AVG(price)) AS avg_price, COUNT(*) AS productsFROM productsGROUP BY categoryHAVING AVG(price) > 3000;
ResultMySQL 8.4
categoryavg_priceproducts
Furniture80744
1 row in set
Advertisement

Exercise: Popular products

List the product_ids that appear in two or more order lines, with the number of lines.

Expected resultMySQL 8.4
product_idorder_lines
23
63
12
42
72
82
6 rows in set
Hint 1

Group order_items by product_id and filter with HAVING COUNT(*) >= 2.

Exercise: Cities with one employee

Find the cities where exactly one employee works.

Expected resultMySQL 8.4
citystaff
Kolkata1
Delhi1
2 rows in set
Hint 1

HAVING COUNT(*) = 1.

Quick quiz

1. When does HAVING run?

2. Which clause can use COUNT(*) > 2?

3. How do you find duplicate emails in a users table?

Advertisement

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.