Skip to content
Interview Pages

Interview questions, clearly answered.

Short, careful explanations of common technical interview questions, starting with SQL.

Free PDF: 10 SQL Queries That Run but Give the Wrong Answer

Ten queries that look correct, run without errors, and return the wrong result. For each one: why it looks right, what it actually returns, and how to fix it.

See what's inside

Get the free PDF by email

Sign-up opens soon. This form isn't active yet, so nothing you type here is sent anywhere.

When it opens, we'll use your email only to send the PDF and occasional Interview Pages updates. Unsubscribe any time. See our privacy policy.

A sample trap, in full

NOT IN with a NULL in the subquery

This is trap 1 from the free PDF, in full, with its real output on MySQL 8.4, so you can judge the style before you sign up.

Interview Pages Editorial
Written by a computer-science educator with a PhD and industry software experience

The question

List the employees who manage nobody.

Example data (fictional). Only the two tables this trap needs:

CREATE TABLE departments (
  dept_id INT PRIMARY KEY,
  name    VARCHAR(30) NOT NULL
);
CREATE TABLE employees (
  emp_id     INT PRIMARY KEY,
  name       VARCHAR(30) NOT NULL,
  dept_id    INT NOT NULL,
  manager_id INT NULL,          -- NULL = no manager (top of the company)
  salary     INT NOT NULL,      -- yearly, in rupees
  bonus      INT NULL,          -- NULL = no bonus this year
  FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support');
INSERT INTO employees VALUES
 (1,'Asha', 1,NULL,150000,20000),
 (2,'Ravi', 1,1,    90000,NULL),
 (3,'Meera',1,1,    90000,10000),
 (4,'Kabir',2,1,   150000,NULL),
 (5,'Neha', 2,4,    50000,5000),
 (6,'Arjun',3,1,    40000,NULL),
 (7,'Zoya', 3,6,    45000,NULL);

Asha, Kabir and Arjun manage someone. Ravi, Meera, Neha and Zoya manage nobody.

The query that looks right

SELECT name
FROM employees
WHERE emp_id NOT IN (
  SELECT manager_id FROM employees)
ORDER BY emp_id;

Output on MySQL 8.4:

Empty set

It runs without an error and returns nothing.

Why

The subquery returns every manager_id, and Asha’s is NULL because she has no manager. For Ravi (emp_id 2), NOT IN becomes a chain of <> comparisons, and one of them is 2 <> NULL. Any comparison with NULL is UNKNOWN, not TRUE, so the whole condition can never be TRUE. WHERE keeps only rows where the condition is TRUE, so every row is dropped.

The fix

SELECT e.name
FROM employees e
WHERE NOT EXISTS (
  SELECT 1 FROM employees m
  WHERE m.manager_id = e.emp_id)
ORDER BY e.emp_id;

Output on MySQL 8.4:

+-------+
| name  |
+-------+
| Ravi  |
| Meera |
| Neha  |
| Zoya  |
+-------+

NOT EXISTS asks “is there a row where this person is the manager?”, and a NULL manager_id never matches, so it doesn’t get in the way.

PostgreSQL 16

Same trap, same result: the NOT IN query returns no rows, and the NOT EXISTS fix returns the same four names.

How to spot it

Whenever you see NOT IN (SELECT ...), ask whether that column can be NULL. If you can’t be sure, use NOT EXISTS.