Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
HAVING Clause in SQL

HAVING Clause in SQL

Posted on February 7, 2025February 7, 2025 By Rishabh saini No Comments on HAVING Clause in SQL

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.

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
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

Post Views: 567
SQL Tutorial Tags:clause, clauses in sql, difference between having and where clause in sql with example, group by and having clause in sql, group by clause in sql, having clause, having clause in mysql, having clause in sql, having clause in sql example, having clause in sql server, having clause statement in sql, how to use having clause, sql having clause, what is having clause in sql?, where clause in sql, where vs having clause

Post navigation

Previous Post: How to Round Numbers in Python
Next Post: Sports Club Management System in Django

More Related Articles

SQL UPDATE with JOIN SQL UPDATE with JOIN SQL Tutorial
SQL SELECT AS SQL SELECT AS – Column and Table Aliases in SQL SQL Tutorial
SQL UPDATE DATE SQL UPDATE DATE: How to Update a Date and Time Field in SQL? SQL Tutorial

Leave a Reply Cancel reply

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

You may also like

  1. Understanding the SQL DELETE Statement and Its Variants
  2. SQL Temporary Tables: A Handy Tool for Developers
  3. SQL SELECT TOP Clause: A Comprehensive Guide
  4. SQL UPDATE with JOIN
  5. Understanding SQL CROSS JOIN with Examples
  6. IN vs EXISTS in SQL: A Complete Guide

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,612)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,210)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,862)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme