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(*) >= 2

Related terms

Taught in

References