Bird
0
0

You want to find the average salary per job title but only for job titles with more than 5 employees. Which query correctly uses grouping and filtering?

hard📝 Application Q15 of 15
SQL - GROUP BY and HAVING
You want to find the average salary per job title but only for job titles with more than 5 employees. Which query correctly uses grouping and filtering?
ASELECT job_title, AVG(salary) FROM employees GROUP BY job_title WHERE COUNT(*) > 5;
BSELECT job_title, AVG(salary) FROM employees WHERE COUNT(*) > 5 GROUP BY job_title;
CSELECT job_title, AVG(salary) FROM employees GROUP BY job_title HAVING COUNT(*) > 5;
DSELECT job_title, AVG(salary) FROM employees HAVING COUNT(*) > 5 GROUP BY job_title;
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering groups with HAVING

    To filter groups after aggregation, use HAVING, not WHERE.
  2. Step 2: Check query order and clauses

    SELECT job_title, AVG(salary) FROM employees GROUP BY job_title HAVING COUNT(*) > 5; correctly groups by job_title and filters groups with more than 5 employees using HAVING.
  3. Final Answer:

    SELECT job_title, AVG(salary) FROM employees GROUP BY job_title HAVING COUNT(*) > 5; -> Option C
  4. Quick Check:

    Filter groups with HAVING after GROUP BY [OK]
Quick Trick: Use HAVING to filter groups, not WHERE [OK]
Common Mistakes:
MISTAKES
  • Using WHERE to filter aggregated groups
  • Placing HAVING before GROUP BY
  • Confusing clause order in SQL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes