SQLQuestion 03 of 14
SELF JOIN — an employee and their manager
Answer out loud or in your head first, then compare.
Answer
I join the table to itself using two aliases. LEFT JOIN also keeps people without a manager.
SQL
-- employees(id PRIMARY KEY, name, manager_id)
SELECT e.name AS employee, m.name AS manager
FROM employees AS e
LEFT JOIN employees AS m ON m.id = e.manager_id;To be precise: INNER JOIN returns only people with a match. Here, no manager gives NULL in the manager column.
Pitfall: a filter WHERE m.name = 'Anna' drops the rows without a match. With outer joins it matters whether a condition goes into ON or WHERE.
How did it go? Be honest — “Not yet” puts the question on your review list.