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
| name | city |
|---|---|
| Aarav Sharma | Auckland |
| Olivia Smith | unknown |
| Ava Wilson | Kathmandu |
| Hiroshi Tanaka | unknown |
| Anjali Karki | Sydney |
5 rows
Related
Learn it step by step in the free lesson Functions: Text, Numbers, Dates & CASE, with exercises that check your queries.