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
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 $30000and 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) and11 (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 6as it was deleted.
55
+
56
+
57
+
58
+
# Write your MySQL query statement below
59
+
SELECTe1.employee_id
60
+
FROM
61
+
Employees AS e1
62
+
LEFT JOIN Employees AS e2 ONe1.manager_id=e2.employee_id
63
+
WHEREe1.salary<30000ANDe1.manager_idIS NOT NULLANDe2.employee_id IS NULL
64
+
ORDER BY1;
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