EasyDatabaseUpdated Sep 2026

Employees Earning More Than Their Managers

Asked at Google, Meta

Difficulty

Easy

Time

O(n^2)

Space

O(n)

Pattern

Database

Companies

2

Problem

You are given a table called Employee. Each row describes one worker and has four columns: id (unique key), name, salary, and managerId.

The managerId column holds the id of that person's manager. Since managers are also employees, managerId points to another row in the same table. A row with managerId = NULL means that person has no manager.

Write a query that returns the names of all employees whose salary is strictly greater than their own manager's salary. The output column must be named Employee, and the rows may come back in any order. Employees with no manager are never part of the answer.

Who reports to whom

EmployeeidnamesalarymanagerId01231Joe7000032Henry8000043Sam60000NULL4Max90000NULLJoe (row 1) points to id 3 = Sam, and Henry (row 2)points to id 4 = Max.Joe 70000 vs Sam 60000 -> yesHenry 80000 vs Max 90000 -> no
Each row points at its manager through managerId, so the manager is just another row in the same table.

Constraints

  • ●id is the primary key of the Employee table
  • ●1 <= number of rows in Employee <= 300
  • ●managerId is either NULL or the id of another row in the same table
  • ●Return only the column named Employee

Examples

Example 1
InputEmployee table: +----+-------+--------+-----------+ | id | name | salary | managerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3
Output+----------+ | Employee | +----------+ | Joe | +----------+
WhyJoe's managerId is 3, so his manager is Sam with salary 60000. Joe earns 70000, which is more, so he qualifies. Henry's managerId is 4, so his manager is Max with salary 90000. Henry earns 80000, which is less, so he is left out. Sam and Max have no manager, so they can never qualify.
Example 2
InputEmployee table: +----+-------+--------+-----------+ | id | name | salary | managerId | +----+-------+--------+-----------+ | 1 | Anna | 100 | Null | | 2 | Joe | 90 | 1 | | 3
Output+----------+ | Employee | +----------+ | Henry | +----------+
WhyJoe's manager is Anna (100), and 90 is not greater than 100, so Joe is out. Henry's manager is Joe (90), and 110 is greater than 90, so Henry is in. Anna has no manager, so she is skipped.

Asked At

2 companies

How to Think About It

1

Self-join approach: join the Employee table with itself. Alias one copy as e (employee) and the other as m (manager). The join condition is e.managerId = m.id. Then filter where e.salary > m.salary.

2

Subquery approach: use a correlated subquery in the WHERE clause. WHERE salary > (SELECT salary FROM Employee m WHERE m.id = e.managerId). Same logic, different syntax. Both are O(n²) in the worst case.

3

Why self-join: the manager is also an employee, stored in the same table. The managerId foreign key points to another row in the same table. A self-join lets you compare two rows from the same table as if they were different tables.

4

Edge cases: employees with no manager (managerId is NULL) should be excluded. An employee who is their own manager (self-referential) should also be excluded since they cannot earn more than themselves.

Optimal Approach

Step 1: Self-join Employee table. Alias employee copy as e and manager copy as m.
Step 2: Join on e.managerId = m.id to pair each employee with their manager.
Step 3: Filter with e.salary > m.salary to keep only employees earning more.
Step 4: Select e.name as the result.

Walkthrough: For [[1, Anna, 100, null], [2, Joe, 90, 1], [3, Henry, 110, 2]]:

  • Self-join pairs: (Joe, Anna), (Henry, Joe)
  • Filter salary > salary: (Henry, 110 > 90) passes
  • Result: Henry

Time: O(n²) worst case for the join. Space: O(n) for the join result.

Self-join employee with manager

copy table twice: e and mjoin e.managerId = m.idkeep e.salary > m.salaryselect e.name as EmployeeThe join pairs every employee with the row that holdstheir manager's salary.joined pairse.namee.salarym.namem.salary01Joe70000Sam60000Henry80000Max90000Row 1 passes the filter (70000 > 60000); row 2 fails(80000 < 90000).SELECT e.name AS EmployeeFROM Employee e JOIN Employee m ON e.managerId = m.idWHERE e.salary > m.salary
Join the table to itself on managerId = id, then keep only the rows where the employee earns more.
ComplexityTime O(n^2)Space O(n)The self-join compares each row against the other rows to find its manager, and the result set can hold up to n names.

What Trips People Up in Real Interviews

1

Forgetting that managerId can be NULL. Employees without managers (like the CEO) have managerId = NULL. A self-join with e.managerId = m.id naturally excludes them since NULL != any id.

2

Using INNER JOIN without realizing it already handles NULLs. INNER JOIN silently drops rows where managerId is NULL, so you don't need an explicit NULL check. But a LEFT JOIN would require one.

3

Selecting the wrong column. The problem asks for names, not ids or salaries. SELECT e.name is what you need, not SELECT e.id or SELECT e.salary.

4

Joining on the wrong direction. e.managerId = m.id means e is the employee and m is the manager. Flipping it gives you managers who earn less than their employees, which is logically the same but semantically backwards and confuses interviewers.

5

Forgetting that one manager can have multiple employees earning more. The query should return all such employees, not just one. This is a common mistake when people add LIMIT 1 without being asked.

Solution Code

O(n^2) time
SELECT e.name AS Employee
FROM Employee e
JOIN Employee m ON e.managerId = m.id
WHERE e.salary > m.salary;

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Employees Earning More Than Their Managers problem?

Write a SQL query to find the names of employees who earn more than their managers. The Employee table contains id, name, salary, and managerId. This is a classic self-join problem that tests your understanding of relational queries.

How do you solve Employees Earning More Than Their Managers?

The optimal approach runs in O(n^2) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Employees Earning More Than Their Managers?

Employees Earning More Than Their Managers is asked at Google, Meta. It is a easy difficulty problem.

What are common mistakes on Employees Earning More Than Their Managers?
  • Forgetting that managerId can be NULL. Employees without managers (like the CEO) have `managerId = NULL`. A self-join with `e.managerId = m.id` naturally excludes them since NULL != any id.
  • Using INNER JOIN without realizing it already handles NULLs. INNER JOIN silently drops rows where `managerId` is NULL, so you don't need an explicit NULL check. But a LEFT JOIN would require one.
  • Selecting the wrong column. The problem asks for names, not ids or salaries. `SELECT e.name` is what you need, not `SELECT e.id` or `SELECT e.salary`.
  • Joining on the wrong direction. `e.managerId = m.id` means e is the employee and m is the manager. Flipping it gives you managers who earn less than their employees, which is logically the same but semantically backwards and confuses interviewers.
  • Forgetting that one manager can have multiple employees earning more. The query should return all such employees, not just one. This is a common mistake when people add `LIMIT 1` without being asked.