sqlstreak

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

PostgreSQLSQLite
AvailableCOALESCE: yes. IFNULL: noBoth
SyntaxCOALESCE(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

namebreed
BiscuitBeagle
MochiSiamese
Picklesmixed
Lunamixed
WafflesHolland Lop
BrunoBoxer

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

idcontact
1555-0101
2ben@mail.com
3ana@mail.com
4555-0104
5555-0105
6cara@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

nameitems_bought
Diego Santos0
Nora Lindqvist0
Priya Nair2
Tomás Herrera3
Lena Fischer5

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.

CasePostgreSQLSQLite
First non-NULLCOALESCE(NULL, 'x')'x'COALESCE(NULL, 'x')'x'
Several NULLs firstCOALESCE(NULL, NULL, 3)3COALESCE(NULL, NULL, 3)3
An empty string isn't NULLCOALESCE('', 'x')''COALESCE('', 'x')''
All NULLCOALESCE(NULL, NULL)NULLCOALESCE(NULL, NULL)NULL
Mixed typesCOALESCE(1, 'a')error: invalid input syntax for type integer: "a"COALESCE(1, 'a')1
IFNULLIFNULL(NULL, 0)error: function ifnull(unknown, integer) does not existIFNULL(NULL, 0)0

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.

Start free