Bird
0
0

Why does this self join query cause an error?

medium📝 Debug Q7 of 15
PostgreSQL - Joins in PostgreSQL
Why does this self join query cause an error?
SELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.id WHERE e1.manager_id = e2.manager_id;
AWHERE clause references columns ambiguously causing error
BMissing alias for employees table
CNo error, query runs fine
DON clause is invalid syntax
Step-by-Step Solution
Solution:
  1. Step 1: Verify syntax and column references

    All columns are fully qualified with table aliases (e1.manager_id = e2.manager_id), no ambiguity.
  2. Step 2: Confirm query executes

    The JOIN with ON and WHERE clause are syntactically valid; it may return no rows depending on data but does not error.
  3. Final Answer:

    No error, query runs fine -> Option C
  4. Quick Check:

    Fully qualified columns = No syntax error [OK]
Quick Trick: Fully qualified columns in WHERE prevent ambiguity [OK]
Common Mistakes:
  • Thinking qualified references are ambiguous
  • Expecting NULL handling to cause runtime errors
  • Confusing empty results with syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes