> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-parallel-read-in-order-multi-part.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Calculates the arithmetic mean.

# avg

<h2 id="avg">
  avg
</h2>

Introduced in: v1.1.0

Calculates the arithmetic mean.

**Syntax**

```sql theme={null}
avg(x)
```

**Arguments**

* `x` — Input values. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float) or [`Decimal`](/reference/data-types/decimal) or [`Date`](/reference/data-types/date) or [`Date32`](/reference/data-types/date32) or [`DateTime`](/reference/data-types/datetime) or [`DateTime64(P)`](/reference/data-types/datetime64) or [`Time`](/reference/data-types/time) or [`Time64(P)`](/reference/data-types/time64) or [`Interval`](/reference/data-types/special-data-types/interval)

**Returned value**

Returns the arithmetic mean. For a numeric `x` the result is a `Float64`, and `NaN` if `x` is empty. For a date, time or interval `x` the result keeps the type of `x`: the mean is rounded to the nearest representable value, and an empty `x` gives the zero value of that type. [`Float64`](/reference/data-types/float) or [`Date`](/reference/data-types/date) or [`Date32`](/reference/data-types/date32) or [`DateTime`](/reference/data-types/datetime) or [`DateTime64(P)`](/reference/data-types/datetime64) or [`Time`](/reference/data-types/time) or [`Time64(P)`](/reference/data-types/time64) or [`Interval`](/reference/data-types/special-data-types/interval)

**Examples**

**Basic usage**

```sql title=Query theme={null}
SELECT avg(x) FROM VALUES('x Int8', 0, 1, 2, 3, 4, 5);
```

```response title=Response theme={null}
┌─avg(x)─┐
│    2.5 │
└────────┘
```

**Empty table returns NaN**

```sql title=Query theme={null}
CREATE TABLE test (t UInt8) ENGINE = Memory;

SELECT avg(t) FROM test;
```

```response title=Response theme={null}
┌─avg(t)─┐
│    nan │
└────────┘
```

**Average of an interval**

```sql title=Query theme={null}
CREATE TABLE requests (req_id Int64, duration IntervalMillisecond) ENGINE = Memory;

INSERT INTO requests VALUES (1, 100), (2, 200), (3, 300);

SELECT avg(duration) AS avg_duration, toTypeName(avg_duration) FROM requests;
```

```response title=Response theme={null}
┌─avg_duration─┬─toTypeName(avg_duration)─┐
│          200 │ IntervalMillisecond      │
└──────────────┴──────────────────────────┘
```
