Having Clause:
The following is the position of the HAVING clause in a query:
The SQL HAVING Clause is used in combination with the GROUP BY Clause to restrict the groups of returned rows to only those whose the condition is TRUE.
The following is the position of the HAVING clause in a query:
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
The HAVING clause must follow the GROUP BY clause in a query and must also precede the ORDER BY clause if used. The following is the syntax of the SELECT statement, including the HAVING clause:
SELECT column1, column2 FROM table1, table2
WHERE [ conditions ] GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
Examples for Having clause | Having clause examples:
Remember HAVING clause is used to filter groups and WHERE clause is used to filter rows. You cannot use WHERE clause to filter groups.
select deptno,sum(sal) from emp group by deptno
having sum(sal) >= 5000;
We want to see those departments and the number of employees working in them where the number of employees is more than 2.
Select deptno, count(*) from emp group by deptno
Having count(*) >=2;