A large group of interview questions asks for something that is missing: customers who never ordered, employees with no sales, products nobody bought, departments with no staff. An inner join cannot answer them, because an inner join only keeps rows that have a match. You need a way to ask "which rows have no partner?". There are three standard ways, and one of them has a famous trap.
Way 1: LEFT JOIN and test for NULL
A LEFT JOIN keeps every row from the left table. When a row has no partner on the right, the right-hand columns are NULL. So if you filter for a NULL on the right, you keep exactly the rows without a partner. We look at the Sales department, where Zoe Park has made no sales.
SELECT e.name
FROM employees e
LEFT JOIN sales s ON s.employee_id = e.id
WHERE e.dept_id = 2
AND s.id IS NULL;
name
Zoe Park
Always test a column that can never be NULL in a real match, such as the primary key s.id. Testing a column like s.amount could wrongly hide a real sale with a NULL amount. Also notice where the department filter sits. e.dept_id = 2 filters the left table, so it belongs in WHERE. A condition on the right table must go into the ON clause. If you put a filter on the right table in WHERE, the NULL rows are removed and your LEFT JOIN quietly turns into an inner join.
Way 2: NOT EXISTS
NOT EXISTS asks, row by row, whether a matching row can be found. It does not care what the subquery selects, only whether something comes back, which is why people write SELECT 1.
SELECT e.name
FROM employees e
WHERE e.dept_id = 2
AND NOT EXISTS (
SELECT 1 FROM sales s WHERE s.employee_id = e.id
);
name
Zoe Park
Same answer. Many people find this version easiest to read aloud: "employees in Sales for whom no sale exists". It is also safe with NULL values, which brings us to the third way.
Way 3: NOT IN, and its NULL trap
NOT IN looks the simplest, and it is the one that fails in a hidden way. Suppose you want the people who manage nobody. You might write: people whose id is not in the list of manager ids.
SELECT COUNT(*) AS people_who_manage_nobody
FROM employees
WHERE id NOT IN (SELECT manager_id FROM employees);
people_who_manage_nobody
0
Zero! But nine people manage nobody. What happened? The manager_id column contains a NULL, because Meera Joshi has no manager. SQL cannot say whether an id is "not equal to NULL", the answer is unknown, and a single unknown makes the whole NOT IN unknown for every row. So no row is returned. The query does not fail and does not warn you. It just returns nothing.
The fix is to remove NULL from the list, or to use NOT EXISTS:
SELECT name
FROM employees
WHERE id NOT IN (
SELECT manager_id FROM employees WHERE manager_id IS NOT NULL
)
ORDER BY name;
name
Chloe Wu
Ella Stone
Jack Reed
Liam Brown
Nia Okoye
Priya Nair
Sara Khan
Tom Hill
Zoe Park
Nine people manage nobody, including Jack Reed, who is new. This one trap is worth knowing by heart: NOT IN plus a subquery that can return NULL gives an empty result. Interviewers ask about it because it separates people who read about SQL from people who used it.
Keeping empty groups when you count
The same idea matters when you count. Which departments have how many people, including departments with nobody? Start from the departments table and LEFT JOIN the employees.
SELECT d.name, COUNT(e.id) AS staff
FROM departments d
LEFT JOIN employees e ON e.dept_id = d.id
GROUP BY d.id
ORDER BY staff DESC, d.name;
name | staff
Engineering | 5
Sales | 4
Finance | 3
HR | 2
Legal | 0
Legal shows 0, which is correct. The count is COUNT(e.id), which counts only the real employee ids. Compare it with the common mistake, COUNT(*):
SELECT d.name, COUNT(*) AS staff
FROM departments d
LEFT JOIN employees e ON e.dept_id = d.id
GROUP BY d.id
ORDER BY staff DESC, d.name;
name | staff
Engineering | 5
Sales | 4
Finance | 3
HR | 2
Legal | 1
Now Legal shows 1. COUNT(*) counts rows, and the join produced one row for Legal, a row full of NULLs. A department with nobody in it looks like it has one person. This is the most common bug in this kind of query, and checking the empty group is the best test you can do.
Which one should you choose?
LEFT JOIN ... IS NULLis common, fast on most engines and easy to extend with more columns.NOT EXISTSis the safest and often the clearest. It handles NULL correctly.NOT INis fine only when the list can never contain NULL, for example when the column is declaredNOT NULL. When unsure, addWHERE column IS NOT NULLinside the subquery.
Your task
- List every department that has no employees, using LEFT JOIN. Then do it again with NOT EXISTS.
- List all employees in any department who have no sales. How many are there?
- Change
COUNT(e.id)toCOUNT(*)in the department count and explain in one sentence why Legal changes. - Write the NOT IN version for employees without sales. Does it work, and why is it still risky?
How to check it worked
- Legal is the only department with no employees.
- Of the 14 employees, 11 have no sales (the 3 sellers are Omar Ali, Priya Nair and Tom Hill).
COUNT(e.id)shows Legal with 0 andCOUNT(*)shows 1.