Sql - HAVING
Code Life
By Pierre Marsaa
Introduction to HAVING Clause
The HAVING clause is used to filter groups in SQL. While the WHERE clause filters rows before they are grouped, the HAVING clause filters groups after the GROUP BY clause has been applied.
The syntax for using HAVING is:
Loading editor...
Let's see the HAVING clause in action. Suppose we want to find Codémon types that have an average power greater than 75:
Loading data...
↗️
Loading editor...
↶
↷
This query first groups the Codémons by their type, then calculates the average power for each group. The HAVING clause then filters out any groups where the average power is not greater than 75.
HAVING vs WHERE
Remember the key difference: WHERE filters individual rows before grouping, while HAVING filters groups after they've been created by GROUP BY. You can only use aggregate functions like COUNT(), AVG(), SUM(), etc. in the HAVING clause, not in the WHERE clause.
Verify