Bird
0
0

Identify the error in this query:

medium📝 Debug Q6 of 15
PostgreSQL - Subqueries in PostgreSQL
Identify the error in this query:
SELECT e.name FROM employees e WHERE e.salary > (SELECT AVG(salary) FROM employees WHERE department_id = department_id);
AThe subquery uses an ambiguous column reference.
BThe subquery is missing correlation to the outer query.
CThe subquery should use COUNT instead of AVG.
DThe outer query is missing a GROUP BY clause.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the subquery condition

    The condition department_id = department_id compares the same column without alias, causing ambiguity.
  2. Step 2: Understand the fix

    It should reference outer query's alias like department_id = e.department_id to correlate properly.
  3. Final Answer:

    The subquery uses an ambiguous column reference. -> Option A
  4. Quick Check:

    Ambiguous column in subquery = The subquery uses an ambiguous column reference. [OK]
Quick Trick: Always alias columns to avoid ambiguity in correlated subqueries. [OK]
Common Mistakes:
  • Ignoring missing alias causing ambiguity.
  • Thinking AVG vs COUNT is the error.
  • Assuming GROUP BY is required here.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes