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 CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Difference between order by and group by clause in SQL
- Difference between Where and Having Clause in SQL
- Group by clause in MS SQL Server
- SQL query using COUNT and HAVING clause
- Having vs Where Clause in SQL
- Having clause in MS SQL Server
- Difference between From and Where Clause in SQL
- Difference between Where and Group By
- Difference between Security Group and Network ACL in AWS
- Combining aggregate and non-aggregate values in SQL using Joins and Over clause
- SQL | Distinct Clause
- SQL | WHERE Clause
- SQL | SELECT TOP Clause
- SQL | Union Clause
- SQL | WITH clause
- SQL | Except Clause
- SQL | OFFSET-FETCH Clause
- SQL | LIMIT Clause
- PHP | MySQL ORDER BY Clause
- PHP | MySQL WHERE Clause
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.