SQL Practice Logo

SQLPractice Online

NULL-Safe Filtering: Concept

Module: Advanced Filtering

NULL represents unknown/missing data. NULL comparisons return NULL (not true/false), requiring special handling with IS NULL and IS NOT NULL.

**NULL Comparison Rules:**

- NULL = NULL returns NULL (not true!)

- NULL != value returns NULL (not true!)

- NULL > value returns NULL

- NULL AND true returns NULL

- NULL OR true returns true

**Correct NULL Checks:**

- IS NULL: Tests if value is NULL

- IS NOT NULL: Tests if value is not NULL

- COALESCE(column, default): Replaces NULL with default

**Common Mistakes:**

- WHERE column = NULL (wrong! use IS NULL)

- WHERE column != NULL (wrong! use IS NOT NULL)

- NOT IN with NULL (returns no rows)

**NULL-Safe Patterns:**

- Use IS NULL / IS NOT NULL explicitly

- Use COALESCE for default values

- Filter NULL before NOT IN

- Use EXISTS instead of IN for NULL safety

Critical for data quality and correctness. NULL mishandling is a top cause of bugs in production queries.

NULL-safe filtering prevents bugs: find records with missing data, handle optional fields, validate data completeness, filter nullable columns correctly.