COALESCE is a function that returns the first non-null value from a list of arguments. It is part of the ANSI SQL standard and available in PostgreSQL, MySQL, SQLite, DuckDB, ClickHouse & many other database systems.

Syntax

COALESCE(argument1, argument2, ..., argumentN)
  • Arguments: These are the values checked by COALESCE, which can be columns, expressions, or literals.

The function evaluates arguments in order and returns the first non-null value. If all are null, the result is null.

Example

SELECT COALESCE('A', 'B');       -- returns 'A'
SELECT COALESCE(NULL, 'A', 'B'); -- returns 'A'

COALESCE is often useful for providing a fallback value. Here, if column_name is null, 'Default Value' is returned instead:

SELECT COALESCE(column_name, 'Default Value') FROM table_name;

Get started with DB Pilot

DB Pilot is a Database GUI client and SQL editor for PostgreSQL, MySQL, SQLite, DuckDB & more.