# COALESCE

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [cloud-data-platform-full.txt](https://docs.redpanda.com/cloud-data-platform-full.txt)

---
title: COALESCE
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: sql/sql-functions/other-functions/coalesce
page-component-name: cloud-data-platform
page-version: master
page-component-version: master
page-component-title: Cloud
page-relative-src-path: sql/sql-functions/other-functions/coalesce.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/reference/pages/sql/sql-functions/other-functions/coalesce.adoc
description: The `COALESCE` function returns the first non-NULL argument from a list of arguments.
page-topic-type: reference
page-git-created-date: "2026-05-26"
page-git-modified-date: "2026-05-26"
---

<!-- Source: https://docs.redpanda.com/cloud-data-platform/reference/sql/sql-functions/other-functions/coalesce.md -->

The `COALESCE` function returns the first non-NULL argument from a list of arguments. After finding the first non-NULL argument, the function stops evaluating the remaining arguments.

> 📝 **NOTE**
>
> If all arguments are NULL, `COALESCE` returns NULL.

## [](#syntax)Syntax

The syntax for this function is:

```sql
COALESCE (argument_1, argument_2, …);
```

Key points from the syntax:

-   `COALESCE` requires a minimum of two inputs.

-   It can take an unlimited number of arguments.

-   Evaluation occurs sequentially from left to right, stopping at the first non-null value.


## [](#examples)Examples

Here are some examples to illustrate the application of `COALESCE`:

### [](#return-the-first-non-null-value)Return the first non-null value

In this example, a set of values is provided. The `COALESCE` function returns the first non-null value from this set.

```sql
SELECT COALESCE(9, 3, 8, 7, 1);
```

The result will be `9`, the first value without NULL among the provided options.

```sql
 coalesce
----------
        9
```

### [](#handle-null-value-as-the-last-argument)Handle NULL value as the last argument

Include NULL as the final argument and check the query output.

```sql
Select COALESCE(3,4,5,9,10,NULL);
```

The function output is `3` because it returns the first non-null value.

```sql
 coalesce
----------
        3
```

### [](#handle-null-value-as-the-first-argument)Handle NULL value as the first argument

Consider NULL as the first argument in this example:

```sql
Select COALESCE(NULL,1,5,7,9,2);
```

The query returns `1`, as it is the first non-null value of the argument.

```sql
 coalesce
----------
        1
```

### [](#handle-multiple-null-values)Handle multiple NULL values

In this query, NULL appears in the first, second, fourth, and last positions:

```sql
Select COALESCE(NULL, NULL ,3, NULL, 7,9,4,5, NULL);
```

The `COALESCE` function ignores the first two NULLs and returns the first non-null value, `3`. It does not process the subsequent NULL values.

```sql
 coalesce
----------
        3
```

### [](#handle-all-null-values)Handle all NULL values

Assume that the given values are entirely composed of nulls.

```sql
Select COALESCE(NULL, NULL ,NULL, NULL);
```

In this case, the `COALESCE` function returns an empty value (NULL).

```sql
 coalesce
----------
```

### [](#coalesce-with-table-data)`COALESCE` with table data

Consider the `employee_absent` table, which comprises a mix of NULL and non-null values:

```sql
CREATE TABLE employee_absent (
    emp_name TEXT,
    emp_dept TEXT,
    absent TEXT
);

INSERT INTO employee_absent (emp_name, emp_dept, absent)
VALUES
    ('Alice', 'Finance', 'absent'),
    ('Bob', 'Operations', 'absent'),
    ('Carol', 'Finance', 'absent'),
    ('David', 'HR', NULL),
    ('Emily', 'HR', NULL);
```

Use the `SELECT` statement to display all the records:

```sql
SELECT * FROM employee_absent;
```

```sql
 emp_name |  emp_dept  | absent
----------+------------+--------
 Alice    | Finance    | absent
 Bob      | Operations | absent
 Carol    | Finance    | absent
 David    | HR         |
 Emily    | HR         |
```

The following query uses the `COALESCE` function on the `absent` column. It retrieves names and absences (with `out of office` for NULL values) for each employee.

```sql
SELECT emp_name, COALESCE(absent, 'out of office') AS DisplayAbsent FROM employee_absent;
```

```sql
 emp_name | displayabsent
----------+---------------
 Alice    | absent
 Bob      | absent
 Carol    | absent
 David    | out of office
 Emily    | out of office
```

### [](#error-output-in-coalesce)Error output in `COALESCE`

When specifying arguments with different datatypes, they should be convertible.

```sql
Select Coalesce ('x',NULL,1);
```

If the datatypes cannot be converted, the `COALESCE` function generates an error.

```sql
ERROR:  invalid input syntax for type integer: "x"
```