Bird
0
0

You want to find the department with the highest average salary and also show the total number of employees in that department. Which query correctly combines these aggregates?

hard📝 Application Q8 of 15
SQL - Aggregate Functions
You want to find the department with the highest average salary and also show the total number of employees in that department. Which query correctly combines these aggregates?
ASELECT department, COUNT(*), AVG(salary) FROM employees WHERE AVG(salary) = (SELECT MAX(AVG(salary)) FROM employees);
BSELECT department, COUNT(*), AVG(salary) FROM employees GROUP BY department ORDER BY AVG(salary) DESC LIMIT 1;
CSELECT department, COUNT(*), AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) = MAX(AVG(salary));
DSELECT department, COUNT(*), AVG(salary) FROM employees ORDER BY AVG(salary) DESC LIMIT 1;
Step-by-Step Solution
Solution:
  1. Step 1: Group by department to get aggregates per department

    COUNT and AVG require GROUP BY department to calculate per department.
  2. Step 2: Order by AVG(salary) descending and limit to top

    ORDER BY AVG(salary) DESC LIMIT 1 selects department with highest average salary.
  3. Step 3: Validate other options

    SELECT department, COUNT(*), AVG(salary) FROM employees ORDER BY AVG(salary) DESC LIMIT 1; lacks GROUP BY, C uses invalid HAVING with MAX(AVG(salary)), A misuses WHERE with aggregate.
  4. Final Answer:

    SELECT department, COUNT(*), AVG(salary) FROM employees GROUP BY department ORDER BY AVG(salary) DESC LIMIT 1; -> Option B
  5. Quick Check:

    GROUP BY + ORDER BY + LIMIT finds top aggregate [OK]
Quick Trick: Use ORDER BY AVG(...) DESC LIMIT 1 for top group [OK]
Common Mistakes:
MISTAKES
  • Missing GROUP BY when using aggregates
  • Using HAVING with nested aggregates incorrectly
  • Trying to filter aggregates in WHERE clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes