Filtering Groups with HAVING

The HAVING clause sets conditions on the GROUP BY clause similar to the way that WHERE interacts with SELECT. The HAVING clause’s important characteristics are:

  • The HAVING clause comes after the GROUP BY clause and before the ORDER BY clause.

  • Just as WHERE limits the number of rows displayed by SELECT, HAVING limits the number of groups displayed by GROUP BY.

  • The WHERE search condition is applied before grouping occurs, and the HAVING search condition is applied after.

  • HAVING syntax is similar to the WHERE syntax, except that HAVING can contain aggregate functions.

  • A HAVING clause can reference any of the items that appear in the SELECT list.

The sequence in which the WHERE, GROUP BY, and HAVING clauses are applied is: ...

Get SQL: Visual QuickStart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.