SQL HAVING: filtering groups, explained and practiced
HAVING keeps only the groups that meet a condition: departments with at least two people, categories with fewer than 100 units in stock, customers with two or more orders. WHERE can't do this, because it runs before the rows are grouped.
In short: ... GROUP BY department HAVING COUNT(*) >= 2 keeps only departments with at least two rows. HAVING sits right after GROUP BY and checks finished groups. WHERE checks single rows before that and knows nothing about totals or counts yet.
What you should know first: building groups with GROUP BY and filtering rows with WHERE.
Try it now
Count the employees in each department and keep only the departments where two people can cover for each other.
Task HR is planning who covers for whom during time off. Show only departments with at least two employees, with department and the number of employees.
employees 10 rows, 7 columns
| employee_id | first_name | last_name | department | salary | hire_date | manager_id |
|---|---|---|---|---|---|---|
| 1 | Karen | Mitchell | Management | 93600 | 2018-01-02 | NULL |
| 2 | Tom | Bradley | Sales | 62400 | 2019-04-01 | 1 |
| 3 | Julia | Reyes | Sales | 46800 | 2021-06-15 | 2 |
| 4 | Marcus | Allen | Sales | 49200 | 2020-09-01 | 2 |
| 5 | Sarah | Long | Warehouse | 39600 | 2022-02-01 | 1 |
| 6 | Felix | Turner | Warehouse | 35400 | 2024-03-18 | 5 |
| 7 | Nina | Cooper | Accounting | 52800 | 2019-10-01 | 1 |
| 8 | David | Ross | Customer service | 38400 | 2023-05-02 | 2 |
| 9 | Laura | Hughes | Customer service | NULL | 2025-09-01 | 2 |
| 10 | Paul | Ward | Accounting | 43200 | 2022-11-14 | 7 |
How HAVING works
Think of a grouped query as running in this order: FROM fetches the table, WHERE drops single rows, GROUP BY builds the groups, HAVING drops whole groups, then SELECT works out the columns, ORDER BY sorts and LIMIT cuts off. So HAVING only gets its turn once COUNT, SUM and AVG are known for every group. Which categories have an average price above 100?
SELECT category,
COUNT(*) AS products,
ROUND(AVG(price), 2) AS avg_price
FROM products
GROUP BY category
HAVING AVG(price) > 100;
| category | products | avg_price |
|---|---|---|
| Electronics | 3 | 209 |
| Furniture | 2 | 319 |
The other four categories were calculated too, and HAVING threw them out afterwards. The HAVING condition doesn't have to appear in the SELECT list: HAVING SUM(stock) < 50 would work just as well, even though stock isn't shown at all.
WHERE or HAVING?
| WHERE | HAVING | |
|---|---|---|
| checks | single rows | finished groups |
| runs | before GROUP BY | after GROUP BY |
| COUNT, SUM, AVG allowed | no | yes, that's the usual case |
| example | WHERE status = 'delivered' | HAVING COUNT(*) >= 2 |
Rule of thumb: if the condition contains an aggregate function, it goes in HAVING. Everything else goes in WHERE. The next exercise shows that this is more than a matter of style: WHERE stock < 100 and HAVING SUM(stock) < 100 answer two different questions.
Task Purchasing is looking for categories with fewer than 100 units in stock in total. Return category and the total stock of the category, lowest total first.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
WHERE and HAVING together
You'll often need both. WHERE decides which rows make it into the groups, HAVING then decides which groups survive. Which employees have handled at least three delivered orders?
SELECT employee_id, COUNT(*) AS delivered
FROM orders
WHERE status = 'delivered'
GROUP BY employee_id
HAVING COUNT(*) >= 3;
| employee_id | delivered |
|---|---|
| 3 | 5 |
| 4 | 4 |
Without WHERE, open and cancelled orders would be counted too. A condition without an aggregate belongs in WHERE even when it would also work in HAVING: the PostgreSQL documentation calls this more efficient, because rows that are filtered out never have to be grouped. The MySQL manual also advises against putting such conditions in HAVING.
Task Sales wants to thank repeat customers: everyone with at least two orders in status 'delivered'. Return customer_id and the number of delivered orders.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Aliases in HAVING
Once you've named the count with COUNT(*) AS headcount, writing HAVING headcount >= 2 is tempting. The exercise database accepts it, and so does MySQL, which documents it as an extension to the standard. PostgreSQL and SQL Server reject it, because the alias is only created in the SELECT step and HAVING is evaluated before that. So repeat the function instead: HAVING COUNT(*) >= 2 works everywhere.
Common mistakes
COUNT or SUM in WHERE
WHERE COUNT(*) >= 2 can't work, because there's nothing to count yet while a single row is being checked. The exercise tells you: "Functions like COUNT or SUM do not work in WHERE, because WHERE checks single rows. Conditions on groups go in HAVING." You get the same message if you use a count's alias in WHERE, such as WHERE headcount >= 2.
HAVING before GROUP BY, or WHERE after HAVING
... HAVING COUNT(*) >= 2 GROUP BY department gives a syntax error near "GROUP", and ... HAVING COUNT(*) >= 2 WHERE status = 'delivered' one near "WHERE". The message lists the right order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT.
Forgetting GROUP BY
SELECT department, COUNT(*) FROM employees HAVING COUNT(*) >= 2 has been allowed in SQLite since version 3.39.0: without GROUP BY, the whole table forms a single group. You get one row with 10 next to some department, and the exercise says "Your query returns only 1 row. Wanted: 4 rows."
A row condition in HAVING
HAVING COUNT(*) >= 2 AND status = 'delivered' asks for the status of a group that contains many orders with different statuses. According to its documentation, SQLite picks an arbitrary row of the group for that, while PostgreSQL rejects the query. In the exercise you get 5 rows instead of 2. The status condition belongs in WHERE.
HAVING in MySQL, PostgreSQL, SQL Server and SQLite
HAVING with aggregate functions after GROUP BY works the same everywhere. The differences:
| Database | SELECT alias in HAVING | HAVING without GROUP BY |
|---|---|---|
| MySQL | yes (extension) | yes, all rows form one group |
| PostgreSQL | no, write out the expression | yes, all rows form one group |
| SQL Server | no, HAVING is evaluated before SELECT | yes, one implicit group of all rows |
| SQLite (these exercises) | works in the exercises | yes, since version 3.39.0 |
Sources: MySQL Reference Manual (SELECT Statement, MySQL Handling of GROUP BY), PostgreSQL documentation (SELECT, Aggregate Functions in the tutorial), Microsoft Learn (SELECT - HAVING, SELECT statement with the logical processing order), sqlite.org (release 3.39.0, SELECT).
FAQ
Does HAVING always need an aggregate function?
Almost always. A condition on a grouping column, such as HAVING department <> 'Warehouse', is allowed, but it's better placed in WHERE. That way the warehouse rows are never grouped in the first place.
Can I combine several conditions in HAVING?
Yes, with AND and OR just like in WHERE: HAVING COUNT(*) >= 2 AND SUM(salary) > 84000 returns departments with at least two people and a salary total above 84000.
Can HAVING use a different function than the SELECT list?
Yes. You can show the count and filter on the sum, for example SELECT category, COUNT(*) ... HAVING SUM(stock) < 100.
How do I get the top three groups?
Use ORDER BY and LIMIT, not HAVING. HAVING compares each group with a fixed condition; it has no idea of rankings.