Bird
0
0

Identify the error in this SQL query intended to list employees with their managers:

medium📝 Debug Q6 of 15
SQL - Advanced Joins
Identify the error in this SQL query intended to list employees with their managers:
SELECT e.name, m.name FROM employees e JOIN employees m ON e.id = m.manager_id;
AUsing JOIN instead of LEFT JOIN causes missing employees without managers.
BThe query is missing a WHERE clause to filter managers.
CThe join condition is reversed; it should be e.manager_id = m.id.
DThe SELECT clause should use aliases for columns.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze join condition

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

    It should be e.manager_id = m.id to link employee to their manager.
  3. Step 3: Effect of error

    Current join will not correctly associate employees with their managers.
  4. Final Answer:

    The join condition is reversed; it should be e.manager_id = m.id. is correct.
  5. Quick Check:

    Employee.manager_id matches Manager.id [OK]
Quick Trick: Join on employee.manager_id = manager.id [OK]
Common Mistakes:
MISTAKES
  • Reversing join keys in self join
  • Confusing employee id with manager id
  • Assuming JOIN type causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes