SQL HAVING Clause

As Where clauses are not used with aggregate functions, so we use the HAVING clause to perform aggregate functions.The HAVING clause is used to restrict the results returned by Group BY Clause.

HAVING Clause Syntax :

The syntax of theHAVING Clause is −

SELECT column-name(s)
FROM table-name
WHERE condition
GROUP BY column-name(s)
HAVING condition
ORDER BY column-name(s);   



HAVING Clause Example :

Consider the Customer table with the following records -

CustomerID CustomerName Age Address CustomerSalary Gender
1 Aarav 28 Udaipur 28000 M
2 Aarushi 25 Mumbai 30000 F
3 Reyansh 28 Chennai 35000 M
4 Aditi 24 Udaipur 50000 F
5 Sai 30 Mumbai 27000 M


Test Case:- The following code is an example, which would fetch the age field from Customer table where the count of similar age is greater than or equal to 2.

SELECT Age
FROM Customer
GROUP BY Age
HAVING COUNT(Age) >= 2;   


The result for the respective sql query is as follows −

Age
28





Visit :


Discussion



* You must be logged in to add comment.