QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Interview tasks

Task: find users who have no accounts at all.

Users with no accounts are found with NOT EXISTS or a LEFT JOIN that checks the right table's key for NULL, not with NOT IN. If the subquery contains a NULL, NOT IN returns zero rows without any error, because a comparison with NULL gives UNKNOWN. A condition on the right side, such as an active account, goes into ON or inside NOT EXISTS, not into WHERE.

SQL · Tasks

The query "users with no account" returns zero rows, yet such users exist. What is wrong?

This is a task about absence: we look for what is missing in another table. The natural thought "take the ids that are not among the accounts" leads to NOT IN. It works while the subquery has no NULL, and on real data with an orphan account that has no owner it returns an empty result without any error. The lesson shows three correct ways and why the fourth is dangerous.

users.idname
1Anna
2Bohdan
3Olha
accounts.iduser_id
101
111
123
13NULL

How it works and why. LEFT JOIN with a NULL check (anti-join): the left table is kept in full, and for a user with no match the account columns are filled with NULL. The filter WHERE a.id IS NULL keeps exactly those rows. The checked column must be one that is never NULL in a real match, i.e. the primary key of the right table. NOT EXISTS asks for each user whether at least one account exists, and handles the three logical values safely. The PostgreSQL planner runs NOT EXISTS as an anti-join. It turns LEFT JOIN … IS NULL into an anti-join when the checked column comes from the ON condition (a.user_id), and since version 19 also a NOT NULL column such as the primary key; on older versions checking a.id leaves an ordinary LEFT JOIN with a filter. On small tables the difference is invisible; on big ones EXPLAIN shows it.

LEFT JOIN keeps everyone, WHERE drops the rest

After LEFT JOIN
u.idnamea.id
1Anna10 dropped
1Anna11 dropped
2BohdanNULL
3Olha12 dropped
After WHERE a.id IS NULL
idname
2Bohdan
Figure 1. A user with no match gets NULL in the account columns, and WHERE finds him by exactly that.
SELECT u.id, u.name
FROM users u
LEFT JOIN accounts a ON a.user_id = u.id
WHERE a.id IS NULL;
-- 2 | Bohdan

SELECT u.id, u.name
FROM users u
WHERE NOT EXISTS (
  SELECT 1 FROM accounts a WHERE a.user_id = u.id
);
-- 2 | Bohdan

Example: why NOT IN stays silent.

SELECT id, name
FROM users
WHERE id NOT IN (SELECT user_id FROM accounts);
-- 0 rows, while Bohdan was expected

SELECT COUNT(*) FROM accounts WHERE user_id IS NULL;
-- 1

Diagnosis: the subquery contains NULL (account 13 with no owner). The condition id NOT IN (1, 1, 3, NULL) expands to id <> 1 AND id <> 1 AND id <> 3 AND id <> NULL. The last term gives UNKNOWN for every id, so the whole expression never becomes true. The fix is NOT EXISTS or LEFT JOIN … IS NULL, or NOT IN with a filtered subquery (WHERE user_id IS NOT NULL). The ownerless account itself is a bug-report finding if user_id was supposed to be required.

Limits and alternatives. If you need users with no active account, the status condition goes into ON (or inside NOT EXISTS), not into WHERE: in WHERE it turns the LEFT JOIN into an ordinary one. Another way is EXCEPT, for example SELECT id FROM users EXCEPT SELECT user_id FROM accounts. It treats NULLs as equal and has no NOT IN trap, but returns only the listed columns.

Common mistakes.

  • NOT IN over a subquery where NULL is possible: an empty result with no error.
  • INNER JOIN instead of LEFT JOIN: users with no match vanish before the filter.
  • Checking for NULL a column that can be NULL even in a real match: false findings.
  • A condition on the account in WHERE: the query stops finding the missing ones.

Check yourself

The query … LEFT JOIN accounts a ON a.user_id = u.id WHERE a.id IS NULL AND a.status = 'active' should find users without an active account. What does it return and why?

Answer

Nothing. For a user with no match a.status is NULL too, and NULL = 'active' gives UNKNOWN, so the row is dropped. Move a.status = 'active' into ON, and the anti-check a.id IS NULL then also finds those who have only closed accounts.

accounts has one row with user_id = NULL. Which form returns the correct result: NOT IN, NOT EXISTS or LEFT JOIN … IS NULL?

Answer

NOT EXISTS and LEFT JOIN … IS NULL are correct: the row with NULL simply matches no user. NOT IN returns zero rows.

In an interview. Junior: I LEFT JOIN users with accounts and take those whose account key is NULL. The alternative is NOT EXISTS. Middle/Senior: I do not use NOT IN: if the subquery returns a NULL, it yields zero rows without an error. Conditions on the right side (for example an active account) go into ON or into NOT EXISTS, not WHERE. An ownerless account is itself logged as a data defect.

More articles in this chapter