SQL Tutorial

HAVING Clause in SQL

HAVING Clause in SQL
HAVING Clause in SQL

HAVING Clause in SQL

In SQL, the HAVING clause is used to apply conditions to groups created using the GROUP BY clause in a SELECT statement. While the WHERE clause filters records before grouping, the HAVING clause filters grouped data after the grouping operation.

HAVING Clause in SQL

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

Why Do We Need the HAVING Clause?

The HAVING clause is important because the WHERE clause cannot be used with aggregate functions such as SUM, COUNT, AVG, MIN, and MAX. Although both HAVING and WHERE are used for filtering, they are applied at different stages of an SQL query.

Difference Between HAVING and WHERE Clause

One of the most common SQL interview questions is the difference between the HAVING and WHERE clauses. The key difference is that WHERE filters individual records before grouping, while HAVING filters groups after the GROUP BY clause has been applied.

Comparison Table:

HAVINGWHERE
The HAVING clause filters data after the GROUP BY clause has been applied.The WHERE clause filters data before grouping.
Used with GROUP BY.Can be used without GROUP BY.
Supports aggregate functions such as SUM, COUNT, and AVG.Does not support aggregate functions.
Only works with SELECT statements.Can be used with SELECT, UPDATE, and DELETE statements.
Implemented after GROUP BY.Implemented before GROUP BY.
Works on grouped data.Works on individual records.
It is a post-filter.It is a pre-filter.
Used to filter groups of data.Used to filter individual records.

Syntax of the HAVING Clause in SQL

SELECT column1, column2, ..., aggregate_function(column)
FROM table_name
GROUP BY column1
HAVING condition;

Examples of the HAVING Clause in SQL

Let’s explore four different examples that demonstrate the use of the HAVING clause with various aggregate functions.

Example 1: Using HAVING with SUM Function

Consider the following Sales table:

Sales_IDSalesmanAmountCity
101Rohan3000Mumbai
102Aman5000Delhi
103Suresh7000Pune
104Rahul2000Mumbai
105Mohit6000Delhi

If you want to calculate the total sales amount for each city, use:

SELECT SUM(Amount), City FROM Sales GROUP BY City;

Output:

SUM(Amount)City
5000Mumbai
11000Delhi
7000Pune

Now, suppose you want to display only those cities where the total sales exceed 6000. Use the HAVING clause:

SELECT SUM(Amount), City
FROM Sales
GROUP BY City
HAVING SUM(Amount) > 6000;

Output:

SUM(Amount)City
11000Delhi
7000Pune

Example 2: Using HAVING with COUNT Function

Consider the Students table:

Roll_NoNameMarksAge
1Aryan9021
2Soham7519
3Neha8822
4Raj8021
5Simran6520
6Rahul7822
7Sneha9219
8Anuj8521

To count the number of students by age, use:

SELECT COUNT(Roll_No), Age FROM Students GROUP BY Age;

Output:

COUNT(Roll_No)Age
321
219
222
120

Now, to display only ages where at least two students exist:

SELECT COUNT(Roll_No), Age
FROM Students
GROUP BY Age
HAVING COUNT(Roll_No) >= 2;

Output:

COUNT(Roll_No)Age
321
219
222

Example 3: Using HAVING with MIN and MAX Functions

Consider the Employees table:

Emp_IDNameSalaryDepartment
201Aman8500HR
202Riya4500IT
203Karan5000IT
204Simran9200Finance
205Mohan11000Marketing

To find the minimum salary in each department:

SELECT MIN(Salary), Department
FROM Employees
GROUP BY Department;

Output:

MIN(Salary)Department
8500HR
4500IT
9200Finance
11000Marketing

Now, to display departments where the minimum salary is greater than 5000:

SELECT MIN(Salary), Department
FROM Employees
GROUP BY Department
HAVING MIN(Salary) > 5000;

Output:

MIN(Salary)Department
8500HR
9200Finance
11000Marketing

Example 4: Using HAVING with AVG Function

To find the average salary of employees in each department:

SELECT AVG(Salary), Department
FROM Employees
GROUP BY Department;

Output:

AVG(Salary)Department
8500HR
4750IT
9200Finance
11000Marketing

To filter only departments where average salary is more than 7000:

SELECT AVG(Salary), Department
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > 7000;

Output:

AVG(Salary)Department
8500HR
9200Finance
11000Marketing

Download New Real Time Projects :- Click here

Conclusion

The HAVING clause is useful for filtering grouped data in SQL. Unlike WHERE, it allows aggregate functions to be used when refining query results after grouping. Understanding the HAVING clause can help improve SQL skills and make it easier to work with grouped data.

having clause in sql example
group by clause in sql
group by and having clause in sql
having clause in sql without group by
having clause in sql w3schools
having clause in sql oracle
where clause in sql
having clause example

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us