Having Cluse In SQL Queries

Problem Statement:
Write a SQL query to find departments where the average salary is strictly greater than 55,000. Display the department name and its average salary.
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > 55000;
Explanation
GROUP BY Department: Groups the rows in the table by each unique department (IT, HR, Sales, Finance, Marketing).
AVG(Salary): Calculates the average salary for each of those department groups.
HAVING AVG(Salary) > 55000: Filters the grouped results, keeping only the departments where the calculated average meets the condition. (WHERE cannot be used here because WHERE filters individual rows before grouping takes place).
Practice Question 2: Finding Large Teams (Counting Grouped Rows)
Problem Statement:
Write a SQL query to find departments that have more than 3 employees. Display the department name and the total employee count.
Solution
SELECT Department, COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY Department
HAVING COUNT(*) > 3;
Explanation
COUNT(*): Counts how many employee rows belong to each department group.
HAVING COUNT(*) > 3: Ensures that only departments with a headcount greater than 3 are returned in the final output. For instance, if HR has 3 employees and IT has 4, HR will be filtered out.
Practice Question 3: Combining WHERE and HAVING
Problem Statement:
Write a SQL query to find departments where the average salary of employees who have more than 1 year of experience is greater than 60,000. Display the department and that filtered average salary.
Solution
SELECT Department, AVG(Salary) AS FilteredAvgSalary
FROM Employees
WHERE Experience > 1
GROUP BY Department
HAVING AVG(Salary) > 60000;
Explanation
WHERE Experience > 1: This row-level filter runs first, removing any employee with 1 year or less of experience before any grouping happens.
GROUP BY Department: Groups the remaining filtered employees by department.
HAVING AVG(Salary) > 60000: Filters the groups to only include departments where the average salary of the experienced staff exceeds 60,000.
We use the HAVING clause because the WHERE clause cannot filter aggregate functions (like SUM, AVG, COUNT, MAX, or MIN). When you group data together, you often need to set a condition on the summary result of those groups rather than on the individual rows. That is exactly what HAVING is designed for. Order of Execution in SQL Understanding the sequence helps clarify why both clauses coexist: FROM: Gathers the raw table data. WHERE: Filters individual rows (e.g., WHERE Experience > 1). GROUP BY: Packs the remaining rows into groups. HAVING: Filters the groups based on aggregate results (e.g., HAVING COUNT(*) > 3). SELECT: Chooses what columns to display. ORDER BY: Sorts the final output.
Shopeze.in
Boost via Reels & Images
Learn Python By Gagan Khanna
Professional Help for Growth
Difference Between Where And Having Clause In SQL Queries What Is Group By Clause In SQL Query Where Should We Use Having Clause In SQL What Are Aggregate Functions In SQL