In today’s data-driven environment, PostgreSQL continues to be a leading choice for robust database management.
Among its arsenal of features, the PostgreSQL COALESCE function stands as a powerful tool for managing null values.
As a database professional, understanding and leveraging the COALESCE function is crucial for effective data
manipulation and achieving accurate query results.
Although PostgreSQL does not have a built-in ISNULL function like SQL Server, it doesn’t mean that PostgreSQL
lacks the capacity to handle null values effectively. Quite the contrary, PostgreSQL has given us the versatile
COALESCE function, and operators like IS NULL or IS NOT NULL, which work impressively to manage null values,
often outperforming their counterparts in other SQL systems.
In this comprehensive guide, we’re going to delve deep into the PostgreSQL COALESCE function, exploring its syntax,
use cases, and practical applications. We’ll also see how it can be used as an alternative to the ISNULL function
found in other SQL environments. Buckle up for a journey into the world of PostgreSQL’s approach to handling null values!
There is no PostgreSQL ISNULL Function
Understanding Null in PostgreSQL
In any database, null signifies the absence of a value. It’s not zero, it’s not an empty string - it’s a distinct
marker representing missing or undefined data. However, handling these nulls can be tricky and lead to unexpected
results if not managed carefully. This is where PostgreSQL’s COALESCEL function can come to our rescue.
It’s important to note that at the date this post was writen, PostgreSQL does not have a built-in ISNULL function,
unlike SQL Server. However, PostgreSQL does provide us with the COALESCE function and IS NULL or IS NOT NULL
operators which can be used to achieve similar results. So, for the sake of this post, when we discuss ISNULL, we’re
referring to these alternatives in PostgreSQL.
PostgreSQL COLAESCE Syntax and Functionality
The COALESCE function in PostgreSQL returns the first non-null argument. It is effectively equivalent to the
ISNULL function in SQL Server. The syntax for COALESCE is as follows:
COALESCE(value1, value2, ..., valueN)
On the other hand, the IS NULL or IS NOT NULL operators test for whether a value is null or not, respectively.
Now let’s see some examples.
Example 1: Basic usage of COALESCE
Suppose we have a products table as follows:
| ProductID | ProductName | SupplierID |
|---|---|---|
| 1 | Apple | 10 |
| 2 | Banana | NULL |
| 3 | Cherry | 20 |
If we want to replace all null SupplierIDs with a default value of 0, we could use COALESCE as follows:
SELECT ProductID, ProductName, COALESCE(SupplierID, 0) AS SupplierID
FROM Products;
This query will return the following:
| ProductID | ProductName | SupplierID |
|---|---|---|
| 1 | Apple | 10 |
| 2 | Banana | 0 |
| 3 | Cherry | 20 |
Example 2: Using IS NULL operator
The IS NULL operator can be used in a WHERE clause to filter for null values. For example, if we wanted to find
all products without a supplier, we could use:
SELECT *
FROM Products
WHERE SupplierID IS NULL;
This would return the products with null SupplierID:
| ProductID | ProductName | SupplierID |
|---|---|---|
| 2 | Banana | NULL |
Example 3: Combining COALESCE with other functions
COALESCE can be combined with other functions for more complex queries. For example, consider the following table of orders:
| OrderID | Quantity | Price |
|---|---|---|
| 1 | 10 | 5 |
| 2 | NULL | 3 |
| 3 | 5 | NULL |
| 4 | NULL | NULL |
Apologies for the abrupt cut-off. Let’s continue where we left off.
Example 4: Combining COALESCE with other functions (continued)
If we wanted to calculate the total price of each order, we might multiply Quantity and Price. However, this could
lead to unexpected results when null values are present. Any arithmetic operation with a null value results in a
null. So, let’s use COALESCE to handle these:
SELECT OrderID, COALESCE(Quantity, 0) * COALESCE(Price, 0) AS TotalPrice
FROM Orders;
This will replace any null values with 0 before performing the multiplication, giving us:
| OrderID | TotalPrice |
|---|---|
| 1 | 50 |
| 2 | 0 |
| 3 | 0 |
| 4 | 0 |
Example 5: Using IS NOT NULL operator
In contrast to the IS NULL operator, the IS NOT NULL operator filters for non-null values. For instance, if we
wanted to find all the orders with both Quantity and Price defined, we could use:
SELECT *
FROM Orders
WHERE Quantity IS NOT NULL AND Price IS NOT NULL;
This would return the orders with non-null Quantity and Price:
| OrderID | Quantity | Price |
|---|---|---|
| 1 | 10 | 5 |
Example 6: Using COALESCE with CASE WHEN
The COALESCE function can also be used in conjunction with CASE WHEN to create more nuanced rules for handling
null values. The CASE WHEN clause allows us to execute conditional logic in SQL. Combined with COALESCE, this
can enable us to assign different default values based on certain conditions.
Consider an extended version of the earlier Products table:
| ProductID | ProductName | SupplierID | CategoryID |
|---|---|---|---|
| 1 | Apple | 10 | 1 |
| 2 | Banana | NULL | 1 |
| 3 | Cherry | 20 | 2 |
| 4 | Date | NULL | 2 |
Let’s say we want to replace null SupplierIDs, but the default value should be 100 for Category 1 and 200 for
Category 2. We can achieve this with COALESCE and CASE WHEN like so:
SELECT
ProductID,
ProductName,
CASE
WHEN CategoryID = 1 THEN COALESCE(SupplierID, 100)
WHEN CategoryID = 2 THEN COALESCE(SupplierID, 200)
ELSE SupplierID
END AS SupplierID,
CategoryID
FROM Products;
This query replaces null SupplierIDs with 100 for products in Category 1, and with 200 for products in Category 2,
providing a conditional default value. If a product’s CategoryID is neither 1 nor 2, SupplierID remains as it is.
The returned result will be:
| ProductID | ProductName | SupplierID | CategoryID |
|---|---|---|---|
| 1 | Apple | 10 | 1 |
| 2 | Banana | 100 | 1 |
| 3 | Cherry | 20 | 2 |
| 4 | Date | 200 | 2 |
This combination of COALESCE with CASE WHEN demonstrates the flexibility and power that PostgreSQL provides when
working with null values.
Example 7: Using COALESCE with CASE WHEN in Complex Queries
The COALESCE function combined with the CASE WHEN statement is a powerful tool for managing data, including
grouping and summing operations in complex queries. In this example, we handle a common situation where null or
empty string values are present in a categorical text column, and we want these to be treated as a specific category.
Consider a table mytable with a text column mycat, which typically contains values ranging from ‘0’ to ‘4’, and
three additional numerical columns col1, col2, col3. Due to an error in the data entry process, there are
occasional null or empty string values in mycat which should be treated the same as ‘0’.
Suppose we have the following data in mytable:
| mycat | col1 | col2 | col3 |
|---|---|---|---|
| ‘0’ | 2 | 1 | 3 |
| ‘1’ | 3 | 1 | 2 |
| ‘2’ | 4 | 2 | 1 |
| ‘3’ | 5 | 1 | 2 |
| ‘4’ | 6 | 2 | 1 |
| '’ | 2 | 1 | 3 |
| ‘0’ | 1 | 2 | 3 |
| NULL | 2 | 3 | 1 |
| ‘1’ | 3 | 2 | 1 |
Here is how to handle this situation using COALESCE with CASE WHEN in a PostgreSQL query:
SELECT my_category,
COALESCE(SUM(col1), 0),
COALESCE(SUM(col2), 0),
COALESCE(SUM(col3), 0)
FROM
(
SELECT CASE COALESCE(mycat ,'0')
WHEN '0' THEN 'ZERO'
WHEN '1' THEN 'ONE'
WHEN '2' THEN 'TWO'
WHEN '3' THEN 'THREE'
WHEN '4' THEN 'OTHER'
ELSE 'ZERO'
END AS my_category,
col1,
col2,
col3
FROM mytable
) T
GROUP BY my_category
ORDER BY my_category;
In this query, COALESCE(mycat ,'0') replaces null or empty string values in mycat with ‘0’. The CASE WHEN
statement then renames these categories to ‘ZERO’, ‘ONE’, ‘TWO’, ‘THREE’, and ‘OTHER’. The resulting categories are
then used to group the summed values of col1, col2, and col3.
The outer SELECT statement then applies COALESCE to these sums to ensure that if a category has no associated
values in col1, col2, or col3, a zero value will be returned instead of null.
The query result will group by the new categories, summing up the values of col1, col2, and col3 for each
category, and ensuring that the null or empty string category is treated as ‘0’, summing and grouping it with the ‘ZERO’ category.
If we run the query provided, here’s what we should expect:
| mycat | COALESCE(SUM(col1), 0) | COALESCE(SUM(col2), 0) | COALESCE(SUM(col3), 0) |
|---|---|---|---|
| ZERO | 7 | 7 | 10 |
| ONE | 6 | 3 | 3 |
| TWO | 4 | 2 | 1 |
| THREE | 5 | 1 | 2 |
| OTHER | 6 | 2 | 1 |
This result reflects that the ’’ and NULL values in mycat have been treated as 0, so their corresponding
col1, col2, and col3 values are summed together with those of 0 under the ZERO category. The remaining
categories (ONE, TWO, THREE, OTHER) are similarly summed.
This example demonstrates how to handle null or empty string values in categorical data in PostgreSQL using
COALESCE and CASE WHEN. It showcases the utility of these functions in data cleaning and management,
particularly in complex grouping and summing operations.
Conclusion
While PostgreSQL does not provide an exact ISNULL function like SQL Server, its robust set of alternatives
(COALESCE, IS NULL, IS NOT NULL) are no less powerful in managing and manipulating null values. Remember,
understanding and effectively handling null values is a crucial aspect of database management and can significantly
improve the reliability and accuracy of your data operations. Happy querying!


Comments