COALESCE

Learn how to use the PostgreSQL COALESCE function to return the first non-null value from a list of arguments, replacing NULLs with meaningful defaults.

4 min read · Last updated: March 2026 · Back to overview

Quick Answer

COALESCE(arg1, arg2, ...) returns the first non-null argument in the list. If all arguments are null, it returns null. It is the standard way to substitute a default value when a column or expression might be null.

Spin up a Postgres database in 20 seconds with Vela.

Try Vela Sandbox

The COALESCE() function accepts any number of arguments and returns the first one that is not null. It evaluates arguments left to right and stops as soon as it finds a non-null value—remaining arguments are not evaluated. This makes it the idiomatic way to provide fallback values for nullable columns.

COALESCE syntax and basic examples

COALESCE(argument_1, argument_2, ...);

Simple scalar examples:

SELECT COALESCE(1, 2);       -- returns 1 (first non-null)
SELECT COALESCE(NULL, 2, 1); -- returns 2 (skips null, returns next non-null)

Output of the second query:

 coalesce
----------
        2

Substituting defaults in table queries

A classic use case: calculate net price when the discount column can be null.

CREATE TABLE items (
  id       SERIAL PRIMARY KEY,
  product  VARCHAR(100) NOT NULL,
  price    NUMERIC NOT NULL,
  discount NUMERIC       -- nullable
);

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

SELECT
  product,
  price - COALESCE(discount, 0) AS net_price
FROM items;

Output:

 product | net_price
---------+-----------
 A       |       990
 B       |      1480
 C       |       795
 D       |       500  -- discount treated as 0 instead of NULL

Without COALESCE, any arithmetic involving NULL propagates as NULL—product D's net price would show as null.

COALESCE with expressions as fallbacks

COALESCE arguments can be arbitrary expressions, not just constants. For example, fall back to the first 150 characters of body text when an excerpt is missing:

SELECT
  title,
  COALESCE(excerpt, LEFT(body, 150)) AS display_excerpt
FROM posts;

Combine with NULLIF to treat empty strings the same as null:

SELECT
  title,
  COALESCE(NULLIF(excerpt, ''), LEFT(body, 40)) AS display_excerpt
FROM posts;

Practical tips

  • COALESCE is the SQL-standard equivalent of IFNULL (MySQL) and NVL (Oracle). Prefer it for portability.
  • Arguments after the first non-null value are never evaluated—this matters if later arguments have side effects or are expensive subqueries.
  • All arguments must be of compatible types; PostgreSQL will raise an error if the types cannot be unified.
  • Using COALESCE in a WHERE clause can prevent index scans. Consider rewriting the condition as col IS NULL OR col = default when performance is critical.

Continue in Conditional Expressions & Operators: ISNULL.

Related in this section: CASE · ISNULL · NULLIF

Frequently Asked Questions

What does COALESCE do in PostgreSQL?

COALESCE returns the first non-null value from its argument list. It is commonly used to replace NULLs with a default value in SELECT output or calculations.

What is the difference between COALESCE and NULLIF?

COALESCE returns the first non-null argument and is used to provide defaults for null values. NULLIF returns null if two arguments are equal, and is used to convert a specific value (like an empty string) into null so COALESCE can then substitute a fallback.

Can COALESCE accept more than two arguments?

Yes. COALESCE accepts any number of arguments and returns the first non-null one. This is useful for multi-level fallback chains, such as COALESCE(preferred_phone, mobile_phone, work_phone, 'N/A').

Is COALESCE evaluated lazily?

Yes. PostgreSQL stops evaluating arguments as soon as it finds the first non-null value. Arguments to the right of the first non-null argument are not evaluated.

How does COALESCE differ from CASE?

COALESCE(a, b) is equivalent to CASE WHEN a IS NOT NULL THEN a ELSE b END. COALESCE is more concise for null-substitution. Use CASE when you need conditions beyond simple null checks.