SQL HAVING Vs GROUP BY Clause
The primary distinctions between the HAVING
and GROUPBY
functions will be discussed in this article.
SQL HAVING Clause
The aggregate function with the GROUP BY clause is similar to the HAVING Clause.
With aggregate functions, the HAVING clause is utilised instead of the WHERE
clause.
The GROUP BY Clause, on the other hand, groups rows with the same values into summary rows.
The WHERE
clause is combined with the having clause to discover rows that meet particular criteria. After the group By clause, the having clause is always utilised.
Example: HAVING Clause
let's have Employee table to find who get above 30000 salary
ID | Name | Gender | City | Dept | Salary |
---|---|---|---|---|---|
1 | Padmavathi | Female | Madurai | Database | 35000 |
2 | Nirmala | Female | Mumbai | Programming | 5500 |
3 | Vinoth Kumar | Male | Delhi | Web Design | 7000 |
4 | Siva Kumar | Male | Mysore | Database | 18500 |
The following sql statement to help the above query
Sql-server,Mysql,Sqlite,Ms access
SELECT SUM(ID), Name
FROM EmployeeAdvance
GROUP BY Name
HAVING SUM(Salary) >30000;
Output: The result will be,
Column | name |
---|---|
1 | Padmavathi |
SQL GROUP BY Clause
When using aggregate functions (MAX
, SUM
, AVG
), the GROUP BY clause is frequently used to group the results by one or more columns.
To put it another way, the GROUP BY clause is used in conjunction with the SELECT
statement to organise data into groups.
The GROUP BY command joins rows with the same values together.
After the where clause, this statement is utilised. To group the results by one or more columns, this expression is frequently used with an aggregate function such as SUM
, AVG
, COUNT
, and so on.
Example: Implement Group By clause
We have Employee Table to execute Group by statement:
id | name | gender | city | dept | salary |
---|---|---|---|---|---|
1 | Padmavathi | Female | Madurai | Database | 35000 |
2 | Nirmala | Female | Mumbai | Programming | 5500 |
3 | Vinoth Kumar | Male | Delhi | Web Design | 7000 |
4 | Siva Kumar | Male | Mysore | Database | 18500 |
The below query to execute the groupby name and city column in above table
Mysql, Sqlite
SELECT Name,city,COUNT(City) AS City1
FROM EmployeeAdvance
GROUP BY Name;
Output: The result will be
name | city | city1 |
---|---|---|
Nirmala | Mumbai | 1 |
Padmavathi | Madurai | 1 |
Siva Kumar | Mysore | 1 |
Vinoth Kumar | Delhi | 1 |
SQL Difference Beetween HAVING And GROUPBY CLAUSE
The main distinguish between having & groupby clauses points are tabulated:-
HAVING | GROUP BY |
---|---|
It's used to add some additional conditions to the query. | The groupby clause is used to organise data into groups based on a specific column or row. |
The SQL Aggregate functions are included in the Having clause, allowing us to add conditions to the data values. | SQL Group Because it solely deals with the grouping of values around a certain category, the by clause does not require aggregate functions. |
It uses several conditions to limit the query output. | It divides the output into groups based on certain rows or columns. |
SQL is a database management system. Without a Group, the Having clause is useless. When using a by clause in an aggregate function, it acts similarly to a where clause. |
In the absence of a Having Clause, we can surely utilise a Group By clause. That is, we don't require the usage of a group By clause in conjunction with a Having clause. |