SQL NOT IN and NULL
If even ONE value in a NOT IN list is NULL, the entire condition silently returns zero rows for every row in the table — one of the most consequential gotchas in all of SQL.
What & Why
NOT IN (list) is shorthand for a chain of <> comparisons joined by AND. If any single value in that list is NULL, one of those <> comparisons becomes UNKNOWN — and per three-valued logic's AND rules from earlier in this series, UNKNOWN AND anything-not-FALSE stays UNKNOWN. The practical result: the whole WHERE clause evaluates to UNKNOWN or FALSE for every single row, and the query returns nothing at all.
See How It Works
Marketing compares an unsafe and a NULL-filtered NOT IN check for campaigns without converted leads.
| id | campaign_id | created_at | qualified_at | converted_at | lead_score | source | country | |
|---|---|---|---|---|---|---|---|---|
| 301 | 1 | ana@example.com | 2024-01-21 09:10:00+00 | 2024-01-22 11:00:00+00 | 2024-02-02 10:00:00+00 | 86 | google_ads | US |
| 302 | 1 | ben@example.com | 2024-01-24 12:40:00+00 | NULL | NULL | 52 | google_ads | CA |
| 303 | 2 | chloe@example.com | 2024-02-16 08:30:00+00 | 2024-02-18 14:20:00+00 | NULL | 74 | GB | |
| 304 | 3 | dev@example.com | 2024-03-20 17:15:00+00 | 2024-03-21 09:00:00+00 | 2024-04-04 16:00:00+00 | 91 | US |
| id | name | channel | spend | start_date | end_date | status | target_segment |
|---|---|---|---|---|---|---|---|
| 1 | Spring Launch | google_ads | 55000.00 | 2024-01-15 | 2024-03-31 | active | smb |
| 2 | Retention Webinar | 45000.00 | 2024-02-10 | 2024-04-15 | active | enterprise | |
| 3 | Finance Retargeting | 50000.00 | 2024-03-12 | 2024-05-31 | active | enterprise | |
| 4 | Enterprise Search | google_ads | 60000.00 | 2024-04-01 | 2024-06-30 | active | enterprise |
| id | name | not_in_result |
|---|---|---|
| 1 | Spring Launch | FALSE |
| 2 | Retention Webinar | TRUE |
| 3 | Finance Retargeting | FALSE |
| 4 | Enterprise Search | TRUE |
After NULL is filtered out, campaigns 2 and 4 are correctly absent from the converted-campaign set.
WITH converted_campaign_ids AS (
SELECT
CASE WHEN converted_at IS NOT NULL THEN campaign_id END AS campaign_id
FROM marketing.leads
)
SELECT
c.id,
c.name,
c.id NOT IN (
SELECT campaign_id FROM converted_campaign_ids
) AS unsafe_not_in,
c.id NOT IN (
SELECT campaign_id
FROM converted_campaign_ids
WHERE campaign_id IS NOT NULL
) AS safe_not_in
FROM marketing.campaigns c
ORDER BY c.id;Practice this concept
Marketing compares unsafe and NULL-filtered NOT IN checks for campaigns without converted leads.
marketingPrefix tables with marketing.table_name.
idnameunsafe_not_insafe_not_inmarketing.campaigns| Column | Type |
|---|---|
| id | integer |
| name | text |
| channel | text |
| spend | numeric |
| start_date | date |
| end_date | date |
| status | text |
| target_segment | text |
| legacy_id | text |
marketing.leads| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| text | |
| created_at | timestamp with time zone |
| qualified_at | timestamp with time zone |
| converted_at | timestamp with time zone |
| lead_score | integer |
| source | text |
| country | text |
| archive_status | text |
Sign up free to try it on a real business scenario