PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Functions / PostgreSQL COALESCE

PostgreSQL COALESCE

Summary: in this tutorial, you will learn about the PostgreSQL COALESCE function that returns the first non-null argument. You will learn how to apply this function in SELECT statement to handle null values effectively.

PostgreSQL COALESCE function syntax

The syntax of the COALESCE function is as follows:

1
COALESCE (argument_1, argument_2, …);

The COALESCE function accepts unlimited number of arguments. It 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 IFNULL function, while Oracle provides NVL function.

See the following examples:

1
2
SELECT
COALESCE (1, 2);

postgresql COALESCE example 1

1
2
SELECT
COALESCE (NULL, 2 , 1);

postgresql COALESCE example 2

We often use the COLAESCE function to substitute a default value for null values when we querying the data. For example, we want to display the excerpt of a blog post, if the excerpt is not provided, we can use the first 150 characters of the of the content of the post. To achieve this, we can use the COALESCE function as follows:

1
2
3
4
SELECT
COALESCE (excerpt, LEFT(CONTENT, 150))
FROM
posts;

PostgreSQL COALESCE example

Let’s take a look at an example of using COALESCE function. First, we create a table named items using CREATE TABLE statement as follows:

1
2
3
4
5
6
CREATE TABLE items (
ID serial PRIMARY KEY,
product VARCHAR (100) NOT NULL,
price NUMERIC NOT NULL,
discount NUMERIC
);

There are four fields in the items table:

  • id: the primary key that identify the item in the items table.
  • product: the product name.
  • price: the price of the product.
  • discount: the discount of the product.

Second, we insert some records into the items table using INSERT statement as follows:

1
2
3
4
5
6
INSERT INTO items (product, price, discount)
VALUES
('A', 1000 ,10),
('B', 1500 ,20),
('C', 800 ,5),
('D', 500, NULL);

Third, we query the net prices of the products using the following formula:

1
net_price = price - discount;

1
2
3
4
5
SELECT
product,
(price - discount) AS net_price
FROM
items;

postgresql COALESCE example 3

If you look at the fourth row, you will notice that the net price of the product D is null which seems not correct. The issue is the discount of the product D is null, therefore when we take the null value to calculate the net price, PostgreSQL returns null.

The get the right price, we need to assume that if the discount is null, it is zero. Then we can use the COALESCE function as follows:

1
2
3
4
5
SELECT
product,
(price - COALESCE(discount,0)) AS net_price
FROM
items;

postgresql COALESCE substitute null values

Now the net price of product D is 500 because we use zero instead of null value when we calculated the net price.

Besides using the COALESCE function, you can use the CASE expression to handle the null values in this case. See the following query that uses the CASE expression to achieve the same result above.

1
2
3
4
5
6
7
8
9
10
11
12
SELECT
product,
(
price - CASE
WHEN discount IS NULL THEN
0
ELSE
discount
END
) AS net_price
FROM
items;

In the query above we say if the discount is null then use zero (0) otherwise use discount value to in the expression that calculate the net price.

In terms of performance, COALESCE function and CASE expression are the same. We prefer COALESCE function than CASE expression because COALESCE function makes the query shorter and easier to read.

In this tutorial, you have learned how to use the COALESCE function to substitute null values in the query.

Related Tutorials

  • PostgreSQL NULLIF
Previous Tutorial: PostgreSQL CASE
Next Tutorial: PostgreSQL NULLIF

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Aggregate Functions

  • PostgreSQL COUNT Function
  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function

PostgreSQL Conditional Expressions

  • PostgreSQL CASE
  • PostgreSQL NULLIF
  • PostgreSQL COALESCE

PostgreSQL String functions

  • PostgreSQL CONCAT Function
  • PostgreSQL TRIM Function
  • PostgreSQL LENGTH Function
  • PostgreSQL REPLACE
  • PostgreSQL Substring
  • PostgreSQL Letter Case Functions

PostgreSQL Operators

  • PostgreSQL CAST

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2016 by PostgreSQL Tutorial Website. All Rights Reserved.