Skip to content

Commit 52ca376

Browse files
authored
Merge pull request #36 from iamAntimPal/Branch-1
1978. Employees Whose Manager Left the Company
2 parents fba32ed + f727c07 commit 52ca376

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
1978. Employees Whose Manager Left the Company
2+
Solved
3+
Easy
4+
Topics
5+
Companies
6+
SQL Schema
7+
Pandas Schema
8+
Table: Employees
9+
10+
+-------------+----------+
11+
| Column Name | Type |
12+
+-------------+----------+
13+
| employee_id | int |
14+
| name | varchar |
15+
| manager_id | int |
16+
| salary | int |
17+
+-------------+----------+
18+
In SQL, employee_id is the primary key for this table.
19+
This table contains information about the employees, their salary, and the ID of their manager. Some employees do not have a manager (manager_id is null).
20+
21+
22+
Find the IDs of the employees whose salary is strictly less than $30000 and whose manager left the company. When a manager leaves the company, their information is deleted from the Employees table, but the reports still have their manager_id set to the manager that left.
23+
24+
Return the result table ordered by employee_id.
25+
26+
The result format is in the following example.
27+
28+
29+
30+
Example 1:
31+
32+
Input:
33+
Employees table:
34+
+-------------+-----------+------------+--------+
35+
| employee_id | name | manager_id | salary |
36+
+-------------+-----------+------------+--------+
37+
| 3 | Mila | 9 | 60301 |
38+
| 12 | Antonella | null | 31000 |
39+
| 13 | Emery | null | 67084 |
40+
| 1 | Kalel | 11 | 21241 |
41+
| 9 | Mikaela | null | 50937 |
42+
| 11 | Joziah | 6 | 28485 |
43+
+-------------+-----------+------------+--------+
44+
Output:
45+
+-------------+
46+
| employee_id |
47+
+-------------+
48+
| 11 |
49+
+-------------+
50+
51+
Explanation:
52+
The employees with a salary less than $30000 are 1 (Kalel) and 11 (Joziah).
53+
Kalel's manager is employee 11, who is still in the company (Joziah).
54+
Joziah's manager is employee 6, who left the company because there is no row for employee 6 as it was deleted.
55+
56+
57+
58+
# Write your MySQL query statement below
59+
SELECT e1.employee_id
60+
FROM
61+
Employees AS e1
62+
LEFT JOIN Employees AS e2 ON e1.manager_id = e2.employee_id
63+
WHERE e1.salary < 30000 AND e1.manager_id IS NOT NULL AND e2.employee_id IS NULL
64+
ORDER BY 1;
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:
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)