Bird
0
0

Identify the error in this SQL query:

medium📝 Debug Q6 of 15
SQL - GROUP BY and HAVING
Identify the error in this SQL query:

SELECT department, AVG(salary) FROM employees ORDER BY AVG(salary) DESC GROUP BY department;
AAVG(salary) cannot be used in ORDER BY
BORDER BY clause appears before GROUP BY, causing syntax error
CGROUP BY must include salary column
DMissing alias for AVG(salary)
Step-by-Step Solution
Solution:
  1. Step 1: Check clause order

    GROUP BY must come before ORDER BY in SQL syntax.
  2. Step 2: Identify error

    ORDER BY appears before GROUP BY, which is invalid and causes syntax error.
  3. Final Answer:

    ORDER BY clause appears before GROUP BY, causing syntax error -> Option B
  4. Quick Check:

    GROUP BY before ORDER BY required [OK]
Quick Trick: GROUP BY must come before ORDER BY [OK]
Common Mistakes:
MISTAKES
  • Placing ORDER BY before GROUP BY
  • Using aggregate functions incorrectly in ORDER BY
  • Forgetting to alias aggregates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes