|
| 1 | +/* |
| 2 | +
|
| 3 | +Problem Link -> https://leetcode.com/problems/employees-whose-manager-left-the-company/description/?envType=study-plan-v2&envId=top-sql-50 |
| 4 | +
|
| 5 | +------------------------------------------------------------- QUESTION ----------------------------------------------------------- |
| 6 | +
|
| 7 | +Table: Employees |
| 8 | +
|
| 9 | ++-------------+----------+ |
| 10 | +| Column Name | Type | |
| 11 | ++-------------+----------+ |
| 12 | +| employee_id | int | |
| 13 | +| name | varchar | |
| 14 | +| manager_id | int | |
| 15 | +| salary | int | |
| 16 | ++-------------+----------+ |
| 17 | +In SQL, employee_id is the primary key for this table. |
| 18 | +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). |
| 19 | + |
| 20 | +
|
| 21 | +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. |
| 22 | +Return the result table ordered by employee_id. |
| 23 | +The result format is in the following example. |
| 24 | +
|
| 25 | +
|
| 26 | +Example 1: |
| 27 | +
|
| 28 | +Input: |
| 29 | +Employees table: |
| 30 | ++-------------+-----------+------------+--------+ |
| 31 | +| employee_id | name | manager_id | salary | |
| 32 | ++-------------+-----------+------------+--------+ |
| 33 | +| 3 | Mila | 9 | 60301 | |
| 34 | +| 12 | Antonella | null | 31000 | |
| 35 | +| 13 | Emery | null | 67084 | |
| 36 | +| 1 | Kalel | 11 | 21241 | |
| 37 | +| 9 | Mikaela | null | 50937 | |
| 38 | +| 11 | Joziah | 6 | 28485 | |
| 39 | ++-------------+-----------+------------+--------+ |
| 40 | +
|
| 41 | +Output: |
| 42 | ++-------------+ |
| 43 | +| employee_id | |
| 44 | ++-------------+ |
| 45 | +| 11 | |
| 46 | ++-------------+ |
| 47 | +
|
| 48 | +Explanation: |
| 49 | +The employees with a salary less than $30000 are 1 (Kalel) and 11 (Joziah). |
| 50 | +Kalel's manager is employee 11, who is still in the company (Joziah). |
| 51 | +Joziah's manager is employee 6, who left the company because there is no row for employee 6 as it was deleted. |
| 52 | +
|
| 53 | +*/ |
| 54 | + |
| 55 | +-- ----------------------------------------------------------- SOLUTION ----------------------------------------------------------- |
| 56 | + |
| 57 | +SELECT employee_id |
| 58 | +FROM Employees |
| 59 | +WHERE salary < 30000 |
| 60 | +AND manager_id NOT IN ( |
| 61 | + SELECT employee_id |
| 62 | + FROM Employees |
| 63 | +) |
| 64 | +ORDER BY employee_id |
| 65 | + |
0 commit comments