SQL HAVING Clause (original) (raw)

Last Updated : 9 Feb, 2026

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.

Screenshot-2026-02-09-122317

**Query:

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

**Output:

Screenshot-2026-02-09-122407

**Syntax:

SELECT column_name, AGGREGATE_FUNCTION(column_name) FROM table_name GROUP BY column_name HAVING condition;

**Note: When HAVING is used without GROUP BY, the entire table is treated as a single group. This works only with aggregate functions.

Examples of HAVING Clause

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

Having-1

Employee Table

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 GROUP BY (SELECT 1) HAVING SUM(Salary) >= 250000;

**Output:

total_sal

**Note: GROUP BY (SELECT 1) is database-specific and may not work in all SQL databases. So alternatively, you can omit GROUP BY and use HAVING directly with aggregate functions.

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:

Screenshot-2026-02-09-120826

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 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 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 HAVING SUM(Salary) >= 250000 AND AVG(Salary) > 55000;

**Output:

Screenshot-2026-02-09-121829