SQLite coalesce() Function

Summary: in this tutorial, you will learn how to use the SQLite coalesce() function to handle null values.

Introduction to the SQLite coalesce() function

The coalesce() function accepts two or more arguments and returns the first non-null argument.

Here’s the syntax of the coalesce() function:

coalesce(parameter1,parameter2, …);Code language: SQL (Structured Query Language) (sql)

If all the arguments are NULL, the coalesce function returns NULL.

Please note that the coalesce() function is equivalent to the NVL function in Oracle or the IFNULL function in MySQL.

SQLite coalesce() function examples

Let’s take some examples of using the coalesce() function.

1) Basic SQLite coalesce() function examples

The following query uses the coalesce() function to return the first non-null value, which is 10:

SELECT COALESCE(10,20); -- return 10Code language: JavaScript (javascript)

The following statement also uses the coalesce() function to return the first non-null argument, which is 20:

SELECT COALESCE(NULL,20,10); -- returns 20Code language: PHP (php)

2) Using coalesce() function to substitute NULL

We’ll use the following customers table in the sample database: