Skip to content

HAVING

The HAVING clause is used for filtering with GROUP BY. HAVING applies the filter after generating the groups, whereas WHERE applies the filter before generating any groups:

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition

For example:

SELECT MAX(A) FROM table1 GROUP BY B HAVING C < 0
HAVING statements also referring to columns by aliases used in the GROUP BY:
SELECT MAX(A), B - 1 as val FROM table1 GROUP BY val HAVING val 5