Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5d578c3

Browse files
Update readme.md
1 parent 7cfa939 commit 5d578c3

File tree

1 file changed

+159
-0
lines changed
  • LeetCode SQL 50 Solution/1978. Employees Whose Manager Left the Company

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
Below is a well-structured `README.md` for **LeetCode 1978 - Employees Whose Manager Left the Company**. It includes a detailed explanation of the SQL solution and a corresponding Python (Pandas) solution.
2+
3+
```md
4+
# 🏢 Employees Whose Manager Left the Company - LeetCode 1978
5+
6+
## 📌 Problem Statement
7+
You are given a table **Employees** that contains information about employees, including their salary and the ID of the manager they report to.
8+
When a manager leaves the company, their row is deleted from the **Employees** table, but the `manager_id` in the records of their reports still remains.
9+
10+
Your task is to **find the IDs of employees** who:
11+
- Have a salary **strictly less than 30000ドル**.
12+
- Have a **manager** (i.e., `manager_id` is not `NULL`) whose record is **missing** in the table (i.e., the manager left the company).
13+
14+
Return the result table **ordered by `employee_id`** in ascending order.
15+
16+
---
17+
18+
## 📊 Table Structure
19+
20+
### **Employees Table**
21+
| Column Name | Type |
22+
| ----------- | ------- |
23+
| employee_id | int |
24+
| name | varchar |
25+
| manager_id | int |
26+
| salary | int |
27+
28+
- `employee_id` is the **primary key**.
29+
- `manager_id` may be `NULL` if an employee does not have a manager.
30+
- When a manager leaves, their row is deleted, but the `manager_id` remains in the reports' records.
31+
32+
---
33+
34+
## 📊 Example 1:
35+
36+
### **Input:**
37+
#### **Employees Table**
38+
| employee_id | name | manager_id | salary |
39+
| ----------- | --------- | ---------- | ------ |
40+
| 3 | Mila | 9 | 60301 |
41+
| 12 | Antonella | NULL | 31000 |
42+
| 13 | Emery | NULL | 67084 |
43+
| 1 | Kalel | 11 | 21241 |
44+
| 9 | Mikaela | NULL | 50937 |
45+
| 11 | Joziah | 6 | 28485 |
46+
47+
### **Output:**
48+
| employee_id |
49+
| ----------- |
50+
| 11 |
51+
52+
### **Explanation:**
53+
- **Employees with salary < 30000ドル:**
54+
- **Kalel (ID 1)** with salary 21241, whose manager is employee 11.
55+
- **Joziah (ID 11)** with salary 28485, whose manager is employee 6.
56+
- **Kalel's manager (ID 11)** is still in the table.
57+
- **Joziah's manager (ID 6)** is missing from the table, meaning that manager left the company.
58+
Thus, only **employee 11 (Joziah)** meets the criteria.
59+
60+
---
61+
62+
## 🖥 SQL Solution
63+
64+
### ✅ **Approach:**
65+
1. **Self-Join:**
66+
- Use a `LEFT JOIN` on the **Employees** table with itself to check if an employee's manager exists.
67+
- Alias `e1` represents the employee, and alias `e2` represents the manager.
68+
2. **Filter Conditions:**
69+
- The employee's `salary` must be strictly less than 30000.
70+
- The employee must have a manager (`e1.manager_id IS NOT NULL`).
71+
- The join should fail for the manager (`e2.employee_id IS NULL`), indicating the manager left.
72+
3. **Order the Result:**
73+
- Order the final result by `employee_id`.
74+
75+
### ✅ **SQL Query:**
76+
```sql
77+
SELECT e1.employee_id
78+
FROM Employees AS e1
79+
LEFT JOIN Employees AS e2 ON e1.manager_id = e2.employee_id
80+
WHERE e1.salary < 30000
81+
AND e1.manager_id IS NOT NULL
82+
AND e2.employee_id IS NULL
83+
ORDER BY e1.employee_id;
84+
```
85+
86+
---
87+
88+
## 🐍 Python (Pandas) Solution
89+
90+
### **Approach:**
91+
1. **Self-Merge:**
92+
- Merge the **Employees** DataFrame with itself on `manager_id` (from the employee side) and `employee_id` (from the manager side) using a left join.
93+
2. **Filter Rows:**
94+
- Keep rows where:
95+
- `salary` is less than 30000.
96+
- `manager_id` is not null.
97+
- The merged manager information is missing (i.e., the manager left).
98+
3. **Sort Result:**
99+
- Sort the result by `employee_id`.
100+
101+
### **Pandas Code:**
102+
```python
103+
import pandas as pd
104+
105+
def employees_with_left_manager(employees: pd.DataFrame) -> pd.DataFrame:
106+
# Perform a left merge on the Employees table to find existing managers
107+
merged = employees.merge(
108+
employees[['employee_id']],
109+
left_on='manager_id',
110+
right_on='employee_id',
111+
how='left',
112+
suffixes=('', '_manager')
113+
)
114+
115+
# Filter: salary < 30000, manager_id is not null, and manager does not exist (NaN in employee_id_manager)
116+
filtered = merged[
117+
(merged['salary'] < 30000) &
118+
(merged['manager_id'].notnull()) &
119+
(merged['employee_id_manager'].isna())
120+
]
121+
122+
# Select the required column and sort by employee_id
123+
result = filtered[['employee_id']].sort_values('employee_id')
124+
return result
125+
126+
# Example usage:
127+
# employees_df = pd.read_csv("employees.csv")
128+
# print(employees_with_left_manager(employees_df))
129+
```
130+
131+
---
132+
133+
## 📁 File Structure
134+
```
135+
📂 Employees-Manager-Left
136+
│── README.md
137+
│── solution.sql
138+
│── solution_pandas.py
139+
│── test_cases.sql
140+
│── sample_data.csv
141+
```
142+
143+
---
144+
145+
## 🔗 Useful Links
146+
- 📖 [LeetCode Problem](https://leetcode.com/problems/employees-whose-manager-left-the-company/)
147+
- 🔍 [MySQL LEFT JOIN Documentation](https://www.w3schools.com/sql/sql_join_left.asp)
148+
- 🐍 [Pandas Merge Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html)
149+
```
150+
151+
---
152+
153+
### Features of this `README.md`:
154+
- **Clear problem description** with table structure and an example.
155+
- **SQL solution** with step-by-step explanation.
156+
- **Python (Pandas) solution** with detailed code and explanation.
157+
- **Organized file structure** and useful external links for further learning.
158+
159+
Let me know if you need any further modifications or additions!

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /