
The principal distinction between the two clauses is the stage at which they filter data in a query:
SELECT column_Name1, column_Name2, ..., aggregate_function(column_Name) FROM table_name [WHERE row_condition] GROUP BY column_Name1, column_Name2, ... HAVING group_condition;
Notes:
Below are worked examples that demonstrate the use of HAVING with different aggregate functions.
Context: Consider an Employee table. The following shows how to find total salary by city and then filter cities whose total salary exceeds a threshold.

Query to compute total salary per city:
SELECT SUM(Emp_Salary) AS total_salary, Emp_City FROM Employee GROUP BY Emp_City;
Result:

To show only those cities whose total salary of employees is more than 5000:
SELECT SUM(Emp_Salary) AS total_salary, Emp_City FROM Employee GROUP BY Emp_City HAVING SUM(Emp_Salary) > 5000;
Filtered result:

Context: Consider a Student_details table. We count students by age and filter ages having at least a specified number of students.

Query to count students by age:
SELECT COUNT(Roll_No) AS student_count, Age FROM Student_details GROUP BY Age;
Result:

To show only ages where the count of students is greater than or equal to 2:
SELECT COUNT(Roll_No) AS student_count, Age FROM Student_details GROUP BY Age HAVING COUNT(Roll_No) >= 2;
Filtered result:

Context: Using the Employee table again, we demonstrate MIN and MAX with HAVING.

Query to show minimum salary by department:
SELECT MIN(Emp_Salary) AS min_salary, Emp_Dept FROM Employee GROUP BY Emp_Dept;
Result:

To show only those departments whose minimum salary is greater than 4000:
SELECT MIN(Emp_Salary) AS min_salary, Emp_Dept FROM Employee GROUP BY Emp_Dept HAVING MIN(Emp_Salary) > 4000;
Filtered result:

Query to show maximum salary by department:
SELECT MAX(Emp_Salary) AS max_salary, Emp_Dept FROM Employee GROUP BY Emp_Dept;
Result:

To show only those departments whose maximum salary is less than 8000:
SELECT MAX(Emp_Salary) AS max_salary, Emp_Dept FROM Employee GROUP BY Emp_Dept HAVING MAX(Emp_Salary) < 8000;
Filtered result:

Context: Consider an Employee_Dept table. We compute average salary per department and filter departments by average salary.

Query to compute average salary by department:
SELECT AVG(Emp_Salary) AS avg_salary, Emp_Dept FROM Employee_Dept GROUP BY Emp_Dept;
Result:

To show only those departments whose average salary is greater than or equal to 6500:
SELECT AVG(Emp_Salary) AS avg_salary, Emp_Dept FROM Employee_Dept GROUP BY Emp_Dept HAVING AVG(Emp_Salary) >= 6500;
Filtered result:

SELECT SUM(Emp_Salary) AS total_salary FROM Employee HAVING SUM(Emp_Salary) > 100000;
The WHERE clause filters rows before grouping and cannot use aggregate functions; the HAVING clause filters groups after aggregation and can reference aggregate functions. Use WHERE to reduce rows early for performance and HAVING to apply conditions to aggregated results.
![]() | Explore Courses for Computer Science Engineering (CSE) exam |
![]() | Get EduRev Notes directly in your Google search |