Problem
Table: Employees
|
|
Write a solution to analyze the organizational hierarchy and answer the following:
- Hierarchy Levels: For each employee, determine their level in the organization (CEO is level
1
, employees reporting directly to the CEO are level2
, and so on). - Team Size: For each employee who is a manager, count the total number of employees under them (direct and indirect reports).
- Salary Budget: For each manager, calculate the total salary budget they control (sum of salaries of all employees under them, including indirect reports, plus their own salary).
Return the result table ordered by the result ordered bylevel in ascending order, then by budget in descending order, and finally by employee_name in ascending order.
The result format is in the following example.
Examples
Example 1:
|
|
Explanation:
- Organization Structure:
- Alice (ID: 1) is the CEO (level 1) with no manager
- Bob (ID: 2) and Charlie (ID: 3) report directly to Alice (level 2)
- David (ID: 4), Eva (ID: 5) report to Bob, while Frank (ID: 6) and Grace (ID: 7) report to Charlie (level 3)
- Hank (ID: 8) reports to David, and Ivy (ID: 9) and Judy (ID: 10) report to Frank (level 4)
- Level Calculation:
- The CEO (Alice) is at level 1
- Each subsequent level of management adds 1 to the level
- Team Size Calculation:
- Alice has 9 employees under her (the entire company except herself)
- Bob has 3 employees (David, Eva, and Hank)
- Charlie has 4 employees (Frank, Grace, Ivy, and Judy)
- David has 1 employee (Hank)
- Frank has 2 employees (Ivy and Judy)
- Eva, Grace, Hank, Ivy, and Judy have no direct reports (team_size = 0)
- Budget Calculation:
- Alice’s budget: Her salary (12000) + all employees’ salaries (72500) = 84500
- Charlie’s budget: His salary (10000) + Frank’s budget (23000) + Grace’s salary (8500) = 41500
- Bob’s budget: His salary (10000) + David’s budget (13500) + Eva’s salary (7500) = 31000
- Frank’s budget: His salary (9000) + Ivy’s salary (7000) + Judy’s salary (7000) = 23000
- David’s budget: His salary (7500) + Hank’s salary (6000) = 13500
- Employees with no direct reports have budgets equal to their own salary
Note:
- The result is ordered first by level in ascending order
- Within the same level, employees are ordered by budget in descending order then by name in ascending order
Solution
Method 1 – Using Recursive CTE
Intuition
The organization hierarchy forms a tree structure, where each employee points to their manager. By recursively traversing this tree from the CEO (who has no manager), we can assign levels, count subordinates, and sum salaries for each subtree rooted at every employee.
Approach
- Build the Hierarchy with Levels:
- Use a recursive CTE to start from the CEO (manager_id IS NULL), assigning level 1.
- For each employee, increment the level by 1 from their manager.
- Find All Subordinates:
- Use another recursive CTE to collect, for each manager, all employees in their subtree (direct and indirect reports).
- Calculate Team Size and Budget:
- For each employee, count the number of subordinates (excluding themselves) for team_size.
- Sum the salaries of themselves and all their subordinates for budget.
- Assemble the Result:
- Join the above results to get employee_id, employee_name, level, team_size, and budget.
- Order by level ASC, budget DESC, employee_name ASC.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
(Each employee may be traversed for every manager in the worst case, e.g., a chain hierarchy.) - 🧺 Space complexity:
O(n^2)
(The Subtree CTE can have up to n^2 rows in the worst case.)