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
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
Employee table:
+----+-------+--------+-----------+
| id | name | salary | managerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 +----------+
| Employee |
+----------+
| Joe |
+----------+Employee table:
+----+-------+--------+-----------+
| id | name | salary | managerId |
+----+-------+--------+-----------+
| 1 | Anna | 100 | Null |
| 2 | Joe | 90 | 1 |
| 3 +----------+
| Employee |
+----------+
| Henry |
+----------+Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| Easy | View all Google questions → | |
| Meta | Easy | View all Meta questions → |
How to Think About It
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.
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.
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.
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
What Trips People Up in Real Interviews
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.
Solution Code
O(n^2) timeSELECT e.name AS Employee
FROM Employee e
JOIN Employee m ON e.managerId = m.id
WHERE e.salary > m.salary;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.