Example 1
Exclude IDs from a list that contains NULL
Alice is excluded because her ID is present in the blacklist. Bob and Carol remain. The critical detail is WHERE customer_id IS NOT NULL inside the subquery. Without that filter, the NULL in blacklist would make NOT IN behave unexpectedly and could produce no rows at all.
CREATE TABLE customers (id INT, name VARCHAR(50));
CREATE TABLE blacklist (customer_id INT);
INSERT INTO
customers (id, name)
VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Carol');
INSERT INTO
blacklist (customer_id)
VALUES
(1),
(NULL);SELECT
name
FROM
customers
WHERE
id NOT IN (
SELECT
customer_id
FROM
blacklist
WHERE
customer_id IS NOT NULL
)
ORDER BY
id;| name |
|---|
| Bob |
| Carol |
The example filters out `NULL` inside the subquery to keep `NOT IN` trustworthy.