Bird
0
0

Consider this SQL query intended to list employees and their managers:

medium📝 Debug Q14 of 15
SQL - Advanced Joins
Consider this SQL query intended to list employees and their managers:
SELECT e.name, m.name AS manager_name
FROM employees e
JOIN employees m ON e.id = m.manager_id;

What is the error in this query?
AThe join condition is reversed; it should be e.manager_id = m.id
BThe table alias 'm' is not defined
CThe query should use LEFT JOIN instead of JOIN
DThe SELECT clause should use m.manager_name instead of m.name
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the join condition

    The query joins on e.id = m.manager_id, which means employee id equals manager's manager_id, which is incorrect.
  2. Step 2: Correct join condition for manager lookup

    To find each employee's manager, join on e.manager_id = m.id so employee's manager_id matches manager's id.
  3. Final Answer:

    The join condition is reversed; it should be e.manager_id = m.id -> Option A
  4. Quick Check:

    Join on employee.manager_id = manager.id [OK]
Quick Trick: Match child.manager_id to parent.id, not reverse [OK]
Common Mistakes:
MISTAKES
  • Reversing join keys causing wrong matches
  • Confusing alias usage
  • Assuming INNER JOIN always needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes