Employee and manager
MediumSelf joinsHR analytics
The problem
List every employee with their manager's name. Name the columns employee and manager.
Employees without a manager must still appear, with manager shown as NULL.
Learn this in the course: Joins and NULLs (video and notes)
Tables · Company directory
Click a table or column name to insert it into your query.
- INTEGER · primary key
- TEXT
Sample rows (4 total)
| dept_id | dept_name |
|---|---|
| 1 | Engineering |
| 2 | Analytics |
| 3 | Sales |
| 4 | HR |
- INTEGER · primary key
- TEXT
- INTEGER · → departments, can be NULL
- INTEGER · → employees.emp_id, NULL for top managers
- INTEGER · rupees per year
Sample rows (12 total)
| emp_id | name | dept_id | manager_id | salary |
|---|---|---|---|---|
| 1 | Anita Rao | 1 | NULL | 260000 |
| 2 | Bhavin Shah | 1 | 1 | 180000 |
| 3 | Chitra Menon | 1 | 1 | 180000 |
| 4 | Deepak Verma | 1 | 2 | 140000 |
| 5 | Esha Gupta | 2 | NULL | 220000 |
Ctrl/⌘ + Enter runs the query. Table and column names are suggested as you type. Press Esc then Tab to leave the editor with the keyboard.
Expected output (12 rows)
| employee | manager |
|---|---|
| Anita Rao | NULL |
| Bhavin Shah | Anita Rao |
| Chitra Menon | Anita Rao |
| Deepak Verma | Bhavin Shah |
| Esha Gupta | NULL |
| Farhan Ali | Esha Gupta |
| Gita Pillai | Esha Gupta |
| Harsh Vora | Farhan Ali |
| Imran Sheikh | NULL |
| Jaya Bose | Imran Sheikh |
| Kiran Dutta | Imran Sheikh |
| Lata Menon | NULL |
Row order doesn’t matter for this question. Numbers are compared with a tiny tolerance; text must match exactly.
Stuck?
Stored on this device only. Your latest query and “solved” status are remembered in this browser only. They don’t sync between devices, and clearing your browser data removes them.