HAVING Clause
HAVING Clause is used to filter the records from the groups based on the given condition in the HAVING Clause. Those groups who will satisfy the given condition will appear in the final result. It is applied after the grouping and aggregation of data.
Let us consider Student table mentioned above and apply having clause on it:
SELECT Age, COUNT(Roll_No) AS No_of_Students
FROM Student GROUP BY Age
HAVING COUNT(Roll_No) > 1
OUTPUT:
Age |
No_of_Students |
---|---|
17 |
3 |
20 |
2 |
21 |
2 |
Difference between Where and Having Clause in SQL
WHERE and HAVING clauses play an important role in refining the result set of a query. While both clauses are used for filtering data, they serve distinct purposes and are applied at different stages of query execution. Let’s explore the details of each and their differences with examples.