Build Each Employee's Management Chain From the CEO With a Recursive CTE
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Using a recursive CTE, return each employee's management chain from the CEO (the employee whose `manager_id` is NULL) down to that employee, written as the names along the path joined by ` -> `. For every employee, report `employee_id`, `name`, `level` (1 for the CEO, 2 for the CEO's direct reports, and so on) and `chain`.
### Input Table(s)
`employees`
| Column | Type | Meaning |
| --- | --- | --- |
| employee_id | INT | Employee id. Primary key. |
| name | VARCHAR(100) | Employee name. NOT NULL. |
| manager_id | INT | `employee_id` of this employee's manager. Foreign key to `employees(employee_id)`. NULL only for the CEO. |
| department | VARCHAR(50) | Department name. |
| salary | DECIMAL(10,2) | Salary. |
### Output Contract
- Columns, in this order: `employee_id`, `name`, `level`, `chain`.
- One row per employee, including the CEO.
- `level` is an integer: 1 for the CEO, and one more than the manager's level for everyone else.
- `chain` starts with the CEO's name and ends with the employee's own name, with consecutive names separated by exactly ` -> ` (space, hyphen, greater-than sign, space). The CEO's chain is just the CEO's name.
- Sort by `level` ascending, then `employee_id` ascending.
### Example
`employees`
| employee_id | name | manager_id | department | salary |
| --- | --- | --- | --- | --- |
| 1 | Alice | NULL | Executive | 250000.00 |
| 2 | Bob | 1 | Engineering | 180000.00 |
| 3 | Carol | 2 | Engineering | 140000.00 |
| 4 | Dan | 1 | Sales | 150000.00 |
| 5 | Erin | 3 | Engineering | 110000.00 |
| 6 | Frank | 4 | Sales | 95000.00 |
Expected result:
| employee_id | name | level | chain |
| --- | --- | --- | --- |
| 1 | Alice | 1 | Alice |
| 2 | Bob | 2 | Alice -> Bob |
| 4 | Dan | 2 | Alice -> Dan |
| 3 | Carol | 3 | Alice -> Bob -> Carol |
| 6 | Frank | 3 | Alice -> Dan -> Frank |
| 5 | Erin | 4 | Alice -> Bob -> Carol -> Erin |
Alice has no manager, so she is level 1. Bob and Dan report to her, Carol and Frank report to Bob and Dan, and Erin reports to Carol. Dan (id 4) comes before Carol (id 3) because rows are sorted by level first.
### Constraints and Clarifications
- The table has at most 100 rows, and `employee_id` values are unique positive integers.
- Exactly one employee has a NULL `manager_id`. That employee is the CEO.
- The hierarchy is a valid tree with no cycles and a depth of at most 10, so every employee is reachable from the CEO and appears exactly once in the result.
- `name` is never NULL. Two employees may share a name; they are still separate rows.
- `department` and `salary` are not used.
```hint Column types in recursion
In PostgreSQL, each column of a recursive CTE must have the same type in the starting query and in the recursive step. Check what type string concatenation returns compared with `VARCHAR(100)`.
```
Overview: Write a recursive SQL query that walks an employee hierarchy from the CEO downward and returns each employee's level and the chain of names leading to them. It tests recursive CTEs, carrying state from one level to the next, and PostgreSQL's column type rules for recursion.