PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the COALESCE
function is a valuable tool that allows you to return the first non-NULL value from a list of expressions. If all the expressions evaluate to NULL
, then the COALESCE
function will return NULL
.
COALESCE(expression1, expression2, ..., expressionN)
The function will evaluate the expressions from left to right and return the first non-NULL value it encounters.
Suppose you have a table called products
with columns discount_price
and original_price
. If discount_price
is NULL
for some products, you might want to display original_price
instead.
SELECT product_name, COALESCE(discount_price, original_price) AS sale_price FROM products;
The power of COALESCE
becomes clear when you have multiple fallback values.
SELECT COALESCE(NULL, NULL, 'third value', 'fourth value');
This will return 'third value'
because it's the first non-NULL value in the list.
You can use COALESCE
with other functions to handle NULL values gracefully. For instance, let's say you want to get the length of a string from a column, but it might be NULL:
SELECT COALESCE(LENGTH(column_name), 0) AS string_length FROM table_name;
If column_name
is NULL
, the LENGTH
function would return NULL
, but the COALESCE
function ensures that 0
is returned instead.
COALESCE
is essentially a shorthand for a series of CASE
statements. For example, COALESCE(a, b, c)
is equivalent to:CASE WHEN a IS NOT NULL THEN a WHEN b IS NOT NULL THEN b ELSE c END
COALESCE
can be especially useful when dealing with outer joins, where the joined table might not have a matching row, resulting in NULL
values.
Always ensure that the data types of the expressions provided to COALESCE
are compatible. If they are not, you might encounter errors or unexpected results.
In summary, the COALESCE
function in PostgreSQL is a handy utility for handling NULL
values, allowing you to specify fallback values and ensure more predictable query results.
Using COALESCE for handling NULL values in PostgreSQL:
COALESCE
is used to return the first non-null expression from a list. It's handy for handling NULL values.SELECT COALESCE(my_column, 'Default Value') AS my_result FROM my_table;
COALESCE vs. NULLIF in PostgreSQL:
COALESCE
returns the first non-null expression, NULLIF
compares two expressions and returns null if they are equal.SELECT COALESCE(my_column, 'Default Value') AS my_result FROM my_table;
SELECT NULLIF(my_column, 'Invalid Value') AS my_result FROM my_table;
COALESCE with multiple arguments in PostgreSQL:
COALESCE
can take multiple arguments. It returns the first non-null expression from the list.SELECT COALESCE(column1, column2, column3, 'Fallback Value') AS my_result FROM my_table;
Replacing NULL values with default values using COALESCE in PostgreSQL:
COALESCE
is often used to replace NULL values with default values.SELECT COALESCE(my_column, 'Default Value') AS my_result FROM my_table;
Using COALESCE in SELECT statements in PostgreSQL:
COALESCE
is frequently used in SELECT
statements to handle NULL values gracefully.SELECT column1, COALESCE(column2, 'N/A') AS formatted_column FROM my_table;
COALESCE vs CASE in PostgreSQL:
COALESCE
and CASE
can be used to handle NULL values. COALESCE
is concise for simple cases, while CASE
provides more flexibility.SELECT COALESCE(my_column, 'Default Value') AS my_result FROM my_table;
SELECT CASE WHEN my_column IS NULL THEN 'Default Value' ELSE my_column END AS my_result FROM my_table;
Nested COALESCE functions in PostgreSQL:
COALESCE
functions can be nested to handle multiple levels of potential NULL values.SELECT COALESCE(COALESCE(column1, column2), 'Fallback Value') AS my_result FROM my_table;