SQL HAVING Clause: GROUP BY Filters, Examples, and HAVING vs WHERE
For SQL learners who know GROUP BY and want to keep only some of the groups; every query is shown with the rows it returns.
On this page
In a GROUP BY query, HAVING keeps the groups that pass a condition on SUM, COUNT or another aggregate function. For example, you can list only the customers who placed more than two orders.
Every HAVING query has this shape:
SELECT column, aggregate(column)
FROM table_name
WHERE row_condition
GROUP BY column
HAVING aggregate_condition
ORDER BY column;
aggregate(column)is a function such as SUM or COUNT. It returns one value for each group.FROMnames the table, or the tables of a join. The join runs before GROUP BY.WHEREremoves single rows before they are grouped. It is optional.GROUP BYputs the rows with the same value ofcolumninto one group.HAVINGkeeps the groups for whichaggregate_conditionis true.ORDER BYsorts the groups that remain. It is optional.
You can leave out WHERE and ORDER BY. The clauses you keep stay in this order.
Create a sample table of six orders:
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
customer_id INT,
product VARCHAR(20),
quantity INT
);
INSERT INTO Orders VALUES
(1, 1, 'Apples', 50),
(2, 2, 'Bananas', 30),
(3, 2, 'Apples', 20),
(4, 3, 'Cherries', 70),
(5, 2, 'Bananas', 10),
(6, 1, 'Apples', 20);
Every statement in this article was run on PostgreSQL 18, MySQL 9.1 and SQLite 3.50. All three returned the rows shown, except where a table compares the engines.
HAVING
HAVING tests each group once GROUP BY has formed the groups. For example, you can keep the products that sold more than 40 units. Grouped by product, the six orders give three totals:
SELECT product, SUM(quantity) AS total_quantity
FROM Orders
GROUP BY product
ORDER BY product;
| product | total_quantity |
|---|---|
| Apples | 90 |
| Bananas | 40 |
| Cherries | 70 |
Add HAVING after GROUP BY, and only the totals over 40 remain:
SELECT product, SUM(quantity) AS total_quantity
FROM Orders
GROUP BY product
HAVING SUM(quantity) > 40
ORDER BY product;
| product | total_quantity |
|---|---|
| Apples | 90 |
| Cherries | 70 |
Apples (90) and Cherries (70) pass. The Bananas total is exactly 40, and 40 is not greater than 40, so HAVING removes Bananas. With >= 40, Bananas would stay.
HAVING vs WHERE
WHERE removes single rows before GROUP BY forms the groups, and HAVING removes whole groups after it. For example, you can drop the orders of 20 units or less, then test each product's remaining total:
SELECT product, SUM(quantity) AS total_quantity
FROM Orders
WHERE quantity > 20
GROUP BY product
HAVING SUM(quantity) > 40
ORDER BY product;
| product | total_quantity |
|---|---|
| Apples | 50 |
| Cherries | 70 |
WHERE removed the orders of 20, 10 and 20 units before the grouping. The Apples total drops from 90 to 50. Bananas keeps only its order of 30, and HAVING removes Bananas because 30 is not over 40.
HAVING also accepts a condition without an aggregate, such as HAVING product = 'Apples'. Put such a condition in WHERE instead, as the MySQL 8.4 SELECT manual asks.
Practice this: Lesson 7 of the SQL tutorial runs GROUP BY and HAVING on a shop database in your browser, with checked exercises.
HAVING with COUNT
COUNT in HAVING tests how many rows each group holds. For example, you can list the products that were ordered more than once:
SELECT product, COUNT(*) AS order_count
FROM Orders
GROUP BY product
HAVING COUNT(*) > 1
ORDER BY product;
| product | order_count |
|---|---|
| Apples | 3 |
| Bananas | 2 |
Apples has three orders and Bananas two, so both stay. Cherries has one order, and HAVING removes it. AVG, MIN and MAX work in HAVING the same way, and SQL Aggregate Functions explains each of them.
HAVING with AND
AND joins two conditions in HAVING, and a group has to pass both. For example, you can keep the products with at least two orders and more than 50 units:
SELECT product, COUNT(*) AS order_count, SUM(quantity) AS total_quantity
FROM Orders
GROUP BY product
HAVING COUNT(*) >= 2 AND SUM(quantity) > 50
ORDER BY product;
| product | order_count | total_quantity |
|---|---|---|
| Apples | 3 | 90 |
Apples passes both conditions. Bananas has two orders but only 40 units, and Cherries has 70 units but one order. OR, NOT and BETWEEN work in HAVING as they do in WHERE.
HAVING with COUNT(DISTINCT)
COUNT(DISTINCT column) counts the distinct values of a column in each group, not the group's rows. For example, you can list the products that at least two different customers bought:
SELECT product, COUNT(DISTINCT customer_id) AS customers
FROM Orders
GROUP BY product
HAVING COUNT(DISTINCT customer_id) >= 2
ORDER BY product;
| product | customers |
|---|---|
| Apples | 2 |
Customers 1 and 2 bought Apples. Bananas has two orders, but customer 2 placed both, so Bananas has one customer. Cherries has one order and one customer.
HAVING without GROUP BY
Without GROUP BY, HAVING treats all the rows of the table as one group. For example, you can return the total quantity only when it is over 100:
SELECT SUM(quantity) AS total_quantity
FROM Orders
HAVING SUM(quantity) > 100;
| total_quantity |
|---|
| 200 |
The six orders total 200, so the query returns one row. With HAVING SUM(quantity) > 1000, the query returns no rows at all, not a row with 200. SQLite accepts HAVING without GROUP BY from version 3.39.0.
What goes wrong
An aggregate in WHERE
WHERE runs before the groups exist, so it can't test SUM or COUNT. All three engines reject this query:
SELECT product, SUM(quantity)
FROM Orders
WHERE SUM(quantity) > 40
GROUP BY product;
| engine | error |
|---|---|
| MySQL | ERROR 1111 (HY000): Invalid use of group function |
| PostgreSQL | ERROR: aggregate functions are not allowed in WHERE |
| SQLite | misuse of aggregate: SUM() |
Move the condition to HAVING.
A column alias in HAVING
The engines differ on an alias that SELECT defines, such as total_quantity:
SELECT product, SUM(quantity) AS total_quantity
FROM Orders
GROUP BY product
HAVING total_quantity > 40;
| engine | result |
|---|---|
| MySQL | Apples 90 and Cherries 70 |
| SQLite | Apples 90 and Cherries 70 |
| PostgreSQL | ERROR: column "total_quantity" does not exist |
| SQL Server | rejected, per Microsoft Learn; not run here |
MySQL allows the alias as an extension to standard SQL. PostgreSQL's SELECT page allows an output column's name only in ORDER BY and GROUP BY. SQL Server runs HAVING before SELECT in its logical processing order, so HAVING can't use an alias that SELECT defines.
Write the aggregate again, HAVING SUM(quantity) > 40, and the query runs on all three engines.
A column that is not grouped
HAVING sees groups, not single rows. A column that is neither grouped nor inside an aggregate has several values in a group:
SELECT product, SUM(quantity)
FROM Orders
GROUP BY product
HAVING quantity > 20;
| engine | result |
|---|---|
| MySQL | ERROR 1054 (42S22): Unknown column 'quantity' in 'having clause' |
| PostgreSQL | ERROR: column "orders.quantity" must appear in the GROUP BY clause or be used in an aggregate function |
| SQLite | Apples 90, Bananas 40 and Cherries 70, with no error |
SQLite picks one row of each group arbitrarily and tests the condition on that row alone. Here each picked row passed, so all three groups stay. Put a condition on single rows in WHERE, or wrap the column in an aggregate, such as MIN(quantity) > 20.
Run the queries in DbSchema
The SQL Editor in DbSchema runs a query and shows its rows in a grid under the editor. Press Editor in the toolbar, choose SQL Editor, paste a query from this article, and press Run.
Download DbSchema at https://dbschema.com/download.html and connect it to PostgreSQL, MySQL or SQLite. Run the CREATE TABLE and INSERT statements in the SQL Editor with Run Script, then run each query with and without its HAVING line. The free Community Edition includes the database connection and the SQL Editor.

