Joins · Problem 50 of 118

Customers Without Accounts

MediumJoinsBank data

Return customer_id and full_name of every customer who owns no account.

Hint 1

Either LEFT JOIN ... IS NULL or NOT EXISTS works.

Reference solution

SELECT c.customer_id, c.full_name
FROM customers AS c
WHERE NOT EXISTS (SELECT 1 FROM accounts AS a WHERE a.customer_id = c.customer_id);

NOT EXISTS reads naturally ("customers for whom no account exists") and does not break when the inner column contains NULL. In an interview, being able to write the anti-join two ways and explain why NOT IN is dangerous with NULLs is a strong signal.

Another way. The same anti-join written with LEFT JOIN and IS NULL.

SELECT c.customer_id, c.full_name
FROM customers AS c
LEFT JOIN accounts AS a ON a.customer_id = c.customer_id
WHERE a.account_id IS NULL;