← Back to SQL, page 5

sql-0096 · Hard senior

A DELETE meant to remove customers listed in a blocklist table deleted every customer. The subquery column name was wrong. How can that happen?

If a column named in a subquery does not exist in the subquery's own tables, SQL looks for it in the outer query. WHERE id IN (SELECT id FROM blocklist), when blocklist has customer_ref but no id, silently becomes a correlated subquery comparing customers.id with itself, which is true for every row. Always qualify columns in subqueries with a table alias.

Full answer

Name resolution in subqueries works from the inside out. A column is looked up first in the subquery's FROM tables, then in the enclosing query. That is what makes correlated subqueries possible, and it is also how a typo turns into a disaster.

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);

CREATE TABLE blocklist (customer_ref INTEGER);
INSERT INTO blocklist VALUES (2), (3);

The blocklist stores customer ids in a column called customer_ref. The query uses id:

sql
SELECT COUNT(*) AS would_delete
FROM customers
WHERE id IN (SELECT id FROM blocklist);
would_delete
4

Every customer matches. blocklist has no id column, so id inside the subquery refers to customers.id, the outer row. For each customer the subquery returns that customer's own id once per blocklist row, and id IN (id, id) is true. As a DELETE, this would remove the whole table without any error.

Qualifying the column with the subquery's alias turns the mistake into an error:

sql
SELECT COUNT(*) FROM customers c WHERE c.id IN (SELECT b.id FROM blocklist b);  -- error on purpose
text
Error: no such column: b.id

And the correct query:

sql
SELECT c.id, c.name
FROM customers c
WHERE c.id IN (SELECT b.customer_ref FROM blocklist b)
ORDER BY c.id;
idname
2Bilal
3Chloe

PostgreSQL resolves names the same way, so this is not an SQLite quirk. Protective habits: alias every table in a query with subqueries and qualify every column; run the SELECT version and check the count before running a DELETE or UPDATE; and do destructive changes inside a transaction so a surprising row count can be rolled back.

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);

CREATE TABLE blocklist (customer_ref INTEGER);
INSERT INTO blocklist VALUES (2), (3);

SELECT COUNT(*) AS would_delete
FROM customers
WHERE id IN (SELECT id FROM blocklist);

-- error on purpose
SELECT COUNT(*) FROM customers c WHERE c.id IN (SELECT b.id FROM blocklist b);

SELECT c.id, c.name
FROM customers c
WHERE c.id IN (SELECT b.customer_ref FROM blocklist b)
ORDER BY c.id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Unknown columns in a subquery resolve to the outer query
  • A typo can create an accidental correlated subquery that is always true
  • Qualify subquery columns with aliases to turn typos into errors
  • Preview destructive statements with SELECT COUNT(*) inside a transaction

Common mistakes

  • Writing unqualified columns inside IN subqueries
  • Running a DELETE without checking how many rows it will affect

Follow-ups

  • How would you safely run this DELETE in a transaction and verify it?
  • Does PostgreSQL resolve names the same way?

Tips

  • Explain the inside-out lookup rule; it is the root cause