You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: LeetCode SQL 50 Solution/1978. Employees Whose Manager Left the Company.sql
+117Lines changed: 117 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -63,3 +63,120 @@ FROM
63
63
WHEREe1.salary<30000ANDe1.manager_idIS NOT NULLANDe2.employee_id IS NULL
64
64
ORDER BY1;
65
65
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:
0 commit comments