SQL Quest › SQL Interview Questions › Subqueries & CTEs
Who Manages Nobody
HR is drawing up an individual-contributor list: everyone who is nobody's manager. A person manages someone when their emp_id appears in some other row's manager_id.
Show exactly these 3 columns, in this order: name, department, position. Order by department ascending, then name ascending.
NOT EXISTS is how you ask this. It takes a correlated subquery — one that mentions the outer row — and keeps the row when that subquery finds nothing. What the subquery *selects* is irrelevant, which is why SELECT 1 is the convention: the question is only whether a matching row exists.
Now the part worth the card. The obvious alternative looks equivalent:
WHERE e.emp_id NOT IN (SELECT manager_id FROM employees)
Run it. It returns zero rows — not an error, not a warning, just nothing. Seven employees have no manager, so manager_id is NULL for them, and NOT IN against a list containing NULL can never be true: SQL cannot promise your id differs from a value it does not know. NOT EXISTS compares row by row and is unbothered.
This is the single most common way a correct-looking anti-join returns a silently empty answer. [The anti-join explained](/blog/sql-anti-join/) has the long version.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
employees
| emp_id | name | department | position | salary | hire_date | manager_id | performance_rating |
|---|---|---|---|---|---|---|---|
| 1 | Alice Johnson | Engineering | Senior Developer | 95000 | 2019-03-15 | 5 | 4.5 |
| 2 | Bob Smith | Engineering | Developer | 75000 | 2020-06-01 | 1 | 3.8 |
| 3 | Carol Williams | Marketing | Marketing Manager | 85000 | 2018-09-20 | NULL | 4.2 |
Expected output: 44 rows — everyone who manages nobody
Hint
SELECT, Subquery, EXISTS, and open the hint there if you stall.Concepts
SELECT Subquery EXISTS Correlated Subquery WHERE
Practise the topic: SQL practice questions · CTE practice
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test