A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql/postgresql-coalesce/ below:

PostgreSQL COALESCE - GeeksforGeeks

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 COALESCE

The 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
  1. Unlimited Arguments: We can provide as many arguments as we need to the COALESCE function.
  2. Returns First Non-NULL Value: COALESCE evaluates each argument in the order they are provided and returns the first one that is not NULL.
  3. Returns NULL if All Are NULL: If all arguments are NULL, COALESCE will return NULL.
  4. Short-Circuit Evaluation: Once COALESCE finds a non-NULL value, it stops evaluating the remaining arguments. This can improve performance in certain scenarios.
Examples of PostgreSQL  COALESCE() Function

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 Table
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:

Inserting Data into the Items Table

Now we insert some records into the items table using statement as follows:

INSERT INTO items (product, price, discount)
VALUES
('A', 1000, 10),
('B', 1500, 20),
('C', 800, 5),
('D', 500, NULL);
Querying the Net Prices

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 COALESCE

To 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 Values

This 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 NULLs

If 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 Function Conclusion

The 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