Skip to content

Commit f727c07

Browse files
Update 1978. Employees Whose Manager Left the Company.sql
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com>
1 parent 3b1d770 commit f727c07

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

LeetCode SQL 50 Solution/1978. Employees Whose Manager Left the Company.sql

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,120 @@ FROM
6363
WHERE e1.salary < 30000 AND e1.manager_id IS NOT NULL AND e2.employee_id IS NULL
6464
ORDER BY 1;
6565

66+
67+
68+
69+
70+
Sure! Let's break down the given MySQL query step by step, explain each part, and analyze its purpose.
71+
72+
---
73+
74+
## **Understanding the Problem Statement**
75+
We want to find **employees** whose:
76+
1. **Salary is less than 30,000.**
77+
2. **Have a manager (i.e., `manager_id IS NOT NULL`).**
78+
3. **Their manager does not exist in the `Employees` table.** (i.e., the `manager_id` they refer to does not match any existing `employee_id`).
79+
80+
---
81+
82+
## **Database Schema**
83+
Assume we have a table called `Employees` with the following structure:
84+
85+
| employee_id | name | salary | manager_id |
86+
|------------|--------|--------|------------|
87+
| 1 | Alice | 50000 | NULL |
88+
| 2 | Bob | 20000 | 1 |
89+
| 3 | Charlie| 25000 | 4 |
90+
| 4 | David | 60000 | NULL |
91+
| 5 | Emma | 27000 | 10 |
92+
93+
- **`employee_id`**: Unique ID for each employee.
94+
- **`name`**: Employee’s name.
95+
- **`salary`**: Salary of the employee.
96+
- **`manager_id`**: The `employee_id` of their manager. `NULL` means the employee has no manager.
97+
98+
---
99+
100+
## **Step-by-Step Explanation of the Query**
101+
```sql
102+
SELECT e1.employee_id
103+
```
104+
- We are selecting the `employee_id` of employees who satisfy the given conditions.
105+
106+
```sql
107+
FROM Employees AS e1
108+
```
109+
- We define the alias `e1` for the `Employees` table to refer to employees.
110+
111+
```sql
112+
LEFT JOIN Employees AS e2 ON e1.manager_id = e2.employee_id
113+
```
114+
- We perform a **LEFT JOIN** to check if the `manager_id` of `e1` exists in the `employee_id` column of another instance of `Employees` (aliased as `e2`).
115+
- If there is **no match**, it means the manager does not exist in the table.
116+
117+
```sql
118+
WHERE e1.salary < 30000
119+
```
120+
- We **filter employees** whose salary is **less than 30,000**.
121+
122+
```sql
123+
AND e1.manager_id IS NOT NULL
124+
```
125+
- Ensures that the employee **has a manager** (`manager_id` should not be `NULL`).
126+
127+
```sql
128+
AND e2.employee_id IS NULL
129+
```
130+
- This is the key condition!
131+
- Since we did a **LEFT JOIN**, `e2.employee_id` will be `NULL` if the manager **does not exist** in the `Employees` table.
132+
133+
```sql
134+
ORDER BY 1;
135+
```
136+
- Orders the result by `employee_id` in ascending order.
137+
138+
---
139+
140+
## **Example Execution**
141+
Using the above `Employees` table, let's analyze the output:
142+
143+
| employee_id | name | salary | manager_id |
144+
|------------|--------|--------|------------|
145+
| 1 | Alice | 50000 | NULL |
146+
| 2 | Bob | 20000 | 1 |
147+
| 3 | Charlie| 25000 | 4 |
148+
| 4 | David | 60000 | NULL |
149+
| 5 | Emma | 27000 | 10 |
150+
151+
### **Step 1: LEFT JOIN**
152+
We join the table on `manager_id = employee_id`:
153+
154+
| e1.employee_id | e1.salary | e1.manager_id | e2.employee_id |
155+
|---------------|----------|--------------|---------------|
156+
| 2 | 20000 | 1 | 1 |
157+
| 3 | 25000 | 4 | 4 |
158+
| 5 | 27000 | 10 | NULL |
159+
160+
### **Step 2: Apply WHERE Conditions**
161+
- `e1.salary < 30000`: Only `Bob`, `Charlie`, and `Emma` qualify.
162+
- `e1.manager_id IS NOT NULL`: No issue.
163+
- `e2.employee_id IS NULL`: Only **Emma (employee_id = 5) qualifies** because `manager_id = 10`, and there is no `employee_id = 10`.
164+
165+
### **Final Output**
166+
| employee_id |
167+
|------------|
168+
| 5 |
169+
170+
So, the result is:
171+
```
172+
5
173+
```
174+
---
175+
## **Summary**
176+
### **Key Takeaways:**
177+
✔ We use **LEFT JOIN** to check if the manager exists.
178+
✔ We filter employees whose **salary is below 30,000**.
179+
✔ We ensure the manager **does not exist** in the database (`e2.employee_id IS NULL`).
180+
✔ The query returns a **list of employees** who meet these conditions.
181+
182+
Would you like me to explain anything further or modify the query for additional scenarios? 😊

0 commit comments

Comments
 (0)