HAVING

SQL HAVING Keyword

The HAVING clause is used to filter groups of records after they have been grouped using GROUP BY. It is typically used with aggregate functions like COUNT, SUM, AVG, MAX, or MIN. Unlike WHERE, which filters rows before grouping, HAVING filters after grouping.

Tutorials dojo strip

SQL HAVING Syntax

SQL

SQL HAVING with GROUP BY Example

The following SQL lists how many patients exist for each gender in the Patients table, and only includes genders that have more than 2 patients:

SQL

SQL HAVING with ORDER BY

The next example also sorts the results in descending order of patient count:

SQL

SQL HAVING Labs

Tutorials dojo strip