A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

COALESCE and NULLIF: NULL Handling Functions

College Depth 71 in the knowledge graph ☐ I know this ☆ Set as goal
1topic build on this
330prerequisites beneath it
See this on the map →
SQL: SELECT Statement and Basic Queries→→Ranking Functions: ROW_NUMBER, RANK, DENSE_RANK
sql null-handling functions

Core Idea

COALESCE returns the first non-NULL value from a list of arguments, while NULLIF returns NULL if two expressions are equal and otherwise returns the first expression. Both are essential for robust NULL handling in queries.

How It's Best Learned

Practice COALESCE with multiple columns to provide default values, and use NULLIF to convert specific values to NULL for analysis.

Common Misconceptions

COALESCE is not the same as ISNULL/IFNULL—it evaluates multiple columns sequentially. NULLIF only compares two values; use CASE for complex NULL logic.

Explainer

From your work with SELECT basics, you know that NULL represents missing or unknown data — and that NULL behaves strangely in comparisons (NULL = NULL is not true, it is NULL). In real-world databases, NULL values appear constantly: a customer has no middle name, an order has no shipping date yet, a sensor reading was not recorded. COALESCE and NULLIF are the two essential functions for handling these gaps cleanly in your queries.

COALESCE takes a list of expressions and returns the first one that is not NULL. Think of it as a fallback chain. For example, `COALESCE(mobile_phone, home_phone, work_phone, 'No phone on file')` walks through each column in order and returns the first actual value it finds. If all phone columns are NULL, it falls back to the literal string. This is enormously useful for providing default values in reports, merging columns with overlapping data, and preventing NULL from propagating through calculations. Without COALESCE, you would need nested CASE WHEN expressions to achieve the same result — far more verbose and harder to read.

NULLIF does the reverse job: it *creates* NULL values where you want them. `NULLIF(a, b)` returns NULL if a equals b, and returns a otherwise. The classic use case is preventing division-by-zero errors: `revenue / NULLIF(units_sold, 0)` converts a zero denominator to NULL, which makes the division result NULL rather than crashing the query. NULLIF is also useful for cleaning data — converting placeholder values like empty strings or sentinel values (like -1 or 9999) into proper NULLs so that aggregate functions like AVG and COUNT handle them correctly, since aggregates automatically skip NULL values.

The two functions complement each other in a natural workflow: use NULLIF to normalize messy data into proper NULLs, then use COALESCE to substitute meaningful defaults when presenting results. For example, `COALESCE(NULLIF(middle_name, ''), 'N/A')` first converts empty strings to NULL, then replaces NULL with 'N/A'. Mastering this pair gives you precise control over missing data without resorting to verbose conditional logic.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Set Operations: Union, Intersection, and Complement → Relational Algebra → SQL: SELECT Statement and Basic Queries → COALESCE and NULLIF: NULL Handling Functions

Longest path: 72 steps · 330 total prerequisite topics

Prerequisites (1)

Leads To (1)