SQLite equivalent to ISNULL, NVL, IFNULL or COALESCE
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When you move SQL code between database engines, NULL handling is one of the first places where syntax diverges. SQLite does not implement Oracle's NVL() or SQL Server's ISNULL() function, but it gives you the same capability through IFNULL() and COALESCE().
SQLite Functions That Replace NVL() and ISNULL()
The closest SQLite equivalent depends on how many fallback values you need.
IFNULL(x, y) takes exactly two arguments and returns y when x is NULL. If x is not NULL, it returns x.
COALESCE(x, y, z, ...) is more general. It checks each argument from left to right and returns the first value that is not NULL. This makes it the best replacement when an old query chains several possible values together.
In practice:
- Oracle
NVL(col, 'fallback')maps naturally to SQLiteIFNULL(col, 'fallback') - SQL Server
ISNULL(col, 0)usually maps to SQLiteIFNULL(col, 0) - Standard SQL
COALESCE(col1, col2, col3, 'unknown')works in SQLite as written
If you want the most portable SQL, prefer COALESCE(). It is part of the SQL standard and is widely supported beyond SQLite.
Query Examples
Suppose you have a table where contact details may be missing.
If you only need one fallback value, IFNULL() is concise:
That returns the phone number when it exists and the text 'no phone on file' otherwise.
If you want several fallback choices, use COALESCE():
This query first tries phone, then email, and finally the string 'no direct contact'.
You can also use COALESCE() in calculations:
Without the COALESCE() calls, a single NULL value would make the whole sum evaluate to NULL.
IS NULL Is Not the Same Thing
A common point of confusion is the difference between the IS NULL operator and ISNULL() in other database systems. In SQLite, IS NULL is a condition used for testing whether a value is NULL. It does not replace missing values.
That query filters rows. It does not provide a default string or number. When porting code, keep the distinction clear:
- use
IS NULLto test - use
IFNULL()orCOALESCE()to substitute
Porting Tips from Other Databases
When rewriting existing SQL for SQLite, these translations are usually safe:
If you already have two-argument calls, IFNULL() is readable and idiomatic in SQLite. If your query may grow later or must stay portable across engines, COALESCE() is usually the better long-term choice.
Common Pitfalls
The first pitfall is assuming SQLite supports every vendor-specific function name. It does not. A query using Oracle NVL() or SQL Server ISNULL() will usually need to be rewritten.
The second pitfall is confusing empty strings with NULL. In SQLite, '' is still a real string value, so COALESCE('', 'fallback') returns the empty string, not 'fallback'. If you want blank strings treated as missing, combine NULLIF() with COALESCE().
The third pitfall is mixing incompatible types without noticing. SQLite is flexible about types, but your application code may not be. If one branch returns text and another returns a number, the result may be harder to consume cleanly.
Summary
- SQLite does not provide Oracle
NVL()or SQL ServerISNULL()as function names - '
IFNULL(x, y)is the direct SQLite replacement for two-argument null substitution' - '
COALESCE(x, y, z, ...)returns the first non-NULLvalue and is more portable' - '
IS NULLis only for testing whether a value is missing' - Use
NULLIF()together withCOALESCE()when blank strings should count as missing data
Related reading
- Sqlite File Location Core Data
- Sqlite File Location Core Data
- SQLite Sharing Connections across threads to read and write
- SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects
- start index at 1 for Pandas DataFrame
- Static Indexers?
- steps for making a non-distributed db to distributed db [talking about lmdb specifically]
- storage engine how to quickly find that key is not exist

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.