Employees Whose Manager Left the Company - Problem
Find Employees Whose Manager Left the Company
You're working with a company's HR database and need to identify employees who are in a problematic situation: their salary is less than $30,000 AND their manager has left the company.
The
Your task: Find the employee IDs of all employees who:
1. Earn strictly less than $30,000
2. Have a manager_id that doesn't exist in the current employee table (meaning their manager left)
Return the results ordered by employee_id in ascending order.
Note: Some employees may not have a manager at all (manager_id is null) - these should not be included in the results.
You're working with a company's HR database and need to identify employees who are in a problematic situation: their salary is less than $30,000 AND their manager has left the company.
The
Employees table contains information about all employees including their salary and manager ID. When a manager leaves the company, their record is completely deleted from the table, but their former direct reports still have the departed manager's ID in their manager_id field.Your task: Find the employee IDs of all employees who:
1. Earn strictly less than $30,000
2. Have a manager_id that doesn't exist in the current employee table (meaning their manager left)
Return the results ordered by employee_id in ascending order.
Note: Some employees may not have a manager at all (manager_id is null) - these should not be included in the results.
Input & Output
example_1.sql โ Basic Example
$
Input:
Employees table:
+-------------+---------+------------+--------+
| employee_id | name | manager_id | salary |
+-------------+---------+------------+--------+
| 3 | Mila | 9 | 60301 |
| 12 | Antonella| 9 | 31000 |
| 13 | Emery | 9 | 67084 |
| 1 | Kalel | 11 | 21241 |
| 9 | Mikaela | null | 50937 |
| 11 | Joziah | 6 | 28485 |
+-------------+---------+------------+--------+
โบ
Output:
+-------------+
| employee_id |
+-------------+
| 11 |
+-------------+
๐ก Note:
Employee 11 has salary 28485 < 30000 and manager_id = 6. Manager with ID 6 doesn't exist in the table, so employee 11's manager left the company. Employee 1 also has salary < 30000 but their manager (ID 11) still exists in the table.
example_2.sql โ Multiple Results
$
Input:
Employees table:
+-------------+---------+------------+--------+
| employee_id | name | manager_id | salary |
+-------------+---------+------------+--------+
| 1 | Alice | 5 | 25000 |
| 2 | Bob | 5 | 28000 |
| 3 | Carol | 1 | 35000 |
| 4 | David | 6 | 22000 |
+-------------+---------+------------+--------+
โบ
Output:
+-------------+
| employee_id |
+-------------+
| 1 |
| 2 |
| 4 |
+-------------+
๐ก Note:
Employees 1, 2, and 4 all have salaries < 30000. Employee 1 and 2 have manager_id = 5 which doesn't exist (manager left). Employee 4 has manager_id = 6 which also doesn't exist. Employee 3 has salary โฅ 30000 so is excluded.
example_3.sql โ Edge Cases
$
Input:
Employees table:
+-------------+---------+------------+--------+
| employee_id | name | manager_id | salary |
+-------------+---------+------------+--------+
| 1 | John | null | 25000 |
| 2 | Jane | 1 | 28000 |
| 3 | Mike | 7 | 45000 |
+-------------+---------+------------+--------+
โบ
Output:
+-------------+
| employee_id |
+-------------+
+-------------+
๐ก Note:
No results returned. Employee 1 has salary < 30000 but manager_id is null (no manager). Employee 2 has salary < 30000 and manager_id = 1, but employee 1 exists in the table. Employee 3 has salary โฅ 30000.
Constraints
- 1 โค employee_id โค 104
- 1 โค salary โค 106
- manager_id can be null or reference another employee_id
- employee_id is unique (primary key)
- All salary values are positive integers
Visualization
Tap to expand
Understanding the Visualization
1
Identify Low Earners
Find all employees earning less than $30,000
2
Check Manager Status
For each low earner, verify if their manager still exists in the company
3
Find Orphaned Employees
Return IDs of employees whose managers have left (missing from employee table)
Key Takeaway
๐ฏ Key Insight: LEFT JOIN efficiently identifies missing relationships by producing NULL values where the join condition fails, making it perfect for finding orphaned employees whose managers have left the company.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code