Employees and their managers
Medium · Company database · Self join, LEFT JOIN
An employee's manager_id is the id of another employee in the same table. The chief executive has no manager.
Return each employee's name and their manager's name, in that order, sorted by employee name A to Z. An employee without a manager appears with NULL as the manager.
Expected result
| employee | manager |
|---|---|
| Ana Reyes | Joseph Mwangi |
| Arjun Patel | Vikram Nair |
| Carlos Mendoza | Joseph Mwangi |
| Daniel Okoye | Meera Krishnan |
| Ethan Brooks | Laura Chen |
| Eva Novak | Kenji Watanabe |
| Fatima Rahman | Daniel Okoye |
| Grace Liu | Rahul Bansal |
| Hannah Schmidt | Meera Krishnan |
| Ines Moreau | Hannah Schmidt |
| Joseph Mwangi | Meera Krishnan |
| Kenji Watanabe | Meera Krishnan |
| Laura Chen | Daniel Okoye |
| Lucas Ferreira | Sophie Laurent |
| Maria Gonzalez | Vikram Nair |
| Meera Krishnan | NULL |
| Mohammed Hassan | Rahul Bansal |
| Nadia Petrova | Thandiwe Nkosi |
| Oliver Hughes | Sophie Laurent |
| Priyanka Iyer | Sophie Laurent |
| Rahul Bansal | Meera Krishnan |
| Ravi Shankar | Kenji Watanabe |
| Samuel Adeyemi | Thandiwe Nkosi |
| Sophie Laurent | Meera Krishnan |
| Thandiwe Nkosi | Sophie Laurent |
| Vikram Nair | Daniel Okoye |
| Zara Ahmed | Laura Chen |
The company tables
assignments: 30 rows, first 5 shown
| id | employee_id | project_id | hours |
|---|---|---|---|
| 1 | 8 | 1 | 320 |
| 2 | 9 | 1 | 410 |
| 3 | 11 | 1 | 380 |
| 4 | 12 | 2 | 290 |
| 5 | 10 | 2 | 150 |
departments: 8 rows, first 5 shown
| id | name | location |
|---|---|---|
| 1 | Engineering | Bengaluru |
| 2 | Sales | London |
| 3 | Marketing | London |
| 4 | Finance | Singapore |
| 5 | Human Resources | Bengaluru |
employees: 27 rows, first 5 shown
| id | name | department_id | manager_id | job_title | salary | hire_date |
|---|---|---|---|---|---|---|
| 1 | Meera Krishnan | NULL | NULL | Chief Executive Officer | 250000 | 2015-03-01 |
| 2 | Daniel Okoye | 1 | 1 | Chief Technology Officer | 250000 | 2015-03-01 |
| 3 | Sophie Laurent | 2 | 1 | Head of Sales | 150000 | 2017-01-09 |
| 4 | Rahul Bansal | 4 | 1 | Finance Director | 166000 | 2016-11-21 |
| 5 | Hannah Schmidt | 5 | 1 | Human Resources Manager | 110000 | 2018-04-02 |
projects: 10 rows, first 5 shown
| id | name | department_id | budget | start_date | end_date |
|---|---|---|---|---|---|
| 1 | Payments Platform | 1 | 450000 | 2025-01-06 | 2025-09-30 |
| 2 | Mobile App Redesign | 1 | 300000 | 2025-03-03 | NULL |
| 3 | Data Warehouse | 1 | 380000 | 2025-06-02 | 2026-03-31 |
| 4 | Spring Campaign | 3 | 90000 | 2025-02-10 | 2025-05-30 |
| 5 | Partner Portal | 2 | 150000 | 2025-04-14 | 2025-12-19 |
-- Write your SQL here