SQLite 3 reference · Conditional and Type Functions

SQLite IFNULL() Function

Return the first argument if it isn't NULL, otherwise the second. A two-argument coalesce().

SQLite 3

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

ifnull(X, Y)

Parameters

ParameterDescription
X The value to check.
Y The fallback.

Return value

X if it isn't NULL, otherwise Y.

Examples

Replace NULLTry it yourself
SELECT name, ifnull(city, 'unknown') AS city
FROM customers
WHERE customer_id IN (6, 7);

Output

name | city
Tim Berners-Lee | Geneva
Barbara Liskov | unknown
After a LEFT JOINTry it yourself
SELECT p.name, ifnull(c.name, 'Uncategorised') AS category
FROM products AS p
LEFT JOIN categories AS c USING (category_id)
WHERE p.product_id IN (11, 12);

Output

name | category
Rubber Duck | Toys
Robot Kit | Uncategorised

In other languages

Was this page helpful?