SQL function
COALESCE() and IFNULL(): replace NULL with a default
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
COALESCE(a, b, …) returns the first argument that isn't NULL: COALESCE(breed, 'mixed') shows 'mixed' where the breed is missing. It works in both databases; SQLite also has IFNULL(a, b) for exactly two arguments, which PostgreSQL doesn't.
Syntax and availability
| PostgreSQL | SQLite | |
|---|---|---|
| Available | COALESCE: yes. IFNULL: no | Both |
| Syntax | COALESCE(value1, value2, …) | COALESCE(value1, value2, …) / IFNULL(value, default) |
Examples
A label for missing breeds
SELECT name, COALESCE(breed, 'mixed') AS breed
FROM pets
ORDER BY id
LIMIT 6;Result (PostgreSQL and SQLite, identical): 6 rows
| name | breed |
|---|---|
| Biscuit | Beagle |
| Mochi | Siamese |
| Pickles | mixed |
| Luna | mixed |
| Waffles | Holland Lop |
| Bruno | Boxer |
Try it: PostgreSQL playground · SQLite playground
The first contact detail that exists
Phone if there is one (blank counts as missing), otherwise the email.
SELECT id, COALESCE(NULLIF(TRIM(phone), ''), LOWER(TRIM(email))) AS contact
FROM adoption_requests
ORDER BY id
LIMIT 6;Result (PostgreSQL and SQLite, identical): 6 rows
| id | contact |
|---|---|
| 1 | 555-0101 |
| 2 | ben@mail.com |
| 3 | ana@mail.com |
| 4 | 555-0104 |
| 5 | 555-0105 |
| 6 | cara@mail.com |
Try it: PostgreSQL playground · SQLite playground
Zero instead of NULL after a LEFT JOIN
Customers who never ordered get a NULL total; COALESCE turns it into 0.
SELECT c.name, COALESCE(SUM(oi.quantity), 0) AS items_bought
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
LEFT JOIN order_items oi ON oi.order_id = o.id
GROUP BY c.id, c.name
ORDER BY items_bought, c.name
LIMIT 5;Result (PostgreSQL and SQLite, identical): 5 rows
| name | items_bought |
|---|---|
| Diego Santos | 0 |
| Nora Lindqvist | 0 |
| Priya Nair | 2 |
| Tomás Herrera | 3 |
| Lena Fischer | 5 |
Try it: PostgreSQL playground · SQLite playground
How it behaves: edge cases, measured
Each expression was run as SELECT … on PostgreSQL 18.6 and SQLite 3.54.1 when this page was built. Nothing here is typed by hand.
| Case | PostgreSQL | SQLite |
|---|---|---|
| First non-NULL | COALESCE(NULL, 'x')'x' | COALESCE(NULL, 'x')'x' |
| Several NULLs first | COALESCE(NULL, NULL, 3)3 | COALESCE(NULL, NULL, 3)3 |
| An empty string isn't NULL | COALESCE('', 'x')'' | COALESCE('', 'x')'' |
| All NULL | COALESCE(NULL, NULL)NULL | COALESCE(NULL, NULL)NULL |
| Mixed types | COALESCE(1, 'a')error: invalid input syntax for type integer: "a" | COALESCE(1, 'a')1 |
| IFNULL | IFNULL(NULL, 0)error: function ifnull(unknown, integer) does not exist | IFNULL(NULL, 0)0 |
Related
Common mistakes
Expecting COALESCE to treat '' as missing: an empty string is a value. Combine with NULLIF:
COALESCE(NULLIF(x, ''), 'none').Mixing types: PostgreSQL wants all arguments to share a type (the matrix shows the error); SQLite returns whatever comes first.
Using IFNULL or ISNULL in PostgreSQL: neither exists there.
PostgreSQL and SQLite
COALESCE is standard and identical in both, except that PostgreSQL checks that the arguments share a type. IFNULL is SQLite's (and MySQL's); PostgreSQL rejects it.
Official documentation
Go deeper
Make it stick in five minutes a day
“Empty, NULL and defaults” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.