⌨ Programming Medium SQL Joins

Employee and Their Manager Name

Question

List every employee along with their manager's full name. Include employees who have no manager (the CEO).

πŸ’‘ Use the employees table with a SELF JOIN on manager_id.
SQL Editor
Solution
SELECT
  e.first_name || ' ' || e.last_name AS employee_name,
  e.role,
  m.first_name || ' ' || m.last_name AS manager_name
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id
ORDER BY manager_name NULLS LAST, e.last_name;
Explanation: Self-join the employees table: alias e for employee, alias m for manager. LEFT JOIN so the CEO (who has no manager) also appears.