Handling NULL values effectively is important in database management, and PostgreSQL offers a powerful function called COALESCE to address this issue. The COALESCE function returns the first non-null argument among its parameters, making it particularly useful in SELECT statements.
In this article, we will explain PostgreSQL COALESCE in detail and better understand the syntax, usage, and practical examples of the COALESCE function, helping us master its application in PostgreSQL.
PostgreSQL COALESCEThe COALESCE() function is designed to evaluate a list of arguments and return the first non-null argument it encounters. This makes it an essential tool for dealing with NULL values in our data, allowing for cleaner and more robust queries.
Syntax
COALESCE (argument_1, argument_2, …);Key Features
In this section, we will explain how to utilize the COALESCE() function using practical examples involving a table named items. We will first create the table, insert sample records, and then use a query to calculate the net prices of the products. This will help illustrate how the COALESCE function can effectively handle NULL values in database operations.
1. Creating the Items TableCREATE 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:
Now we insert some records into the items
table using statement as follows:
INSERT INTO items (product, price, discount)Querying the Net Prices
VALUES
('A', 1000, 10),
('B', 1500, 20),
('C', 800, 5),
('D', 500, NULL);
Finally we query the net prices of the products using the following:
SELECT
product,
(price - discount) AS net_price
FROM
items;
Output
Items table Output Example 1: Basic Usage of COALESCETo illustrate the use of the COALESCE function, let’s start with some basic examples.
Query:
SELECT COALESCE(1, 2);
Output
coalesce
----------
1
(1 row)
Explanation:
In this example, both arguments are non-null, so the function returns the first argument, which is 1.
Example 3: Handling NULL ValuesThis example demonstrates how the COALESCE function returns the first non-null argument when handling multiple inputs. It is useful for ensuring that our query always returns a value even when some fields are NULL.
Query:
SELECT COALESCE(NULL, 2, 1);
Output
coalesce
----------
2
(1 row)
Explanation:
In this case, the first argument is NULL, but the second argument is 2, which is returned.
Example 4: Using COALESCE to Handle NULLsIf we look at the fourth row, we 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. To resolve this issue, we can assume that if the discount is NULL, it should be treated as 0. We can use the COALESCE function in our query:
Query:
SELECT
product,
(price - COALESCE(discount, 0)) AS net_price
FROM
items;
Output
Explanation:
Now, the net price of product D is correctly calculated as 500 because the query substitutes 0 for the NULL discount.
Important Points About PostgreSQL COALESCE FunctionThe COALESCE function is a powerful tool for handling NULL values in PostgreSQL. By allowing users to substitute NULLs with meaningful values, COALESCE enhances the readability and robustness of SQL queries. Whether we are using it in simple selections or complex joins, mastering the COALESCE function will greatly improve our database management skills and ensure more reliable data retrieval.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4