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.
Query:
SELECT
Department,
COUNT(EmpID) AS Employee_Count
FROM
Employees
GROUP BY
Department
HAVING
COUNT(EmpID) > 0;
Output:
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.
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:
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:
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
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
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:
SQL HAVING Clause with Examples
Explore
Basics
Queries & Operations
SQL Joins & Functions
Data Constraints & Aggregate Functions
Advanced SQL Topics
Database Design & Security