181. Employees Earning More Than Their Managers - Detailed Explanation
Problem Statement
We are given a table called Employee with the following columns:
- 
id: The unique identifier for each employee. 
- 
name: The employee's name. 
- 
salary: The employee's salary. 
- 
managerId: The id of the employee’s manager (this value is NULL if the employee does not have a manager). 
The task is to write a SQL query that returns the names of all employees who earn more than their respective managers.
Example
Consider the following sample data in the Employee table:
| id | name | salary | managerId | 
|---|---|---|---|
| 1 | Joe | 70000 | 3 | 
| 2 | Henry | 80000 | 4 | 
| 3 | Sam | 60000 | NULL | 
| 4 | Max | 90000 | 3 | 
Explanation:
- Joe (id 1):
- Manager: Sam (id 3) with a salary of 60000.
- Since 70000 > 60000, Joe qualifies.
 
- Henry (id 2):
- Manager: Max (id 4) with a salary of 90000.
- Since 80000 < 90000, Henry does not qualify.
 
- Max (id 4):
- Manager: Sam (id 3) with a salary of 60000.
- Since 90000 > 60000, Max qualifies.
 
Expected Output:
The query should return the names "Joe" and "Max".
Constraints
- Every employee except the top-level manager has a valid managerId.
- The table can have multiple employees reporting to the same manager.
- Some employees might not have a manager (their managerId is NULL), and they should not be considered in the comparison.
Hints
- Self-Join:
- Think about how you can compare rows within the same table. In this case, you need to compare an employee’s salary with his/her manager’s salary. This is a perfect scenario for a self-join.
 
- Filtering with Conditions:
- Use a join condition to connect each employee with his/her manager and then apply a WHERE clause to filter out only those cases where the employee’s salary is greater than the manager’s salary.
 
Approach: Using a Self-Join
Explanation
- Self-Join Concept:
- 
Since both employees and managers reside in the same Employee table, you can perform a self-join. 
- 
Create two aliases for the table, for example, e (for employee) and m (for manager). 
 
- 
- Join Condition:
- Link the two aliases by matching e.managerId with m.id. This effectively pairs each employee with their manager.
 
- Filtering Condition:
- Use a WHERE clause to select only those pairs where the employee’s salary (e.salary) is greater than the manager’s salary (m.salary).
 
- Result:
- Select the employee’s name from the resulting set.
 
SQL Query (Solution)
SELECT e.name AS Employee FROM Employee e JOIN Employee m ON e.managerId = m.id WHERE e.salary > m.salary;
Walkthrough
- Alias Assignment:
- We create two aliases: e for the employees and m for the managers.
 
- Join Operation:
- We join the table on the condition that e.managerId = m.id. This pairs each employee with their corresponding manager.
 
- Applying the Filter:
- The WHERE clause ensures that only those records are selected where the employee's salary exceeds that of the manager.
 
- Result Column:
- We select e.name and alias it as Employee for clarity in the result.
 
Python Code
Java Code
Common Mistakes
- Forgetting the Self-Join:
- Some might try to compare salaries within a single instance of the table without joining, which does not work because the manager’s details are in different rows.
 
- Not Handling NULL Manager IDs:
- Ensure that you only consider rows where managerId is not NULL, as these rows represent employees with an actual manager. The join condition naturally filters out employees without a manager since NULL will not match any id.
 
- Using Incorrect Join Type:
- Using a LEFT JOIN instead of an INNER JOIN could inadvertently include employees without a valid manager. An INNER JOIN is preferred because it only returns rows with matching manager records.
 
Edge Cases
- 
Employees without Managers: - Employees who do not have a manager (i.e., their managerId is NULL) will not appear in the join and thus are correctly excluded from the results.
 
- 
Equal Salaries: - If an employee and their manager have equal salaries, the employee should not be included since the problem specifies strictly greater salaries.
 
Alternative Variations
- 
Reporting Additional Information: - The query can be extended to also return the manager’s name along with the employee’s name and salary details.
 
- 
Sorting the Results: - You might also be asked to sort the result based on the employee’s name or salary. Simply add an ORDER BY clause to the query if needed.
 
- 
Handling Ties or Multiple Conditions: - Variations might include additional filters, such as comparing departments or other attributes, which would require modifying the join or filtering conditions accordingly.
 
Related Problems
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78