SQL QuestSQL Interview Questions › Subqueries & CTEs

Who Manages Nobody

EasyFreeQuerying BasicsSubqueries & CTEs

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_idnamedepartmentpositionsalaryhire_datemanager_idperformance_rating
1Alice JohnsonEngineeringSenior Developer950002019-03-1554.5
2Bob SmithEngineeringDeveloper750002020-06-0113.8
3Carol WilliamsMarketingMarketing Manager850002018-09-20NULL4.2

Expected output: 44 rows — everyone who manages nobody

Hint

The hint for this one spells out most of the query, so it stays in the editor. Reach for 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

A Subquery That Returns One ValueEasy · FreeA Subquery That Returns a Set (IN)Easy · FreeGive a Query a Name (WITH)Easy · FreeMovies Rated Above the AverageEasy · FreeEvery Film by a Director Who Once Hit 8.5Easy · FreeFilter on an Average You Just ComputedEasy · Free

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