Bird
0
0

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:
  1. Step 1: Understand HAVING clause usage

    HAVING filters groups after aggregation, so it must come after GROUP BY.
  2. 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.
  3. Final Answer:

    SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5; -> Option D
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes