-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployees Whose Manager Left the Company.sql
More file actions
38 lines (36 loc) · 1.58 KB
/
Employees Whose Manager Left the Company.sql
File metadata and controls
38 lines (36 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-- Problem Statement
-- ------------------------------------------------------------------
-- Table: Employees
-- +-------------+----------+
-- | Column Name | Type |
-- +-------------+----------+
-- | employee_id | int | -- primary key
-- | name | varchar |
-- | manager_id | int | -- ID of this employee’s manager (nullable)
-- | salary | int |
-- +-------------+----------+
-- • Each row stores an employee’s name, salary, and the ID of their manager.
-- • When a manager leaves the company, their own row is deleted, but the
-- employees who reported to that manager still retain the now-orphaned
-- manager_id.
--
-- Task
-- ----
-- List the IDs of employees who satisfy **both** conditions:
-- 1. Their salary is strictly **less** than 30 000.
-- 2. The manager referenced by their manager_id **no longer exists** in the
-- Employees table.
-- Return the result ordered by employee_id (ascending).
-- Approach
-- ------------------------------------------------------------------
-- • For each employee row, test `salary < 30000`.
-- • To detect a departed manager, check whether `manager_id` is **absent** from
-- the existing set of `employee_id` values.
-- – `manager_id NOT IN (SELECT employee_id FROM Employees)` achieves this.
-- • Select employee_id for rows meeting both criteria and ORDER BY employee_id.
------------------------------------------------------------------
SELECT employee_id
FROM Employees
WHERE salary < 30000
AND manager_id NOT IN (SELECT employee_id FROM Employees)
ORDER BY employee_id;