Home / SQL / Reference / Numbers and NULL

SQL COALESCE

Return the first value that is not NULL.

Syntax

COALESCE(value1, value2, …)

The standard way to fill in a default for missing values.

Example

Query (on the shop database used in the SQL course)

SELECT name, COALESCE(city, 'unknown') AS city
FROM customers
WHERE city IS NULL OR name LIKE 'A%';

Result

namecity
Aarav SharmaAuckland
Olivia Smithunknown
Ava WilsonKathmandu
Hiroshi Tanakaunknown
Anjali KarkiSydney

5 rows

Related

Learn it step by step in the free lesson Functions: Text, Numbers, Dates & CASE, with exercises that check your queries.