Data Engineering

COALESCE: The Data Cleaner's Best Friend

SQL Mastery Team
April 12, 2026
4 min read

It's **Day 81**. We've talked about `NULL` before, but in Phase 6, we're using `COALESCE` as a tool for "Data Enrichment."

The Concept

`COALESCE` takes a list of values and returns the **first one that isn't NULL**.

Real-World Example: User Display Names

User Profiles sometimes have a `nickname`, a `first_name`, and a `username`. You want to display the "best" one available.

SELECT

COALESCE(nickname, first_name, username, 'Anonymous User') as display_name

FROM users;

Why this is great for cleaning

Imagine you're importing data from three different marketing tools. Some have the email, some don't. You can merge them easily:

SELECT

COALESCE(tool_a.email, tool_b.email, tool_c.email) as master_email

FROM ...

The "Zero" Fallback

In financial reports, NULLs are dangerous (Phase 2). Always use `COALESCE(sum(total), 0)` to ensure your report shows `0` instead of a blank space.

Your Task for Today

Create a "Full Name" query that falls back to the user's email address if their name is missing.

*Day 82: Search & Replace Strings in SQL.*

Ready to put your knowledge into practice?

Join SQL Mastery and learn through interactive exercises.