Employees Earning More Than Their Managers - Problem

You're working as a data analyst for a company and need to investigate salary discrepancies. Given an Employee database table, your task is to find all employees who earn more than their direct managers.

The Employee table contains information about each employee including their ID, name, salary, and their manager's ID. Your goal is to identify potential salary inconsistencies where subordinates earn more than their supervisors.

Column NameType
idint
namevarchar
salaryint
managerIdint

Note: The id is the primary key. Some employees may not have a manager (managerId is null for top-level executives).

Return: The names of all employees who earn more than their managers, in any order.

Input & Output

Basic Example
$ Input: Employee table: +----+-------+--------+-----------+ | id | name | salary | managerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | null | | 4 | Max | 90000 | null | +----+-------+--------+-----------+
โ€บ Output: +------+ | name | +------+ | Joe | +------+
๐Ÿ’ก Note: Joe earns $70,000 while his manager Sam earns $60,000. Henry earns $80,000 but his manager Max earns $90,000, so Henry doesn't qualify.
Multiple Results
$ Input: Employee table: +----+--------+--------+-----------+ | id | name | salary | managerId | +----+--------+--------+-----------+ | 1 | Alice | 85000 | 3 | | 2 | Bob | 75000 | 3 | | 3 | Carol | 70000 | null | | 4 | David | 95000 | 5 | | 5 | Eve | 90000 | null | +----+--------+--------+-----------+
โ€บ Output: +-------+ | name | +-------+ | Alice | | Bob | | David | +-------+
๐Ÿ’ก Note: Alice ($85k) and Bob ($75k) both earn more than their manager Carol ($70k). David ($95k) earns more than his manager Eve ($90k).
No Results
$ Input: Employee table: +----+-------+--------+-----------+ | id | name | salary | managerId | +----+-------+--------+-----------+ | 1 | John | 50000 | 2 | | 2 | Jane | 80000 | null | | 3 | Mike | 60000 | 2 | +----+-------+--------+-----------+
โ€บ Output: Empty result set +------+ | name | +------+ +------+
๐Ÿ’ก Note: No employee earns more than their manager. John ($50k) and Mike ($60k) both report to Jane ($80k) and earn less than her.

Visualization

Tap to expand
Company Organizational ChartSam$60kJoe$70kMax$90kHenry$80kโœ“ Joe: $70k > $60kEarns more than Samโœ— Henry: $80k < $90kEarns less than Max
Understanding the Visualization
1
Identify Relationships
Map each employee to their direct manager using managerId
2
Compare Salaries
For each employee-manager pair, check if employee salary > manager salary
3
Filter Results
Keep only employees who earn more than their managers
4
Return Names
Output the names of qualifying employees
Key Takeaway
๐ŸŽฏ Key Insight: A self-join treats the same table as two separate entities, allowing us to efficiently match employees with their managers and compare their salaries in one optimized database operation.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Database can optimize the join using indexes, typically resulting in O(n log n) complexity

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for the result set and any temporary join tables

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค Employee table rows โ‰ค 104
  • All employee IDs are unique
  • salary values are positive integers
  • managerId can be null for top-level employees
  • No circular management relationships exist
Asked in
Google 45 Amazon 38 Meta 22 Microsoft 31
78.4K Views
High Frequency
~15 min Avg. Time
2.3K 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