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 -
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 −