---
title: coalesce function
description: Learn the syntax of the coalesce function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/coalesce
---

# `coalesce` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Returns the first non-null argument.

## Syntax

```
coalesce(expr1 [, ...] )
```

## Arguments

* `exprN`: Any expression that shares a [least common type](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatype-rules.md#least-common-type-resolution) across all `exprN`.

## Returns

The result type is the [least common type](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatype-rules.md#least-common-type-resolution) of the arguments.

There must be at least one argument.
Unlike for regular functions where all arguments are evaluated before invoking the function, `coalesce`
evaluates arguments left to right until a non-null value is found.
If all arguments are `NULL`, the result is `NULL`.

Special considerations apply to `VARIANT` types. See [`isnull` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/isnull.md) for details.

## Examples

```sql
> SELECT coalesce(NULL, 1, NULL);
 1

-- The following example raises a runtime error because the second argument is evaluated.
>  SELECT coalesce(NULL, 5 / 0);
 Error: DIVISION_BY_ZERO

-- The following example raises no runtime error because the second argument is not evaluated.
> SELECT coalesce(2, 5 / 0);
 2

> SELECT coalesce(NULL, 'hello');
 hello
```

## Related

* [`nvl` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/nvl.md)
* [`nvl2` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/nvl2.md)
* [SQL data type rules](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatype-rules.md)
