Employees and Their Managers
Return employee_id, the employee's name as employee, and the manager's name as manager for all employees. The employee at the top has no manager and must still appear, with NULL as the manager. Sort by employee_id.
Hint 1
Join the table to itself with two different aliases, for example e for the employee and m for the manager.
Hint 2
The top employee has manager_id = NULL, so use a LEFT JOIN.
Reference solution
SELECT e.employee_id,
e.name AS employee,
m.name AS manager
FROM employees AS e
LEFT JOIN employees AS m ON m.employee_id = e.manager_id
ORDER BY e.employee_id;
A self join uses the same table twice under different aliases: one copy plays the employee, the other plays the manager. The link is "the manager's id equals this employee's manager_id". Because the top manager has no manager, an inner join would silently remove that row; the LEFT JOIN keeps every employee. Hierarchies (staff, categories, folders) are modelled this way everywhere.