Memra

HAVING: filtering groups

Keep only the groups that pass a test.

Filter the groups

WHERE filters rows before grouping; HAVING filters the groups after aggregation. "Genres with more than two books":

SELECT genre, COUNT(*) AS books
FROM books
GROUP BY genre
HAVING COUNT(*) > 2;

Think of it as WHERE for aggregates. You can use both in one query: WHERE narrows the rows, HAVING narrows the resulting groups.

NORMAL ~/memra/learn/postgresql/having utf-8 LF