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

# `try_avg` aggregate 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 11.3 LTS and above

Returns the mean calculated from values of a group. If there is an overflow, returns NULL.

## Syntax

```
try_avg( [ALL | DISTINCT] expr) [FILTER ( WHERE cond ) ]
```

This function can also be invoked as a [window function](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) using the `OVER` clause.

## Arguments

* `expr`: An expression that returns a numeric or an interval value.
* `cond`: An optional boolean expression filtering the rows used for aggregation.

## Returns

The result type is computed as for the arguments:

* `DECIMAL(p, s)`: The result type is a` DECIMAL(p + 4, s + 4)`.
  If the maximum precision for DECIMAL is reached the increase in scale will be limited to avoid loss of significant digits.
* year-month interval: The result is an `INTERVAL YEAR TO MONTH`.
* day-time interval: The result is an `INTERVAL YEAR TO SECOND`.
* In all other cases the result is a `DOUBLE`.

Nulls within the group are ignored. If a group is empty or consists only of nulls, the result is NULL.

If `DISTINCT` is specified, the average is computed after duplicates are removed.

To raise an error instead of NULL in case of an overflow use [avg](https://docs.databricks.com/aws/en/sql/language-manual/functions/avg.md).

## Examples

```sql
> SELECT try_avg(col) FROM VALUES (1), (2), (3) AS tab(col);
 2.0

> SELECT try_avg(DISTINCT col) FROM VALUES (1), (1), (2) AS tab(col);
 1.5

> SELECT try_avg(col) FROM VALUES (1), (2), (NULL) AS tab(col);
 1.5

> SELECT try_avg(col) FROM VALUES (INTERVAL '1' YEAR), (INTERVAL '2' YEAR) AS tab(col);
 1-6

-- Overflow results in NULL for try_avg()
> SELECT try_avg(col) FROM VALUES (5e37::DECIMAL(38, 0)), (5e37::DECIMAL(38, 0)) AS tab(col);
 NULL

-- Overflow causes error for avg() in ANSI mode.
> SELECT avg(col) FROM VALUES (5e37::DECIMAL(38, 0)), (5e37::DECIMAL(38, 0)) AS tab(col);
 Error: ARITHMETIC_OVERFLOW
```

## Related

* [`avg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/avg.md)
* [`aggregate` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/aggregate.md)
* [`max` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/max.md)
* [`mean` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/mean.md)
* [`min` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/min.md)
* [`try_sum` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_sum.md)
* [`sum` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/sum.md)
* [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md)
