Having clause in MYSQL by R4R Team

MYSQL HAVING clause:
-MySQL HAVING Clause is used with GROUP BY clause.
-It always returns the rows where condition is TRUE.

Syntax:
SELECT columns FROM table_name
GROUP BY columns
HAVING condition;

SELECT * FROM employee;

emp_id emp_name department emp_salary
1 Aman D1 34000
2 Ram D2 43000
3 Ramesh D1 20000
4 John D3 65000
5 Sita D1 10000
6 Dinesh D2 15000

Now fetch the department in which number of employee is equal to 1:

SELECT department,COUNT(*) FROM employee GROUP BY department HAVING COUNT(DEPARTMENT)=2;

department COUNT(*)
D2 2



Leave a Comment: