SQL Having Clause - The Coding Shala
Home >> Learn SQL >> SQL Having Clause
The following query makes the group based on age -
Now using 'Having' Clause we can fetch only those group that have count(age) >2 -
The following query will only those groups that have Id > 4 -
Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.
SQL Having Clause
The 'Having' clause was added to SQL because 'Where' Keyword could not be used with aggregate functions. The 'Having' clause is used to place conditions on the groups created by the 'Group by' Clause.
SQL Having Clause Syntax
The basic syntax of SQL Having Clause is as follows -
SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);
SQL Having Clause Examples
The following 'emp' Table is used for the examples -
Id Name Age Address Salary 1 Akshay 22 Pune 40000 2 Mohit 21 Delhi 42000 3 Akash 21 Delhi 45000 4 Nikhil 24 Mumbai 50000 5 Smith 24 Pune 50000 6 Akshay 22 Pune 50000 7 Nikhil 24 Mumbai 43000
The following query makes the group based on age -
SQL >> select * from emp group by Age; Output >> Id Name Age Address Salary 3 Akash 21 Delhi 45000 6 Akshay 22 Pune 50000 7 Nikhil 24 Mumbai 43000
Now using 'Having' Clause we can fetch only those group that have count(age) >2 -
SQL >> select * from emp group by Age having count(Age)>2; Output >> Id Name Age Address Salary 7 Nikhil 24 Mumbai 43000
The following query will only those groups that have Id > 4 -
SQL >> select * from emp group by Age having Id > 4 Output >> Id Name Age Address Salary 6 Akshay 22 Pune 50000 7 Nikhil 24 Mumbai 43000
Other Posts You May Like
Prev<< SQL TOP/LIMIT/ROWNUM NEXT >>SQL LIKE Clause
Comments
Post a Comment