Overview
Show every employee with their manager by joining the employees table to itself, without losing the boss who has no manager.
- Two aliases, one table
- LEFT JOIN for missing parents
- COALESCE() replaces NULL
Short Course
A table can join to itself: an employee's manager is another employee. Give
each copy its own alias. Trap: an inner join drops the boss, who has no
manager; use LEFT JOIN, and COALESCE(x, 'none') to replace the NULL.
SELECT c.name, COALESCE(p.name, 'top level') AS parent
FROM categories AS c
LEFT JOIN categories AS p ON p.id = c.parent_id;
Task
Return each employee's name and their manager's name, with 'none' for
someone without a manager, ordered by employee name. In the database below,
Ada is the boss.
Database
Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.
| idINTEGER | nameTEXT | manager_idINTEGER |
|---|---|---|
| 1 | Ada | NULL |
| 2 | Ben | 1 |
| 3 | Cleo | 1 |
| 4 | Dan | 2 |
Test Suites
Pending
Next
Output