Having Clause
Having Clause is used with “group by” and it comes before “order by”.
Where clause cannot work with aggregate function. For that in SQL, you use group by with having clause. Group by clause select columns and in having clause you give condition. If the condition is true query will give desired output.
Syntax:
SELECT column1,column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
Below is the sample input:
Table name: emp
Examples:
1)select empname,count(empid)
from emp
where depid=10
group by empname
having count(empid)>0;
output:
2)select count(empid), age
from emp
where age>25
group by age
having count(empid) > 0
order by age desc;
output: