COALESCE Function

SQL · Conditional Expressions

Theory

COALESCE(a, b, c, ...) returns the first argument that is not NULL, reading from left to right. COALESCE(nickname, first_name, 'Unknown') shows the nickname if there is one, otherwise the first name, and 'Unknown' only if both are missing. If every argument is NULL, the result is NULL. It is part of standard SQL and works in every major engine.

Common uses are display defaults, treating missing numbers as zero (COALESCE(discount, 0)), and choosing the best available value from several columns or joined tables. It is shorthand for a CASE WHEN a IS NOT NULL THEN a WHEN b IS NOT NULL THEN b ... END. Arguments should share a compatible type. Engine-specific two-argument versions exist, such as IFNULL (MySQL, SQLite), ISNULL (SQL Server) and NVL (Oracle), but COALESCE is the portable choice.

Why we use it

NULLs make reports show blanks and make arithmetic return NULL (amount + NULL is NULL). COALESCE swaps in a sensible fallback so outputs and calculations stay readable and correct.

When to use it

Use COALESCE to replace NULLs with defaults or pick the first available value. To filter NULL rows out instead of replacing them, use IS NULL / IS NOT NULL. For conditions other than NULL, use CASE.

How it works

  1. Take the first argument
  2. Is it NOT NULL? Yes: Return it and stop. No: Move to the next argument.
  3. Any arguments left? Yes: Check that argument the same way. No: Return NULL.
  4. Result is the first non-NULL value

Syntax

COALESCE(value1, value2, ...)

Example

SELECT COALESCE(nickname, first_name, 'Unknown') FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic