A CASE WHEN expression lets SQL users replace or categorize NULL values by evaluating conditions and returning alternate results.
Handling NULL
values correctly is critical for any data professional. A single unhandled NULL
can distort aggregates, hide data-quality issues, or even break application logic. The most flexible, ANSI-standard way to deal with NULL
s is the CASE WHEN
expression. This article dives deep into how to use CASE WHEN
specifically for NULL
handling, demonstrates cross-vendor syntax, and offers best practices you can apply today in any modern SQL editor—especially Galaxy.
In SQL, NULL
represents an unknown or missing value. Arithmetic with NULL
propagates that unknown state, leading to unexpected results—e.g., 5 + NULL = NULL
. Aggregations like AVG
skip NULL
s entirely. Filtering on equality also behaves differently because NULL = NULL
is not TRUE
; it is UNKNOWN
. An explicit strategy is therefore mandatory in production queries, data pipelines, dashboards, and ELT jobs.
The ANSI-SQL CASE
expression comes in two forms:
CASE WHEN condition THEN value [ ... ] ELSE value END
CASE expression WHEN value THEN result [ ... ] ELSE result END
For NULL
handling we almost always use the searched form, because we need to test with IS NULL
or IS NOT NULL
.
SELECT
customer_id,
CASE WHEN phone IS NULL THEN 'No Phone' ELSE phone END AS phone_clean
FROM dim_customer;
This replaces NULL
with the string 'No Phone'
. Functionally, it is equivalent to COALESCE(phone,'No Phone')
, but CASE WHEN
gives you more room for multiple conditions, as we will see next.
SELECT
order_id,
CASE
WHEN shipped_date IS NULL AND cancelled_date IS NULL THEN 'Pending'
WHEN shipped_date IS NULL AND cancelled_date IS NOT NULL THEN 'Cancelled'
WHEN shipped_date IS NOT NULL THEN 'Shipped'
ELSE 'Unknown'
END AS order_status
FROM fct_orders;
Here we combine NULL
checks with additional predicates to classify orders without creating a nested IF
tree in application code.
Because SUM()
ignores NULL
s, you can count or sum conditional subsets safely inside an aggregate:
SELECT
COUNT(*) AS total_rows,
SUM(CASE WHEN churn_date IS NOT NULL THEN 1 ELSE 0 END) AS churned_users
FROM dim_user;
The CASE
ensures you transform a NULL
into a 0 or 1 so the SUM
behaves predictably.
CASE WHEN col IS NULL THEN 'missing' END
CASE WHEN col IS NULL THEN 'missing' END
CASE WHEN col IS NULL THEN 'missing' END
Because CASE
is ANSI-standard, the syntax is identical across most engines. Differences appear only in helper functions (e.g., ISNULL
in SQL Server), but CASE
remains portable.
ELSE
. Leaving it out silently returns NULL
, which defeats the purpose of the exercise.CASE
has more than ~5 branches, consider refactoring into a dimensional table you can JOIN
on.THEN
and the ELSE
must return the same or coercible data type, or the query will error (or worse, implicitly cast).'No Phone'
vs. ''
) so downstream consumers understand why the NULL
was replaced.NULL
with 0; validate that 0 makes sense in the business context.Because NULL
is not a value, =
comparisons do not work. Always use IS NULL
or IS NOT NULL
.
Leaving off ELSE
returns NULL
when none of the earlier conditions match, often causing silent data loss.
If one branch returns an INTEGER
and another a VARCHAR
, engines like Presto will raise an error or choose an unwanted super-type.
In Galaxy’s desktop SQL editor, you can:
CASE
to insert a template that includes ELSE
by default.NULL
ratios in the schema sidebar—making it obvious where to add CASE WHEN
logic.NULL
shipping dates with ‘Pending’.”NULL
handling.A well-written CASE WHEN
expression is the Swiss-army knife of NULL
handling.
Whether you are producing a KPI dashboard, feeding a machine-learning model, or cleaning data in an ELT pipeline, mastering this pattern prevents silent errors and improves data quality. With Galaxy’s AI-powered editor, writing and sharing these expressions is faster than ever.
Unchecked NULL values can break aggregations, distort metrics, and cause logic errors. Using CASE WHEN gives data engineers granular, portable control to replace or classify NULLs without resorting to vendor-specific functions. Mastering this pattern leads to more reliable analytics and easier cross-database portability.
Use CASE WHEN
when you need multiple conditional branches or non-NULL tests in addition to a simple default. COALESCE
is shorter for single-fallback scenarios.
Yes. CASE expressions are ANSI-SQL compliant and work in PostgreSQL, MySQL, SQL Server, BigQuery, Snowflake, and more with identical syntax.
On most engines the impact is negligible because CASE is evaluated row-by-row during projection. Index use depends on the predicates; wrapping a column in CASE can prevent index usage in WHERE clauses, so perform NULL handling in the SELECT list when possible.
Galaxy’s AI Copilot autocompletes CASE WHEN templates, suggests column names, and can refactor existing queries to add NULL handling. You can then share the vetted query via Collections so teammates reuse the same logic.