TGViewer
Data Analytics Data Analytics @sqlspecialist · 111K subscribers
Post #2727 6.79K
Now, let’s move to the next topic of SQL Roadmap:

✅ HAVING Clause

🧠 1. What is HAVING?

HAVING is used to filter grouped data

👉 Think like this:
- WHERE → filters rows (before grouping)
- HAVING → filters groups (after grouping)

⚡ 2. WHERE vs HAVING

Works on
- WHERE: Rows
- HAVING: Groups

Used with
- WHERE: SELECT
- HAVING: GROUP BY

Can use aggregates?
- WHERE: ❌ No
- HAVING: ✅ Yes

💡 3. Basic Syntax

SELECT column, AGG_FUNCTION(column)
FROM table_name
GROUP BY column
HAVING condition;

🎯 4. Examples (Very Important)

👉 Example 1: Departments with more than 2 employees

SELECT department, COUNT() AS total_emp
FROM employees
GROUP BY department
HAVING COUNT() > 2;

👉 Example 2: Departments with average salary > 50k

SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;

⚡ 5. WHERE + HAVING Together

SELECT department, COUNT() AS total_emp
FROM employees
WHERE salary > 30000
GROUP BY department
HAVING COUNT() > 2;

👉 Step-by-step:
1. WHERE → filters employees with salary > 30k
2. GROUP BY → groups by department
3. HAVING → keeps only groups with > 2 employees

🎯 6. Practice Tasks
1. Find departments having more than 3 employees
2. Find departments with average salary > 60k
3. Count employees per department where salary > 40k
4. Show departments with total salary > 1,00,000
5. Find departments with minimum salary > 30k

⚡ Mini Challenge 🔥

👉 Find departments having more than 2 employees AND avg salary > 50k

Double Tap ❤️ For Practice Task Solution
  • ❤ 19
More from @sqlspecialist
  1. Oct 9, 2026“Here, the data is sorted by the second column in descending order and the first five rows…
  2. Oct 9, 2026📊 Data Analyst Interview Series — Part 6 Guys, let's continue our Data Analyst Interview…
  3. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
  4. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  5. Oct 7, 2026📊 Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vid…
  6. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions such…
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 →