The HAVING clause places conditions on groups, in contrast to the WHERE clause which places conditions on rows.
HAVING can be clubbed with aggregate functions like SUM, AVG and COUNT.
HAVING is evaluated after GROUP BY has formed the groups.
WHERE vs HAVING
WHERE filters individual rows before grouping, and cannot use aggregate functions.
HAVING filters the groups after grouping, and can use aggregate functions.
Both clauses can appear in the same query: WHERE first reduces the rows, HAVING then reduces the groups.
Example:-
-- Grades whose average salary is above 50000
SELECT Grade, AVG(Sal), SUM(Sal)
FROM Employee
GROUP BY Grade
HAVING AVG(Sal) > 50000;
-- Departments having more than 5 employees
SELECT DEPTNO, COUNT(*)
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*) > 5;