Skip to content
Sarj Code Standards

no-null-comparison

Use explicit NULL predicates instead of ordinary NULL comparisons.

Why

Ordinary built-in comparisons with NULL produce unknown, so a WHERE or JOIN predicate does not match rows. A scalar NOT IN list containing NULL can never be true. These expressions often silently discard the rows the query intended to retain.

Fix

Use IS NULL or IS NOT NULL to test for missing values, and IS [NOT] DISTINCT FROM for null-safe equality. Remove a literal NULL from a NOT IN list and express the intended null handling separately.

Examples

Before — flagged Ordinary equality does not test for missing values
queries/events.sql
SELECT
id
FROM
event
WHERE
archived_at = NULL;
After — preferred Use predicates with explicit null semantics
queries/events.sql
SELECT
id
FROM
event
WHERE
archived_at IS NULL
AND state IS DISTINCT FROM $1;