Skip to content

Commit bb4fc82

Browse files
author
Sanajit Jana
committed
Add new question
1 parent e97f97a commit bb4fc82

File tree

3 files changed

+57
-35
lines changed

3 files changed

+57
-35
lines changed

Easy/0181-employees-earning-more-than-their-managers.sql

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [Employees Earning More Than Their Managers](https://leetcode.com/problems/employees-earning-more-than-their-managers/)
2+
3+
## Question Description
4+
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. Return the list of employees who earn more than their managers.
5+
6+
---
7+
8+
## Constraints
9+
- Each employee has a unique Id
10+
- ManagerId references Id in the same table (self-referencing foreign key)
11+
- Employees without managers have ManagerId as NULL
12+
- Salary values are positive integers
13+
14+
---
15+
16+
## Approach
17+
Use a self-join to compare each employee's salary with their manager's salary. Join the Employee table with itself on the condition that employee's ManagerId equals manager's Id, then filter for cases where employee's salary is greater than manager's salary.
18+
19+
This approach works because we need to compare each employee with their specific manager, and a self-join allows us to create the employee-manager relationship for comparison. Alternative approaches like subqueries would work but self-join is generally more efficient for this type of comparison.
20+
21+
---
22+
23+
## Dry Run
24+
Example Input:
25+
Employee table:
26+
| Id | Name | Salary | ManagerId |
27+
|----|------|--------|-----------|
28+
| 1 | Joe | 70000 | 3 |
29+
| 2 | Henry| 80000 | 4 |
30+
| 3 | Sam | 60000 | NULL |
31+
| 4 | Max | 90000 | NULL |
32+
33+
Step-by-step execution:
34+
- Join Employee e with Employee m where e.ManagerId = m.Id
35+
- This creates pairs: (Joe, Sam) and (Henry, Max)
36+
- Check where e.Salary > m.Salary: 70000 > 60000 is true for Joe
37+
- 80000 > 90000 is false for Henry, so only Joe is included
38+
39+
Final Answer = `Joe`
40+
41+
---
42+
43+
## Solution
44+
```sql
45+
SELECT e.name AS Employee
46+
FROM Employee e
47+
JOIN Employee m ON e.managerId = m.id
48+
WHERE e.salary > m.salary;
49+
```
50+
51+
---
52+
53+
## Time and Space Complexity
54+
- **Time Complexity:** O(n) - single pass through the table to perform the join and filtering
55+
- **Space Complexity:** O(n) - temporary space for the join result before filtering

p/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
0169: Majority Element - [`0169-majority-element/`](0169-majority-element/)
1010

11+
0181: Employees Earning More Than Their Managers - [`0181-employees-earning-more-than-their-managers/`](0181-employees-earning-more-than-their-managers/)
12+
1113
0611: Valid Triangle Number - [`0611-valid-triangle-number/`](0611-valid-triangle-number/)
1214

1315
0966: Vowel Spellchecker - [`0966-vowel-spellchecker/`](0966-vowel-spellchecker/)

0 commit comments

Comments
 (0)