Open In App

SQL HAVING Clause

Last Updated : 14 Nov, 2025
Comments
Improve
Suggest changes
20 Likes
Like
Report

The SQL HAVING clause filters the results of grouped data after using the GROUP BY clause. It is used with aggregate functions such as SUM(), COUNT(), or AVG() to display only those groups that meet specific conditions.

Example: First, we will create a demo SQL database and table, on which we will use the HAVING Clause command.

HAVING

Query:

SELECT
Department,
COUNT(EmpID) AS Employee_Count
FROM
Employees
GROUP BY
Department
HAVING
COUNT(EmpID) > 0;

Output:

intro-image

Syntax:

SELECT AGGREGATE_FUNCTION(column_name)
FROM table_name
HAVING condition;

Examples of HAVING Clause

First, we create the Employee table and insert sample data to demonstrate the HAVING clause.

Having-1

Example 1: Filter Total Salary

In this Example we calculate the total salary of all employees and display it only if it meets the specified condition.

Query:

SELECT SUM(Salary) AS Total_Salary
FROM Employee
HAVING SUM(Salary) >= 250000;

Output:

total_sal

Example 2: Filter Average Salary

In this example, we calculate the average salary of all employees and display it only if the average exceeds 55,000.

Query:

SELECT Department, AVG(Salary) AS AverageSalary
FROM Employee
GROUP BY Department
HAVING AVG(Salary) > 55000;

Output:

2

Example 3: Filter Maximum Salary

In this example, we find the highest salary among employees and display it only if it exceeds 70,000.

Query:

SELECT MAX(Salary) AS Max_Salary
FROM Employee
GROUP BY (SELECT 1)
HAVING MAX(Salary) > 70000;

Output

Max_having

Example 4: Filter Minimum Experience

In this example, we find the least experienced employee and display it only if their experience is less than 3 years.

Query:

SELECT MIN(Experience) AS Min_Experience
FROM Employee
GROUP BY (SELECT 1)
HAVING MIN(Experience) < 3;

Output

MIN_EXP

Example 5: Multiple Conditions

In this example, we calculate both the total and average salary of employees and display the results only if the total salary is at least 250,000 and the average salary exceeds 55,000.

Query:

SELECT SUM(Salary) AS Total_Salary, AVG(Salary) AS Average_Salary
FROM Employee
GROUP BY (SELECT 1)
HAVING SUM(Salary) >= 250000 AND AVG(Salary) > 55000;

Output:

Multiple



SQL HAVING Clause with Examples
Article Tags :

Explore