HAVING Clause is used to filter the record based on group rows. Having Clause always used with Group By clause.
Syntax:-
SELECT column(s)
From table_name
WHERE [condition]
GROUP BY column(s)
HAVING [condition]
Example:-
Example:-Suppose you have an employees table that have 7 records.
+----+------------+-----------+----------------+-----------+
| id | first_name | last_name | email | country |
+----+------------+-----------+----------------+-----------+
| 1 | John | Tailor | john@abc.com | USA |
| 2 | Rom | Tailor | rom@abc.com | USA |
| 3 | Andrew | Symonds | andrew@abc.com | Australia |
| 4 | Miacle | clerk | miacle@abc.com | Australia |
| 5 | Sachin | Tendulkar | sachin@abc.com | India |
| 6 | Virat | Kohli | virat@abc.com | India |
| 7 | rohit | NULL | rohit@abc.com | India |
+----+------------+-----------+----------------+-----------+
Now you want to get the number of employees more than 2 belongs to each country.
SELECT COUNT(id), country
FROM employees
GROUP BY country HAVING COUNT(id)>2
Output:-
+-----------+---------+
| COUNT(id) | country |
+-----------+---------+
| 3 | India |
+-----------+---------+
1 row in set (0.00 sec)