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.

Published 2026-11-23 · Translation: VI

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.

Name SQLite → write three expected names first → result must match → keep the query.

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 NULL elsewhere on the right does not matter).
  • Left value not found, but the right side contains NULL → NOT IN is NULL (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.

With the blank still in mailed, NOT IN returns no row for any of the three. After removing the blank, An stays out; Binh and Chi come back.

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.

Expected: An drop, Binh keep, Chi keep. Run → match → keep. Missing name → discard and start again.

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 IN of an empty list is true for 'Binh' (and for NULL on the left). Insert the blank as a real row to reproduce this bug.
  • NOT (x IN (...)) stays NULL when the matrix says NULL — the outer NOT does not turn it into true.
  • A blank customer can disagree between NOT EXISTS and EXCEPT (compound operators treat two NULLs as equal). This checklist uses three named customers.
  • Use quote() so a SQL NULL is 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.

Share

X Facebook LinkedIn
Educational content only. Rankings reflect stated criteria at the update date—not investment advice or permanent performance guarantees.