MySQL Tutorial
The HAVING Clause enables you to specify conditions that filter which group results appear in the results.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Let's put these statements into real use.
We've a table named Students in our database that contains the following records:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
2 | SAM | Python |
3 | Rani | Node.js | 4 | Rim | Java |
5 | Micheal | JAVA |
6 | Lara | PHP |
The following MySQL statement lists the number of Students in each Subject. Only include Subjects with more than 2 Students:
SELECT COUNT(ROLL_No), SUBJECT
FROM Students
GROUP BY SUBJECT
HAVING COUNT(ROLL_No) > 1;
Now, after executing the above MySQL statement,
COUNT(ROLL_No) | SUBJECT |
---|---|
2 | JAVA |