Given an Employees table containing information about employees and their managers, analyze the organizational hierarchy to determine:
- Hierarchy Levels: Each employee's level in the organization (CEO is level 1, direct reports to CEO are level 2, etc.)
- Team Size: For each manager, count total employees under them (direct and indirect reports)
- Salary Budget: For each manager, calculate total salary budget they control (all reports' salaries plus their own)
Return results ordered by level ascending, then budget descending, then employee_name ascending.
Table Schema
| Column Name | Type | Description |
|---|---|---|
employee_id
PK
|
int | Unique identifier for each employee |
employee_name
|
varchar | Name of the employee |
manager_id
|
int | ID of the employee's manager (NULL for CEO) |
salary
|
int | Employee's salary |
department
|
varchar | Department where employee works |
Input & Output
| employee_id | employee_name | manager_id | salary | department |
|---|---|---|---|---|
| 1 | Alice | 10000 | IT | |
| 2 | Bob | 1 | 8000 | IT |
| 3 | Carol | 2 | 6000 | IT |
| 4 | David | 1 | 7000 | HR |
| 5 | Eve | 4 | 5000 | HR |
| employee_id | employee_name | level | team_size | budget |
|---|---|---|---|---|
| 1 | Alice | 1 | 4 | 36000 |
| 2 | Bob | 2 | 1 | 14000 |
| 4 | David | 2 | 1 | 12000 |
| 3 | Carol | 3 | 0 | 6000 |
| 5 | Eve | 3 | 0 | 5000 |
Alice is the CEO (level 1) with 4 total subordinates and controls the entire company budget of 36000. Bob and David are level 2 managers with 1 subordinate each. Carol and Eve are level 3 employees with no subordinates.
| employee_id | employee_name | manager_id | salary | department |
|---|---|---|---|---|
| 1 | John | 15000 | CEO |
| employee_id | employee_name | level | team_size | budget |
|---|---|---|---|---|
| 1 | John | 1 | 0 | 15000 |
John is the only employee and CEO, so he's at level 1 with no team members and only controls his own salary as budget.
| employee_id | employee_name | manager_id | salary | department |
|---|---|---|---|---|
| 1 | Manager | 12000 | Management | |
| 2 | Alice | 1 | 8000 | Sales |
| 3 | Bob | 1 | 8500 | Marketing |
| 4 | Carol | 1 | 7500 | Support |
| employee_id | employee_name | level | team_size | budget |
|---|---|---|---|---|
| 1 | Manager | 1 | 3 | 36000 |
| 3 | Bob | 2 | 0 | 8500 |
| 2 | Alice | 2 | 0 | 8000 |
| 4 | Carol | 2 | 0 | 7500 |
Flat organization with Manager at level 1 controlling 3 direct reports. Level 2 employees are ordered by budget descending (Bob > Alice > Carol), then by name alphabetically.
Constraints
-
1 ≤ employee_id ≤ 1000 -
1 ≤ employee_name.length ≤ 100 -
1000 ≤ salary ≤ 100000 -
manager_idis eitherNULL(for CEO) or references a validemployee_id -
1 ≤ department.length ≤ 50