๐ Phase 3: SQL for Data Science
๐ Topic 7 โ HAVING
HAVING is used to filter grouped results.
The most important thing to remember:
WHERE โ filters rows
HAVING โ filters groups
๐น Why Do We Need HAVING?
Suppose you want to find departments that have more than 5 employees.
First, you need to group employees by department:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
But if you want only departments with more than 5 employees, use HAVING:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
The grouping happens first, and then HAVING filters the groups.
๐น Basic Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name
HAVING condition;
๐น HAVING with COUNT()
Find departments having more than 10 employees:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;
๐น HAVING with SUM()
Find regions where total sales exceed 100000:
SELECT region, SUM(sales) AS total_sales
FROM sales
GROUP BY region
HAVING SUM(sales) > 100000;
๐น HAVING with AVG()
Find departments where the average salary is greater than 70000:
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 70000;
๐น HAVING with MAX()
Find departments where the highest salary is greater than 150000:
SELECT department, MAX(salary) AS highest_salary
FROM employees
GROUP BY department
HAVING MAX(salary) > 150000;
๐น WHERE vs HAVING
This is one of the most frequently asked SQL interview questions.
WHERE Filters individual rows.
SELECT *
FROM employees
WHERE salary > 50000;
It removes individual records that don't meet the condition.
HAVING
Filters groups after GROUP BY.
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 70000;
It removes entire groups that don't meet the condition.
๐น Using WHERE and HAVING Together
You can use both.
Suppose you want departments where:
โข Only employees earning more than 50000 are considered
โข The department must have at least 5 such employees
SELECT department, COUNT(*) AS employee_count
FROM employees
WHERE salary > 50000
GROUP BY department
HAVING COUNT(*) >= 5;
Conceptually:
All employees
โ
WHERE
โ
Employees with salary > 50000
โ
GROUP BY
โ
Create department groups
โ
HAVING
โ
Keep groups with at least 5 employees
๐น HAVING with ORDER BY
You can also sort the filtered groups.
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department
HAVING SUM(salary) > 500000
ORDER BY total_salary DESC;
This finds departments whose total salary exceeds 500000 and displays the highest totals first.
๐น HAVING Without GROUP BY
Some SQL databases allow HAVING without an explicit GROUP BY, treating the entire result as a single group.
For example:
SELECT COUNT(*) AS employee_count
FROM employees
HAVING COUNT(*) > 100;
This returns the result only if there are more than 100 employees.
For beginners, however, focus primarily on the common pattern:
GROUP BY โ HAVING
๐น Common Mistake
โ Incorrect:
SELECT department, COUNT(*)
FROM employees
WHERE COUNT(*) > 5
GROUP BY department;