1. Having Clause :
Having Clause is basically like the aggregate function with the GROUP BY clause. The HAVING clause is used instead of WHERE with aggregate functions. While the GROUP BY Clause groups rows that have the same values into summary rows. The having clause is used with the where clause in order to find rows with certain conditions. The having clause is always used after the group By clause.
SELECT COUNT (SALARIES) AS COUNT_SALARIES, EMPLOYEES FROM EMPLOYEES GROUP BY SALARIES HAVING COUNT(SALARIES) > 1;
2. Group By Clause :
The GROUP BY clause is often used with aggregate functions (MAX, SUM, AVG) to group the results by one or more columns or In simple words we can say that The GROUP BY clause is used in collaboration with the SELECT statement to arrange required data into groups.
The GROUP BY statement groups rows that have the same values. This Statement is used after the where clause. This statement is often used with some aggregate function like SUM, AVG, COUNT atc. to group the results by one or more columns.
SELECT COUNT (SALARIES) AS COUNT_SALARIES, EMPLOYEES FROM EMPLOYEES GROUP BY SALARIES;
Difference between Having clause and Group by clause :
|S.No.||Having Clause||GroupBy Clause|
|1.||It is used for applying some extra condition to the query.||The groupby clause is used to group the data according to particular column or row.|
|2.||Having cannot be used without groupby clause.||groupby can be used without having clause with the select statement.|
|3.||The having clause can contain aggregate functions.||It cannot contain aggregate functions.|
|4.||It restrict the query output by using some conditions||It groups the output on basis of some rows or columns.|
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between order by and group by clause in SQL
- Difference between From and Where Clause in SQL
- Difference between Where and Having Clause in SQL
- SQL | WHERE Clause
- SQL | Except Clause
- SQL | USING Clause
- Having vs Where Clause in SQL
- SQL | ON Clause
- SQL | WITH clause
- SQL | Union Clause
- SQL | Intersect & Except clause
- SQL | With Ties Clause
- SQL | LIMIT Clause
- PHP | MySQL WHERE Clause
- SQL | SELECT TOP Clause
- SQL | Distinct Clause
- Having clause in MS SQL Server
- SQL | Sub queries in From Clause
- Where clause in MS SQL Server
- Update clause in Cassandra
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.