Learn SQL
-- tables.coalesce_nullif_cast

COALESCE, NULLIF & CAST in SQL

Defaults, guards, and type bridges

--concept

COALESCE returns the first non-NULL — default substitution without overwriting storage. NULLIF(a,b) returns NULL when equal — division guards and sentinel collapsing. CAST(x AS NUMERIC) or x::NUMERIC bridges types (INT→DECIMAL before division). IS DISTINCT FROM is the null-safe <> standard. All nest and compose.

--how it works
1

COALESCE defaults

First non-NULL wins — display/math defaults without rewriting storage.

2

NULLIF guards

NULLIF(x,0) turns zero-division into NULL; collapses sentinels.

3

CAST bridges

CAST/:: converts INT→DECIMAL before division; explicit over implicit conversion.

4

Compare NULL-safe

IS DISTINCT FROM is null-safe <>; = NULL never matches — IS NULL tests.

--visualization
COALESCE(bonus,0): NULL renders 0, storage untouched — COUNT(*) 6 vs COUNT(bonus) 3
namebonus
Alice5000
Bob0
Cynthia0
Stage 1 / 1
NULLIF guards division; CAST bridges INT→DECIMAL; IS DISTINCT FROM compares NULL-safe.
--example
query.sql
1SELECT name, COALESCE(bonus, 0) AS bonus, salary / 12.0 AS monthly FROM employees LIMIT 3;
Result
namebonusmonthly
Alice50007083.33
Bob04333.33
Cynthia07583.33

Bob and Cynthia have NULL bonuses — COALESCE shows 0 without changing storage. Monthly uses decimal division. COUNT(*) stays 6 while COUNT(bonus) is 3 — aggregates skip NULLs.

--variants

Bonus defaults + decimal mean

NULLs shown as 0; means kept decimal.

query.sql
1SELECT name, COALESCE(bonus, 0) AS bonus, salary / 12.0 AS monthly FROM employees LIMIT 2;
Result
namebonusmonthly
Alice50007083.33
Bob04333.33
1Bob NULL bonus renders 0 — storage untouched.
2COUNT(*) 6 vs COUNT(bonus) 3 — aggregates skip NULLs.
3Decimal divisor keeps cents.

Guard + cast + safe compare

Three null/type tools in one row.

query.sql
1SELECT salary / NULLIF(months, 0) AS rate, CAST(salary AS DECIMAL) / 12 AS exact FROM employees LIMIT 1;
Result
rateexact
null7083.33
1Zero months → NULL rate, not error.
2CAST bridges INT to exact decimal.
3IS DISTINCT FROM would compare with NULLs safely.
--key facts
COALESCEfirst non-NULL — defaults
NULLIFequality → NULL — guards
IS DISTINCT FROMnull-safe comparison standard
--use cases
Sparse-column reports: defaults without backfills
Rate math + type-safe migrations
--under the hood
COALESCE/NULLIF are inline expressions — index-neutral in SELECT; in WHERE prefer bare columns.
Implicit conversions can skip indexes — CAST explicitly to matching types.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.