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 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
Finding Orphaned Employees: Visual GuideCurrent Employees1John - $50k11Alice - $28k12Bob - $25kMissing Managers5Manager X6Manager YOrphaned Employees11Alice (Mgr: 5)12Bob (Mgr: 6)reports toorphaned!SQL Solution Process1. Filter Salary < 30k2. LEFT JOINFind Missing Managers3. Return ResultsSELECT e1.employee_id FROM Employees e1LEFT JOIN Employees e2 ON e1.manager_id = e2.employee_idWHERE e1.salary < 30000 AND e2.employee_id IS NULL
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.
Asked in
Amazon 15 Microsoft 12 Meta 8 Google 6
42.0K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen