The HAVING clause is used to specify filtering condition for group of rows (or aggregates). It often comes with the GROUP BY clause as a filter condition to those columns that appear in the GROUP BY clause. If the GROUP BY clause is omitted, the HAVING clause behaves as a replacement for the WHERE clause, with the difference that the WHERE clause applies conditions to each individual row, while the HAVING clause to each group of rows.

The simple syntax, applied to Emp table, looks like this:

Syntax

SELECT employee FROM Emp HAVING salary = MAX(salary);

This example will return, in combination with the MAX function, the highest paid employee from the Emp table. In this case the HAVING clause behaves like the WHERE clause. But if we add the GROUP BY clause, then the real purpose comes to the surface:

SELECT employee, salary, rank FROM Emp GROUP BY rank, salary HAVING salary>5000;

The example above will return all employees with salary above 50000, sorted by rank first, and salary second.