# avg

> 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: avg
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/aggregate-functions/avg
page-component-name: cloud-data-platform
page-version: master
page-component-version: master
page-component-title: Cloud
page-relative-src-path: sql/sql-functions/aggregate-functions/avg.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/reference/pages/sql/sql-functions/aggregate-functions/avg.adoc
description: The `avg()` function calculates the average value of records.
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/aggregate-functions/avg.md -->

The `avg()` function calculates the average value of records. The supported input and return types are listed in the following table:

| Input type | Return type |
| --- | --- |
| integer | double precision |
| bigint | double precision |
| real | double precision |
| double precision | double precision |

> 📝 **NOTE**
>
> If the input type is 32-bit, then the result is 64-bit

Special cases: Returns NaN if the input contains a NaN.

## [](#examples)Examples

This example uses an `orders` table that stores details of the purchase transactions:

```sql
CREATE TABLE orders (
    orderid int,
    custname text,
    orderproduct text,
    ordertotal real
);
INSERT INTO orders (orderid, custname, orderproduct, ordertotal)
VALUES
(9557411, 'Maya', 'Jeans', 10.5),
(9557421, 'Aaron', 'T-Shirt', 9.2),
(9557451, 'Alex', 'Hat', 10.8),
(9557311, 'Will', 'Hat', 8.5),
(9557321, 'Will', 'T-Shirt', 12.15),
(9557351, 'Maya', 'T-Shirt', 9.5),
(9557221, 'Maya', 'Jeans', 11.02),
(9557251, 'Alex', 'Jeans', 11.09),
(9557231, 'Aaron', 'Hat', 14.56),
(9557281, 'Aaron', 'Hat', 12.15),
(9557291, 'Will', 'T-Shirt', 13.1);
```

```sql
SELECT * FROM orders;
```

This query shows the following table:

```sql
+----------+-----------+---------------+-------------+
| orderid  | custname  | orderproduct  | ordertotal  |
+----------+-----------+---------------+-------------+
| 9557411  | Maya      | Jeans         | 10.5        |
| 9557421  | Aaron     | T-Shirt       | 9.2         |
| 9557451  | Alex      | Hat           | 10.8        |
| 9557311  | Will      | Hat           | 8.5         |
| 9557321  | Will      | T-Shirt       | 12.15       |
| 9557351  | Maya      | T-Shirt       | 9.5         |
| 9557221  | Maya      | Jeans         | 11.02       |
| 9557251  | Alex      | Jeans         | 11.09       |
| 9557231  | Aaron     | Hat           | 14.56       |
| 9557281  | Aaron     | Hat           | 12.15       |
| 9557291  | Will      | T-Shirt       | 13.1        |
+----------+-----------+---------------+-------------+
```

### [](#avg-with-a-single-expression)`avg()` with a single expression

The first example calculates the average amount of all orders that customers have paid:

```sql
SELECT AVG(ordertotal) AS "Order Total Average"
FROM orders;
```

This returns the following output:

```sql
+---------------------+
| Order Total Average |
+---------------------+
| 11.142727331681685  |
+---------------------+
```

### [](#avg-with-a-group-by-clause)`avg()` with a GROUP BY clause

The following example uses the `avg()` function and `GROUP BY` clause to calculate the average amount paid by each customer:

-   First, the `GROUP BY` clause divides orders into groups based on customers

-   Then, the `avg` function is applied to each group.


```sql
SELECT custname AS "Customer", AVG (ordertotal) AS "Total Price Average"
FROM orders
GROUP BY custname;
```

The query returns:

```sql
+-----------+----------------------+
| Customer  | Total Price Average  |
+-----------+----------------------+
| Aaron     | 11.96999994913737    |
| Alex      | 10.945000171661377   |
| Will      | 11.25                |
| Maya      | 10.34000015258789    |
+-----------+----------------------+
```

You can use the cast operator like\`::NUMERIC(10,2)\` to add two decimal numbers after the comma:

```sql
SELECT custname AS "Customer", AVG (ordertotal)::NUMERIC(10,2) AS "Total Price Average"
FROM orders
GROUP BY custname;
```

The result will trim and round two numbers after the comma:

```sql
+-----------+----------------------+
| Customer  | Total Price Average  |
+-----------+----------------------+
| Aaron     | 11.97                |
| Alex      | 10.95                |
| Will      | 11.25                |
| Maya      | 10.34                |
+-----------+----------------------+
```