You want to find departments with more than 5 employees. Which query correctly uses GROUP BY and HAVING to achieve this?
hard📝 Application Q15 of 15
SQL - GROUP BY and HAVING
You want to find departments with more than 5 employees. Which query correctly uses GROUP BY and HAVING to achieve this?
ASELECT department, COUNT(*) FROM employees GROUP BY department WHERE COUNT(*) > 5;
BSELECT department, COUNT(*) FROM employees HAVING COUNT(*) > 5 GROUP BY department;
CSELECT department, COUNT(*) FROM employees WHERE COUNT(*) > 5 GROUP BY department;
DSELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5;
Step-by-Step Solution
Solution:
Step 1: Understand HAVING clause usage
HAVING filters groups after aggregation, so it must come after GROUP BY.
Step 2: Check query order and syntax
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5; correctly places HAVING after GROUP BY with condition COUNT(*) > 5. Other options misuse HAVING or WHERE clauses.
Final Answer:
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5; -> Option D
Quick Check:
HAVING filters groups after GROUP BY [OK]
Quick Trick:Use HAVING after GROUP BY to filter groups [OK]
Common Mistakes:
MISTAKES
Using WHERE to filter aggregated results
Placing HAVING before GROUP BY
Confusing WHERE and HAVING clauses
Master "GROUP BY and HAVING" in SQL
9 interactive learning modes - each teaches the same concept differently