List Each Employee Alongside Their Manager's Name

Quick Overview

Write a SQL query that pairs each employee with their manager's name, using one table that stores each person's manager, and skip employees who have no manager. It tests self-joins, how inner joins treat NULL keys, and stable result ordering.

List Each Employee Alongside Their Manager's Name

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `Employee` table stores each employee's manager as `manager_id`, which references another row's `id` in the same table. Return each employee's name as `Employee` and their manager's name as `Manager`, excluding employees whose `manager_id` is NULL. Order by employee `id` ascending. ```hint One table, two roles Each output row combines two rows of the same table: the employee's row and the manager's row. ``` ### Input Table(s) Table: `Employee` | Column | Type | Meaning | |---|---|---| | id | INT | Employee identifier (primary key) | | name | VARCHAR(100) | Employee name, not null | | manager_id | INT | The employee's manager (foreign key to `Employee.id`), NULL for employees with no manager | Keys: `id` is the primary key. `manager_id` references `Employee.id`. ### Output Contract - Two columns, in this order: `Employee` (the employee's `name`) and `Manager` (the manager's `name`). - One row per employee who has a manager. Employees with `manager_id` NULL are not returned. - Sort by the employee's `id` ascending. It is the primary key, so the order is unique. - No rounding is involved. ### Example `Employee` | id | name | manager_id | |---|---|---| | 1 | Alice | NULL | | 2 | Bob | 1 | | 3 | Charlie | 1 | | 4 | Diana | 2 | | 5 | Eve | 3 | Expected output: | Employee | Manager | |---|---| | Bob | Alice | | Charlie | Alice | | Diana | Bob | | Eve | Charlie | Alice has no manager, so she is excluded. Bob and Charlie report to Alice (id 1), Diana reports to Bob (id 2), and Eve reports to Charlie (id 3). The rows follow employee ids 2 to 5. ### Constraints and Clarifications - The table has at most 100 rows. - No employee is their own manager. - A non-NULL `manager_id` always matches an existing `id`. - A manager with several reports appears once per report in the `Manager` column.

Overview: Write a SQL query that pairs each employee with their manager's name, using one table that stores each person's manager, and skip employees who have no manager. It tests self-joins, how inner joins treat NULL keys, and stable result ordering.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The Employee table stores each employee's manager as manager_id, which references another row's id in the same table. Return each employee's name as Employee and their manager's name as Manager, excluding employees whose manager_id is NULL. Order by employee id ascending.

Input Table(s)

Table: Employee

ColumnTypeMeaning
idINTEmployee identifier (primary key)
nameVARCHAR(100)Employee name, not null
manager_idINTThe employee's manager (foreign key to Employee.id), NULL for employees with no manager

Keys: id is the primary key. manager_id references Employee.id.

Output Contract

  • Two columns, in this order: Employee (the employee's name ) and Manager (the manager's name ).
  • One row per employee who has a manager. Employees with manager_id NULL are not returned.
  • Sort by the employee's id ascending. It is the primary key, so the order is unique.
  • No rounding is involved.

Example

Employee

idnamemanager_id
1AliceNULL
2Bob1
3Charlie1
4Diana2
5Eve3

Expected output:

EmployeeManager
BobAlice
CharlieAlice
DianaBob
EveCharlie

Alice has no manager, so she is excluded. Bob and Charlie report to Alice (id 1), Diana reports to Bob (id 2), and Eve reports to Charlie (id 3). The rows follow employee ids 2 to 5.

Constraints and Clarifications

  • The table has at most 100 rows.
  • No employee is their own manager.
  • A non-NULL manager_id always matches an existing id .
  • A manager with several reports appears once per report in the Manager column.