POSTGRESQL

PostgreSQL ISNULL Equivalent: COALESCE, CASE, and NULLIF

intro

Let’s learn everything you need to know about the PostgreSQL ISNULL equivalent approaches for dealing with NULL values.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

If you’re used to databases that uses ISNULL or similar (e.g. SQL Server and others), you might search for “PostgreSQL ISNULL” to figure out how to deal with NULLs in Postgres. The key is: PostgreSQL does not have an ISNULL function. Instead, it relies on the standard SQL COALESCE function together with CASE and NULLIF to handle NULL values.

Let’s learn more about those alternatives!

Checking for NULL in PostgreSQL

Before digging into how to deal with NULLs in Postgres, remember that SQL uses the IS NULL or IS NOT NULL syntax to check if a value is NULL or not in a condition. For example:

Copy
        
1 SELECT * FROM users 2 WHERE email IS NULL;

The above query returns all users with no email.

Note: In SQL, NULL is not “equal” to anything, not even itself, so email = NULL will not work. Thus, you must use IS NULL.

Conversely, IS NOT NULL helps you finds rows where email is present:

Copy
        
1 SELECT * FROM users 2 WHERE email IS NOT NULL;

Now, on to replacing or dealing with NULL in a PostgreSQL result set!

Replacing NULL with a Default Value (COALESCE and CASE)

To replace a NULL with a given “replacement” string, in SQL Server you would end up writing:

Copy
        
1 ISNULL(col, 'replacement')

For this scenario, the PostgreSQL ISNULL equivalent is:

Copy
        
1 COALESCE(col, 'replacement')

The PostgreSQLCOALESCE(value, fallback) function returns the first non-NULL argument. Consider this example:

Copy
        
1 SELECT COALESCE(address, 'N/A') AS address_display 2 FROM customers;

The will output the customer’s address, or 'N/A' if the address is NULL. Verify that by executing the query in a visual PostgreSQL client like DbVisualizer:

Note that the the address_diplay column i N/A when the original address is NULL
Note that the the address_diplay column i N/A when the original address is NULL

That is effectively the same logic as ISNULL in T-SQL. Note that the SQL COALESCE function accepts multiple values if needed (it will return the first that is not NULL among the list).

So, for replacing NULLs in PostgreSQL, use COALESCE. COALESCE is part of standard SQL and works across many DBs (SQL Server, MySQL and Oracle also support COALESCE). That is a straightforward substitute for ISNULL in Postgres:

Copy
        
1 -- SQL Server style (not valid in Postgres): 2 -- SELECT ISNULL(price, 0) FROM products; 3 4 -- PostgreSQL style: 5 SELECT COALESCE(price, 0) FROM products;

If price is NULL, COALESCE returns 0. Otherwise, it will return the actual price.

Note: COALESCE is very efficient and is effectively inlined by the optimizer.

Copy
        
1 SELECT 2 CASE 3 WHEN price IS NULL THEN 0 4 ELSE price 5 END as price_value 6 FROM products;

At the same time, this ISNULL PostgreSQL equivalent approach is more verbose. In a way, COALESCE is basically a shorthand for that pattern.

Does PostgreSQL Have Any ISNULL at All?

  • No, there is no PostgreSQL ISNULL function. If you try SELECT ISNULL(some_column, '-') in Postgres, you will get this error "ERROR: function isnull(unknown, unknown) does not exist".
  • If you see ISNULL in some Postgres context, it might be in procedural languages (PL/pgSQL has an IS NULL check, but not a function).
  • PostgreSQL does have an IS NULL (as explained) for checking nullity.

Other ISNULL Related Functions

  • NULLIF(x, y): This function returns NULL if x = y, otherwise returns x. It is kind of the inverse of COALESCE in a way. For example, NULLIF(status, 'active') will return NULL if status is 'active', otherwise returns the status. This can be used to purposely produce a NULL under certain conditions.
  • COALESCE with multiple arguments: You can provide more than two arguments: COALESCE(col_1, col_2, 'default'). That will return col_1 if is not NULL, else col_2 if it is not NULL, else 'default'.

Example of a NULL Handling Use Case in PostgreSQL

Imagine a query that concatenates a user's first name and nickname. If nickname is null, you want just the first name. In Postgres, you can achieve that with:

Copy
        
1 SELECT first_name || ' "' || COALESCE(nickname, '') || '"' || last_name 2 FROM users;

Here, if nickname is NULL, COALESCE returns empty string '', so you end up with first_name "" last_name. You might adjust formatting to avoid double quotes if nickname is missing (maybe conditionally include them).

Let’s consider another example. This time, you want to sum up values where some may be NULL:

Copy
        
1 SELECT COALESCE(price, 0) + COALESCE(tax, 0) as total_cost 2 FROM purchases;

This ensures if price or tax is NULL, treat it as 0 in the addition operation.

Summary

  • PostgreSQL does not have an ISNULL function.
  • To filter for NULLs in Postgres: use IS NULL / IS NOT NULL in the WHERE clause.
  • To replace NULL with a default value in output or in expressions: use COALESCE(expr, default). That is the direct equivalent to SQL Server’s ISNULL or MySQL’s IFNULL (MySQL uses IFNULL(expr, default) which is similar).
  • Remember that in any database, NULL is special. It means “unknown” or missing. It propagates through expressions (any arithmetic or concatenation with NULL yields NULL, unless handled). So COALESCE is your friend to supply defaults.

By using COALESCE, CASE expressions, or NULLIF, you can effectively handle NULL values in PostgreSQL. Those methods lead to the same outcomes as ISNULL in other systems.

As shown here, dealing with NULL values in PostgreSQL becomes easier with a visual database client like DbVisualizer. This is a fully-featured database client that supports multiple DBMS technologies, offers advanced query optimization capabilities, and can generate ERD-type schemas with a few clicks. Try it for free, and until next time.

Dbvis download link img
About the author
TheTable
TheTable

The Table by DbVisualizer is where we gather together to learn about and simplify the complexity of working with database technologies.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL Boolean Type: How to Use It in All Major Relational Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-09-23
title

Postgres List Schemas: 3 Different Approaches

author Antonello Zanini tags POSTGRESQL 5 min 2025-09-15
title

pgvectorscale: An Extension for Improved Vector Search in Postgres

author Antonello Zanini tags AI POSTGRESQL Vectors 9 min 2025-09-03
title

PostgreSQL Index Mechanism: A Guide to Database Performance Optimization

author Leslie S. Gyamfi tags POSTGRESQL 8 min 2025-09-02
title

UUIDv7 in PostgreSQL 18: What You Need to Know

author Antonello Zanini tags POSTGRESQL 8 min 2025-09-01
title

SQL IS NOT NULL Condition: Definitive Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-13
title

Can a Primary Key Be NULL in an SQL Table?

author TheTable tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 5 min 2025-08-12
title

ISNULL vs COALESCE: Comparing NULL Handling Functions

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-06
title

The SELECT INTO TEMP TABLE Mechanism in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-05
title

PostgreSQL TRUNCATE TABLE Statement: A Guide

author Leslie S. Gyamfi tags POSTGRESQL 6 min 2025-07-30

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.