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.