CEO Subordinate Hierarchy - Problem

CEO Subordinate Hierarchy

Imagine you're working at a company and need to build a complete organizational chart showing everyone who reports to the CEO, either directly or indirectly. This is a classic hierarchical tree traversal problem that tests your understanding of recursive relationships in databases.

You have an Employees table where each employee has a manager (except the CEO who has NULL as manager_id). Your task is to:

  • Find all subordinates of the CEO at any level
  • Calculate their hierarchy level (1 for direct reports, 2 for their reports, etc.)
  • Compute salary differences from the CEO's salary
  • Order results by hierarchy level, then by employee ID

This problem combines recursive tree traversal with salary analysis, making it perfect for testing your SQL skills with Common Table Expressions (CTEs) and recursive queries.

Example: If Alice (CEO) manages Bob and Charlie, and Bob manages David, then David has hierarchy_level = 2 and salary_difference = David's salary - Alice's salary.

Input & Output

example_1.sql โ€” Basic Hierarchy
$ Input: Employees = [[1,'Alice',null,150000],[2,'Bob',1,120000],[3,'Charlie',1,110000],[4,'David',2,105000]]
โ€บ Output: [[2,'Bob',1,-30000],[3,'Charlie',1,-40000],[4,'David',2,-45000]]
๐Ÿ’ก Note: Alice is CEO. Bob and Charlie report directly to Alice (level 1). David reports to Bob (level 2). Salary differences calculated from Alice's 150000.
example_2.sql โ€” Deep Hierarchy
$ Input: Employees = [[1,'CEO',null,200000],[2,'VP',1,150000],[3,'Director',2,120000],[4,'Manager',3,100000],[5,'Employee',4,80000]]
โ€บ Output: [[2,'VP',1,-50000],[3,'Director',2,-80000],[4,'Manager',3,-100000],[5,'Employee',4,-120000]]
๐Ÿ’ก Note: 5-level hierarchy from CEO to Employee. Each level shows increasing negative salary difference from CEO's 200000.
example_3.sql โ€” Single Employee
$ Input: Employees = [[1,'OnlyCEO',null,100000]]
โ€บ Output: []
๐Ÿ’ก Note: Only CEO exists with no subordinates, so result is empty (CEO is excluded from output).

Constraints

  • 1 โ‰ค employees.length โ‰ค 1000
  • employee_id is unique for each employee
  • Exactly one employee has manager_id = NULL (the CEO)
  • All manager_id values (except CEO) reference valid employee_id
  • 1 โ‰ค salary โ‰ค 106
  • No circular management relationships exist

Visualization

Tap to expand
CEO Subordinate Hierarchy VisualizationAlice (CEO)$150,000Bob$120K (-30K)Charlie$110K (-40K)Level 1: Direct ReportsDavid-45KEve-50KFrank-55KGrace-52KLevel 2: Second-tier ReportsHelen-60KLevel 3: Third-tier ReportsRecursive CTE Process:1. Base: Find CEO (Alice) with NULL manager_id2. Recursive: Join employees โ†’ managers, increment level, carry CEO salary3. Result: All subordinates with level + salary difference4. Order: By hierarchy_level ASC, subordinate_id ASC
Understanding the Visualization
1
Identify CEO
Find the root of hierarchy (manager_id IS NULL)
2
Recursive Descent
Use CTE to traverse each management level
3
Track Levels
Increment hierarchy_level for each generation
4
Calculate Differences
Compute salary gaps from CEO's compensation
Key Takeaway
๐ŸŽฏ Key Insight: Recursive CTEs elegantly solve hierarchical problems by starting with a base case (CEO) and recursively building levels while carrying forward essential data (CEO salary) for calculations.
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28 Apple 22
52.4K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen