HAVING
The clause that filters groups after aggregates have been calculated, such as keeping customers with at least two orders.
In more detail
WHERE filters rows before grouping; HAVING filters groups after. A condition on an aggregate, such as COUNT(*) >= 2, can only go in HAVING, while a row condition such as status = 'paid' belongs in WHERE, where it also changes what gets counted.
Example
GROUP BY customer_id HAVING COUNT(*) >= 2Related terms
- WHERE clause — The part of a query that keeps only the rows for which a condition is true.
- GROUP BY — The clause that splits rows into groups sharing the same values, so aggregates return one result per group.
- aggregate function — A function that turns many input rows into one value, such as COUNT, SUM, AVG, MIN or MAX.
Taught in
- Cross Joins And Cartesian Bugs — PostgreSQL, Indexing & Relational Data Modeling
- The Difference Between WHERE And HAVING — PostgreSQL, Indexing & Relational Data Modeling