Menu

Why SQL NOT IN returns no rows when the column has NULL
📰

Why SQL NOT IN returns no rows when the column has NULL

DEV Community: database·Real Dev·about 11 hours ago
#N0znVgWc
#devtodatabase#devto#dev#null#enter
Reading 0:00
15s threshold

You write a query to drop a few blocked customers. The blocked list has two real ids and one NULL. You expect everyone else. You get nothing.

That is the NOT IN plus NULL trap. It shows up in interviews and in production reports. Here it is on a small SQLite dataset, with every result taken from a real run on Python 3.13's sqlite3.

The tiny dataset

Five customers. One of them has no city yet.

SELECT id , name , city FROM customers ORDER BY id ;

Enter fullscreen mode

Exit fullscreen mode

1 | Asha | Pune 2 | Ravi | Delhi 3 | Meera | NULL 4 | Kabir | Pune 5 | Neha | Mumbai

Enter fullscreen mode

Exit fullscreen mode

A blocked list.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.