Bird
0
0

Identify the error in this self join query:

medium📝 Debug Q6 of 15
PostgreSQL - Joins in PostgreSQL
Identify the error in this self join query:
SELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.id WHERE e1.id = e2.id;
AIncorrect JOIN keyword used
BMissing table alias for employees
CThe WHERE clause contradicts the ON condition causing no rows
DNo error, query is valid
Step-by-Step Solution
Solution:
  1. Step 1: Analyze ON and WHERE conditions

    ON joins where e1.manager_id = e2.id, but WHERE requires e1.id = e2.id.
  2. Step 2: Understand contradiction

    e1.id cannot equal e2.id and simultaneously e1.manager_id = e2.id unless manager_id = id, which is rare; likely no rows.
  3. Final Answer:

    The WHERE clause contradicts the ON condition causing no rows -> Option C
  4. Quick Check:

    Contradictory conditions = No rows [OK]
Quick Trick: Check ON and WHERE conditions for conflicts [OK]
Common Mistakes:
  • Ignoring WHERE clause effect
  • Assuming query returns all rows
  • Confusing JOIN types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes