PostgreSQL COALESCE
Summary: in this tutorial, you will learn about the PostgreSQL COALESCE()
function that returns the first non-null argument.
PostgreSQL COALESCE function syntax
The COALESCE()
function accepts a list of arguments and returns the first non-null argument.
Here’s the basic syntax of the COALESCE()
function:
The COALESCE()
function accepts multiple arguments and returns the first argument that is not null. If all arguments are null, the COALESCE()
function will return null.
The COALESCE()
function evaluates arguments from left to right until it finds the first non-null argument. All the remaining arguments from the first non-null argument are not evaluated.
The COALESCE
function provides the same functionality as NVL
or IFNULL
function provided by SQL standard. MySQL has the IFNULL function whereas Oracle Database offers the NVL
function.
PostgreSQL COALESCE() Function examples
Let’s take some examples of using the COALESCE()
function.
1) Basic PostgreSQL COALESCE() function examples
The following example uses the COALESCE()
function to return the first non-null argument:
Since both arguments are non-null, the function returns the first argument:
The following example uses the COALESCE()
function to return the first non-null argument:
Because the first argument is NULL and the second argument is non-null, the function returns the second argument:
In practice, you often use the COLAESCE()
function to substitute a default value for null when querying data from nullable columns.
For example, if you want to display the excerpt from a blog post and the excerpt is not provided, you can use the first 150 characters of the content of the post.
To achieve this, you can use the COALESCE
function as follows:
2) Using the COALESCE() function with table data
First, create a table called items
:
The items
table has four columns:
id
: the primary key that identifies the item in theitems
table.product
: the product name.price
: the price of the product.discount
: the discount on the product.
Second, insert some rows into the items
table:
Third, retrieve the net prices of the products from the items
table:
Output:
The output indicates that the net price of the product D
is null.
The issue is that the discount
of the product D
is null. Therefore, the net price is NULL because it involves NULL in the calculation.
With an assumption that if the discount is null, the net price is zero, you can use the COALESCE()
function in the query as follows:
Output:
Now the net price of the product D
is 500
because the query uses zero instead of NULL when calculating the net price.
Besides using the COALESCE()
function, you can use the CASE expression to handle the NULL in this example.
For example, the following query uses the CASE
expression to achieve the same result:
In this query, if the discount is null then use zero (0) otherwise use the discount value to calculate the net price.
In terms of performance, the COALESCE()
function and CASE
expression are the same.
It is recommended to use COALESCE()
function because it makes the query shorter and easier to read.
Summary
- Use the
COALESCE()
function to substitute null values in the query.