ENQUIRE HOME

Practice SQL Queries On Having Clause

Having Cluse In SQL Queries

Practice SQL Queries On Having Clause

Practice SQL Queries On Having Clause

Having Cluse In SQL Queries , Oct-10-2026

Practice Question 1: Filtering by Average Salary

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.

Solution

SELECT Department, AVG(Salary) AS AvgSalary
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

  1. COUNT(*): Counts how many employee rows belong to each department group.

  2. 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

  1. WHERE Experience > 1: This row-level filter runs first, removing any employee with 1 year or less of experience before any grouping happens.

  2. GROUP BY Department: Groups the remaining filtered employees by department.

  3. 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.


Popular

  • 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

Shopeze.in

Shopeze.in

Aaja.in

Boost via Reels & Images

Learn Python

Learn Python By Gagan Khanna

Quick...

Professional Help for Growth

Tags

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


Topics You may be intrested in


























































































































































































































Teachlive is a platform dedicated to both teachers and students, providing free resources to help spread knowledge and benefit students in their educational journey. Under guidence of Seema Khanna ,at Teachlive, we offer much more than just academic support; we help students develop critical life skills, offer parental guidance, and keep everyone informed about the latest changes in the Indian education system.


Please Take a while to visit our sponsors website Shopeze.in - Home Decoration Items