MySQL HAVING Clause

HAVING is used to filter on aggregations in our GROUP BY.HAVING is done after the grouping is done. After group by clause, WHERE clause is not allowed.
Generally if we want to restrict rows in a table, then we are using WHERE clause. Where as if we want to restrict groups after GROUP BY then we must use HAVING clause.

Examples:

Display those departments having sum of all employees salary is more than 9000.

mysql> SELECT DEPTNO, SUM(SAL) FROM EMP GROUP BY DEPTNO HAVING SUM(SAL)>9000; +--------+----------+ | DEPTNO | SUM(SAL) | +--------+----------+ | 20 | 10875.00 | | 30 | 9400.00 | +--------+----------+ 2 rows in set (0.21 sec)