Bird
0
0

Identify the error in the following query:

medium📝 Debug Q6 of 15
PostgreSQL - Subqueries in PostgreSQL
Identify the error in the following query:
SELECT d.department_id, d.avg_salary FROM (SELECT department_id, AVG(salary) AS avg_salary FROM employees) d WHERE d.avg_salary > 60000;
AIncorrect use of WHERE clause outside subquery
BAlias 'd' is missing
CMissing GROUP BY clause in the subquery
DAVG function cannot be used in subqueries
Step-by-Step Solution
Solution:
  1. Step 1: Check subquery aggregation

    The subquery uses AVG but lacks GROUP BY, so it aggregates over all rows.
  2. Step 2: Understand the impact

    Without GROUP BY, department_id is not grouped, causing an error or unexpected result.
  3. Final Answer:

    Missing GROUP BY clause in the subquery -> Option C
  4. Quick Check:

    Aggregation needs GROUP BY when selecting non-aggregated columns [OK]
Quick Trick: Use GROUP BY with aggregates selecting other columns [OK]
Common Mistakes:
  • Omitting GROUP BY with aggregate functions
  • Assuming alias is optional
  • Misplacing WHERE clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes