To specify a condition with group by clause use SQL Having Clause. The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.Having clause is used only when Group By is used.
Syntax
Select <column_name>,group <function1>,group <function2> from <table_name> group by column name having <condition>;
Table_name => Any accurate table.
condition => Condition is a logic to get a specific result.
Examples
By viewing the below example, the concept of having clause can be easily understood.
[c]
sql> select * from employee;
+--------+-------+-------+--------+
| emp_id | ename | salary| deptno |
+--------+-------+-------+--------+
| 1001 | mike | 12000 | 10 |
| 1002 | rambo | 13000 | 20 |
| 1003 | kate | 14000 | 10 |
| 1003 | jeo | 14000 | 20 |
| 1003 | finn | 14000 | 30 |
+--------+-------+-------+--------+
5 rows in set (0.00 sec)
sql> select deptno,max(salary) from employee group by deptno having max(salary)>13000;
+--------+-----------+
| deptno |max(salary)|
+--------+-----------+
| 10 | 14000 |
| 20 | 14000 |
| 30 | 14000 |
+--------+-----------+
3 rows in set (0.00 sec)[/c]
The above example tells that, when having clause operation is performed on a column name like deptno and salary having salary greater that 13000, then it automatically displays the details of employees based on employee maximum salary.
Summary
Key Points
SQL Having Clause - To specify a condition with group by clause use having clause.