HAVING clause
The following first gives the syntax of the HAVING clause:
SELECT Column1, SUM (Column2)
From "List-of-tables"
GROUP by "Column-list"
having "condition";
This HAVING clause allows you to specify criteria for each group, in other words, you can select rows based on the criteria you specify. If you want to use the HAVING clause, it should be followed by the GROUP BY clause.
Here is an example to explain the HAVING clause. Suppose that our employee table contains the employees ' name, Departmen, salary, and age. If you want to choose an average wage for each employee in each department, you can use the following SQL statement:
SELECT Dept, AVG (Salary)
From employee
GROUP by dept;
Of course, you can add a HAVING clause if you want to calculate and display only the average wage of salary greater than 20000:
SELECT Dept, AVG (Salary)
From employee
GROUP by Dept
Having avg (salary) > 20000;