HAVING Clause in SQL

HAVING Clause in SQL

HAVING Clause in SQL

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

HAVING Clause in SQL

In SQL, the HAVING clause is used to place conditions on groups created by the GROUP BY clause in a SELECT statement. Unlike the WHERE clause, which filters records before grouping, the HAVING clause filters records after grouping.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

Why Do We Need the HAVING Clause?

The HAVING clause is essential because WHERE cannot be used with aggregate functions like SUM, COUNT, AVG, MIN, and MAX. Both HAVING and WHERE are used for filtering, but they serve different purposes in SQL queries.

Difference Between HAVING and WHERE Clause

One of the most frequently asked interview questions in SQL is the difference between HAVING and WHERE clauses. The main difference is that WHERE filters data before grouping, whereas HAVING filters data after grouping.

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 (SUM, COUNT, AVG, etc.).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 (column operations).Works on individual records (row operations).
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.

See also  Understanding SQL SELECT UNIQUE and SELECT DISTINCT

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:

See also  SQL OUTER JOIN: A Comprehensive Guide

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
Complete Advance AI topics:- CLICK HERE

Conclusion

The HAVING clause is crucial for filtering grouped data in SQL. Unlike WHERE, it allows the use of aggregate functions to refine query results after grouping. Mastering HAVING will significantly improve your SQL skills and data manipulation capabilities.


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

🎓 Need Complete Final Year Project?

Get Source Code + Report + PPT + Viva Questions (Instant Access)

See also  SQL TRUNCATE TABLE: A Comprehensive Guide

🛒 Visit UpdateGadh Store →
💬 Chat Now