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

# `mean` 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

Returns the mean calculated from values of a group. This function is a synonym for [`avg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/avg.md).

## Syntax

```
mean ( [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 evaluates to a numeric.
* `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 is 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 DAY 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 mean is computed after duplicates have been removed.

> [!WARNING]
>
> In Databricks Runtime, if [spark.sql.ansi.enabled](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-ansi-compliance.md) is `false`, an overflow returns `NULL` instead of an error.

## Common error conditions

* [ARITHMETIC\_OVERFLOW](https://docs.databricks.com/aws/en/error-messages/arithmetic-overflow-error-class.md)

## Examples

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

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

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

> SELECT mean(c1) FROM VALUES(5e307::DOUBLE), (5e307::DOUBLE) AS tab(c1);
  Error: ARITHMETIC_OVERFLOW
```

## Related functions

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