Test an AI SQL NOT IN on a table that contains one NULL
Mark An as drop, Binh and Chi as keep. Keep a NOT IN query only when the run returns those names — a blank mailed row often drops everyone.
In short
- Name the engine. This guide is SQLite (expression page §8 — IN / NOT IN matrix).
- Write three expected results before you read the reply: An does not come back; Binh and Chi do. The mailed list already holds one blank (
NULL). - Keep the chat’s query only when the names returned are Binh and Chi (An absent).
Example (made up): customers An, Binh, Chi. Mailed table: An, Bo, and a blank. A chat often suggests NOT IN for “customers we have not mailed.” You still check the three names yourself.
Same habit as testing a cron line on clocks you wrote down.

Bo is on the mailed list but is not a customer. The blank row is what breaks a naive NOT IN.
Why NOT IN + NULL drops rows
SQLite’s matrix (checked 23 November 2026, SQLite 3.45.1):
- Left value found on the right → NOT IN is false (a
NULLelsewhere on the right does not matter). - Left value not found, but the right side contains
NULL→ NOT IN isNULL(not true). - SELECT §2.3: WHERE drops rows when the expression is false or
NULL.
So this query returned no rows:
CREATE TABLE customers(name TEXT);
INSERT INTO customers(name) VALUES ('An'), ('Binh'), ('Chi');
CREATE TABLE mailed(name TEXT);
INSERT INTO mailed(name) VALUES ('An'), ('Bo'), (NULL);
SELECT name FROM customers
WHERE name NOT IN (SELECT name FROM mailed);
Beside each name:
| name | quote(name NOT IN (SELECT name FROM mailed)) |
|---|---|
| An | 0 |
| Binh | NULL |
| Chi | NULL |
An is correctly excluded (found). Binh and Chi become NULL because of the blank — WHERE drops them too.

0 and NULL look the same in an empty result — that is why you mark expected names first.
Two queries that return Binh and Chi
Filter blanks out of the subquery:
SELECT name FROM customers
WHERE name NOT IN (SELECT name FROM mailed WHERE name IS NOT NULL);
Or use NOT EXISTS (equality never matches NULL):
SELECT name FROM customers AS c
WHERE NOT EXISTS (
SELECT 1 FROM mailed AS m WHERE m.name = c.name
);
Both returned Binh, then Chi on this database.
Ask the chat, then run it
SQLite only. I already inserted these rows.
customers.name: 'An', 'Binh', 'Chi'
mailed.name: 'An', 'Bo', NULL
Before you answer, say what this returns, and do not change the rows:
SELECT name FROM customers WHERE name NOT IN (SELECT name FROM mailed);
Expected: An does not come back. Binh comes back. Chi comes back.
Then give one query. When the right side of NOT IN contains NULL, and the left value is not found, NOT IN itself is NULL, and WHERE drops that row.
Keep the reply only when the names are Binh and Chi and An is absent.

Only Binh, or a result that includes An, fails the same way an empty result does.
Watch-outs
- Empty mailed table is a different case.
NOT INof an empty list is true for'Binh'(and forNULLon the left). Insert the blank as a real row to reproduce this bug. NOT (x IN (...))stays NULL when the matrix says NULL — the outerNOTdoes not turn it into true.- A blank customer can disagree between
NOT EXISTSandEXCEPT(compound operators treat two NULLs as equal). This checklist uses three named customers. - Use
quote()so a SQLNULLis not mistaken for an empty name in the client. - Keep live customer data out of the chat. Insert the three rows on your own database.
Related
Share