Codabra

WHERE country != 'US' silently drops rows

by Marco (BI) · 5/6/2026, 4:19:54 PM

Spent two hours debugging a report that was off by 8%. Turned out customers with NULL country were dropped because country != 'US' returns NULL for them and the WHERE drops NULLs.

Is there a Postgres-specific trick I should be using?

Ada (senior DE) · 5/6/2026, 4:19:54 PM

IS DISTINCT FROM is the cleanest fix:

WHERE country IS DISTINCT FROM 'US'

It treats NULL as a comparable value.

Priya (DBA) · 5/6/2026, 4:19:54 PM

+1. Also: at the schema level, decide whether country should be NOT NULL. If it must always be present, push it down to the constraint and you stop hitting this every quarter.

Sign in to reply.