Bird
0
0

Which SQL query correctly groups employees by department and orders the result by the number of employees descending?

easy📝 Syntax Q3 of 15
SQL - GROUP BY and HAVING
Which SQL query correctly groups employees by department and orders the result by the number of employees descending?
ASELECT department, COUNT(*) FROM employees ORDER BY COUNT(*) DESC GROUP BY department;
BSELECT department, COUNT(*) FROM employees GROUP BY department ORDER BY COUNT(*) DESC;
CSELECT department, COUNT(*) FROM employees GROUP BY department HAVING ORDER BY COUNT(*) DESC;
DSELECT department, COUNT(*) FROM employees ORDER BY department GROUP BY COUNT(*) DESC;
Step-by-Step Solution
Solution:
  1. Step 1: Check correct GROUP BY and ORDER BY syntax

    GROUP BY must come before ORDER BY. COUNT(*) is used to count employees per department.
  2. Step 2: Validate SELECT department, COUNT(*) FROM employees GROUP BY department ORDER BY COUNT(*) DESC;

    SELECT department, COUNT(*) FROM employees GROUP BY department ORDER BY COUNT(*) DESC; correctly groups by department and orders by count descending.
  3. Final Answer:

    SELECT department, COUNT(*) FROM employees GROUP BY department ORDER BY COUNT(*) DESC; -> Option B
  4. Quick Check:

    GROUP BY before ORDER BY with aggregate [OK]
Quick Trick: GROUP BY before ORDER BY; aggregates in ORDER BY allowed [OK]
Common Mistakes:
MISTAKES
  • Placing ORDER BY before GROUP BY
  • Using HAVING incorrectly with ORDER BY
  • Ordering by columns not in SELECT

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes