TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.8K subscribers
Post #4668 483
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“ 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;
  • โค 1
More from @datasciencefun
  1. Oct 17, 2026Aggregate functions such as COUNT() generally cannot be used in WHERE. โœ… Correct: SELECT dโ€ฆ
  2. Oct 17, 2026๐—œ๐—•๐—  ๐—™๐—ฅ๐—˜๐—˜ ๐—ง๐—˜๐—–๐—› ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! ๐ŸŽ“ Explore IBM Cognitive Class and buildโ€ฆ
  3. Oct 16, 2026๐——๐—ฎ๐˜๐—ฎ ๐˜€๐—ฐ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ช๐—ถ๐˜๐—ต ๐—”๐—œ๐Ÿ˜ Future Ready Data Scienโ€ฆ
  4. Oct 15, 2026Post #4665
  5. Oct 15, 2026Post #4664
  6. Oct 15, 2026Post #4663
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’