SQL 50, decoded
0 of 50 solved/50

1978. Employees Whose Manager Left the Company

SQL 50 · Subqueries (day 6). Decode it with the hints, solve it on LeetCode, then compare with the reference solution in MySQLPostgreSQL.

EasySolve it on LeetCode ↗

Your work

Hints

Open one at a time. Try to finish with as few as you can.

1. What does one output row represent?
One employee.
2. Approach
salary < 30000 AND manager_id NOT IN (SELECT employee_id …), ORDER BY employee_id.
3. The trap
Rows where manager_id is NULL have no manager to leave; NOT IN already drops them.

Reference solution

Tested on MySQL 8.0 and PostgreSQL 16. Switch dialect at the top of the page.

Show the MySQLPostgreSQL solution
SELECT employee_id
FROM Employees
WHERE salary < 30000
  AND manager_id NOT IN (SELECT employee_id FROM Employees)
ORDER BY employee_id;

Patterns used

Toolbox