HAVING clause
The HAVING clause filters the groups that GROUP BY forms, after the aggregates are computed. Its condition can use aggregate functions, grouping expressions, and the aliases of the select list.
Syntax
HAVING condition
Examples
For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.
The following SQL query returns the regions whose total amount is more than 100.
SELECT region, sum(amount) AS revenue FROM sales_db.sales_parquet GROUP BY region HAVING sum(amount) > 100 ORDER BY region
The query returns the following results.
| region | revenue |
|---|---|
|
|
|
|
The following SQL query filters on orders, the alias of an aggregate in the select list.
SELECT region, count(*) AS orders FROM sales_db.sales_parquet GROUP BY region HAVING orders > 1 ORDER BY region
The query returns the following results.
| region | orders |
|---|---|
|
|
|
|