Skip to main content
madhavcodes25
EditReport

Employees Earning More Than Their Managers

Description:​

Write a solution to find the employees who earn more than their managers. Return the result table in any order.

Table: Employee

Column NameType
idint
namevarchar
salaryint
managerIdint

id is the primary key for this table. Each row of this table indicates the ID of an employee, their name, salary, and the ID of their manager.

Example 1: Input: Employee table:

idnamesalarymanagerId
1Joe700003
2Henry800004
3Sam60000Null
4Max90000Null

Output:

Employee
Joe

Explanation: Joe is the only employee who earns more than his manager.


Approaches:​

1. Self-Join​

We can join the Employee table to itself to compare an employee's salary directly with their manager's salary. By matching an employee's managerId to another employee's id (who acts as the manager), we can filter for cases where the employee's salary is strictly greater.

  • Time Complexity: O(N)O(N) or O(Nlog⁑N)O(N \log N) depending on the database engine's join implementation and available indexing.
  • Space Complexity: O(N)O(N) to store the final result set.

Self-Join Solutions:​

MySQL / PostgreSQL

SELECT 
e1.name AS Employee
FROM
Employee e1
JOIN
Employee e2
ON
e1.managerId = e2.id
WHERE
e1.salary > e2.salary;
Finished reading? Mark this topic as complete.