How to Use PostgreSQL Coalesce Function with Examples. Accelerate your SQL professional career with the PostgreSQL COALESCE post. This post guides experienced SQL professionals/ beginners to expedite their database careers. We have decided to focus more on the core concepts of PostgreSQL functions. In this tutorial, you will be learning one of the important functions of PostgreSQL is COALESCE. Are you excited to explore more on the PostgreSQL COALESCE function? Then let’s get started.
Before getting into the functions of the PostgreSQL concept, we would like to give a quick introduction to the PostgreSQL.
Introduction to the PostgreSQL
PostgreSQL is an open source, advanced, and business driven database management system. PostgreSQL database supports both JSON (javascript object notation) as well as relational database management server language (SQL).
Features of PostgreSQL
PostgreSQL database language is a highly stable one that has a history of more than 20 years.
In recent times, most SQL developers prefer to use PostgreSQL in the development of web and mobile applications.
It is a widely used primary database management system because of its user friendly features.
PostgreSQL is a robust database server so the users can run them on any type of operating system (Windows, Linux, and UNIX, etc.).
It supports many programming languages like Python, Java, C, C++, and .net.
PostgreSQL COALESCE function returns the null as well non functional arguments. The basic syntax of the PostgreSQL COALESCE function is given below;
COALESCE (argument_1, argument_2, …..argument_n);
Use cases for PostgreSQL COALESCE functions
The COALESCE function returns the unlimited (infinite) number of arguments. But it does not return the first argument as a null value. Suppose if all the arguments are null values, then the entire statement will be the “NULL” value.
The COALESCE function first evaluates all the arguments from left to the right until it finds any NULL value, it stops immediately evaluating the remaining arguments.
The COALESCE function offers the same functionalities as“NVL” or “IFNULL” functions used in the SQL system.
Note: MySQL uses the “IFNULL” function whereas SQL uses the “NVL” function.
SELECT
product,
(price_val-discount_val) AS net_price
FROM
items1;
The output is as follows:
Product net_price
A 1980
B 2475
C 2995
D NULL
You can observe that the D value has become NULL, the entire PostgreSQL net_price returns a NULL value. To avoid such a situation, we can use COALESCE function:
SELECT
Product,
(price_val - COALESCE (discount_val, 0)) AS net_price
FROM
items;
After adding COALESCE function, the output is as follows:
Product net_price
A 1980
B 2475
C 2995
D 650
Now you can see the difference the D value has written 650, this is because we have used ZERO instead of a NULL value while calculating the net_price of the product.
Here we can use the PostgreSQL COALESCE function with CASE statement to handle the NULL value. The below query statement is an example of how to use a CASE statement in the COALESCE function:
SELECT
Product,
(
Price -CASE
WHEN discount_val is NULL THEN
0
ELSE
discount_val
END
) AS net_price
FROM
items;
In the above query statement example, if the discount is NULL then use the ZERO value otherwise directly use the discount value in the statement that calculates the product’s net price.
Note: When it comes the performance, both COALESCE and CASE functions are the same. Most developers prefer to use COALESCE function instead of the CASE because the COALESCE function makes the query easier and shorter.
PostgreSQL COALESCE function in Where clause
The PostgreSQL COALESCE function in where clause syntax is as follows:
WHERE COALESCE (arg1, arg2)<> ‘1’
In this case, the COALESCE function first returns the non value data from the given list of the arguments. If you have assigned given arguments as a NULL value then it will return the “0”. In a few scenarios of WHERE clause cannot accept the numeric value “0” so instead of using “0” use “ZERO” while defining the argument lists.
If you want to get the department list, follow the syntax:
WHERE COALESCE (A.dept, B.dept) <> ‘1’ NULL
If you wish not to use COALESCE function, NULL values become equal or unequal when compared to other arguments in the WHERE clause, this is because the NULL value is UNKNOWN.
If you want to fetch only department record lists, then modify the above syntax as follows:
WHERE A.department <>1
If you want to fetch the records from department A which is not NULL or equal to ‘1’. Then go with the following statement:
WHERE COALESCE (A. dept, B.dept) <> ‘1’
If you wish not to use COALESCE or IsNULL value, the following statement is as follows:
WHERE 1 in (A.dept, B.dept)
Now you want to fetch records only from department A and make the department B as NULL, then go with the following statement:
Final Thoughts on using PostgreSQL Coalesce Function
From this PostgreSQL COALESCE function post, we have tried our best to explain the core concepts of COALESCE functions, and how to use them with various conditional statements. We hope this post helps many database professionals to enhance their knowledge of the PostgreSQL functions and methods using a PostgreSQL Server. This post may also be helpful for several SQL communities, and online experts who want to upgrade their SQL skillsets.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.
00votes
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.