Full answer
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);The query as written:
sql
SELECT name FROM customers WHERE NOT EXISTS (SELECT 1 FROM orders WHERE customer_id = id) ORDER BY id;| name |
|---|
| Ana |
| Bilal |
| Chloe |
| Dev |
SQL resolves an unqualified name in the innermost scope first. orders has an id column, so customer_id = id means orders.customer_id = orders.id, comparing values like 1 and 101. No row satisfies it, the subquery is always empty, and all four customers are returned.
A corrected version:
sql
SELECT c.name FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id) ORDER BY c.id;| name |
|---|
| Dev |
With aliases, the correlation points at the outer row, and only Dev, who has no orders, remains. A related trap is WHERE id IN (SELECT id FROM order_items): order_items has no id, so id silently refers to the outer table and the filter keeps every row. Aliasing every column in subqueries prevents both.
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
SELECT name FROM customers WHERE NOT EXISTS (SELECT 1 FROM orders WHERE customer_id = id) ORDER BY id;
SELECT c.name FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id) ORDER BY c.id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Unqualified names resolve to the innermost table first
- A wrong correlation can make EXISTS always true or false
- Alias and qualify in correlated subqueries
- Missing inner columns silently bind to the outer query
Common mistakes
- Trusting that 'id' means the outer table
- Testing only with data that happens to pass
Follow-ups
- How would a linter catch this?
- Does PostgreSQL resolve names the same way?
Tips
- Explain the scope rule