Employees Earning More Than Their Managers
Asked at Google, Meta
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.
Asked At
| 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.
Visual walkthrough for table: id=1 Anna salary=100 managerId=null, id=2 Joe salary=90 managerId=1, id=3 Henry salary=110 managerId=2.
e.id=2, e.name=Joe, e.salary=90, m.id=1, m.name=Anna, m.salary=100 -> 90 > 100? No.
e.id=3, e.name=Henry, e.salary=110, m.id=2, m.name=Joe, m.salary=90 -> 110 > 90? Yes! Result: Henry.
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.
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
SELECT 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 is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.
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.