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
| 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.