CEO Subordinate Hierarchy Problem
#3236
Description
LeetCode Problem 3236.
Table: Employees
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| employee_id | int |
| employee_name | varchar |
| manager_id | int |
| salary | int |
+---------------+---------+
employee_id is the unique identifier for this table.
manager_id is the employee_id of the employee's manager. The CEO has a NULL manager_id.
Write a solution to find subordinates of the CEO (both direct and indirect), along with their level in the hierarchy and their salary difference from the CEO.
The result should have the following columns:
The query result format is in the following example.
subordinate_id: The employee_id of the subordinatesubordinate_name: The name of the subordinatehierarchy_level: The level of the subordinate in the hierarchy (1for direct reports,2for their direct reports, and so on)salary_difference: The difference between the subordinate's salary and the CEO's salary
Return the result table ordered by hierarchy_level ascending, and then by subordinate_id ascending.
The query result format is in the following example.
Example:
Input:
Employees table:
+-------------+----------------+------------+---------+
| employee_id | employee_name | manager_id | salary |
+-------------+----------------+------------+---------+
| 1 | Alice | NULL | 150000 |
| 2 | Bob | 1 | 120000 |
| 3 | Charlie | 1 | 110000 |
| 4 | David | 2 | 105000 |
| 5 | Eve | 2 | 100000 |
| 6 | Frank | 3 | 95000 |
| 7 | Grace | 3 | 98000 |
| 8 | Helen | 5 | 90000 |
+-------------+----------------+------------+---------+
Output:
+----------------+------------------+------------------+-------------------+
| subordinate_id | subordinate_name | hierarchy_level | salary_difference |
+----------------+------------------+------------------+-------------------+
| 2 | Bob | 1 | -30000 |
| 3 | Charlie | 1 | -40000 |
| 4 | David | 2 | -45000 |
| 5 | Eve | 2 | -50000 |
| 6 | Frank | 2 | -55000 |
| 7 | Grace | 2 | -52000 |
| 8 | Helen | 3 | -60000 |
+----------------+------------------+------------------+-------------------+
Explanation:
- Bob and Charlie are direct subordinates of Alice (CEO) and thus have a hierarchy_level of 1.
- David and Eve report to Bob, while Frank and Grace report to Charlie, making them second-level subordinates (hierarchy_level 2).
- Helen reports to Eve, making Helen a third-level subordinate (hierarchy_level 3).
- Salary differences are calculated relative to Alice's salary of 150000.
- The result is ordered by hierarchy_level ascending, and then by subordinate_id ascending.
Note: The output is ordered first by hierarchy_level in ascending order, then by subordinate_id in ascending order.
Solution
Approach: Recursive CTE walking down from the CEO
The reporting lines form a tree, and the level of a person is the number of steps from the CEO, which is what a recursive CTE computes naturally.
- Anchor: the CEO, the one row whose
manager_idisNULL, at level 0. - Recursive step: join the rows found so far to
Employeesonmanager_id = employee_idand add one to the level. Each pass adds the next level down, and the recursion stops when nobody reports to the last level. - Take every row except the CEO (level above 0) and subtract the CEO's salary, giving the signed
salary_difference. - Order by level and then by id.
In Pandas the same walk is a loop that repeatedly finds the employees whose manager is in the previous level.
WITH RECURSIVE org AS (
SELECT employee_id, employee_name, salary, 0 AS hierarchy_level
FROM Employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.employee_id, e.employee_name, e.salary, o.hierarchy_level + 1
FROM org AS o
JOIN Employees AS e ON e.manager_id = o.employee_id
)
SELECT o.employee_id AS subordinate_id,
o.employee_name AS subordinate_name,
o.hierarchy_level,
o.salary - ceo.salary AS salary_difference
FROM org AS o
CROSS JOIN (SELECT salary FROM Employees WHERE manager_id IS NULL) AS ceo
WHERE o.hierarchy_level > 0
ORDER BY o.hierarchy_level, o.employee_id;