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

# `sum` 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 sum calculated from the values of a group.

## Syntax

```
sum ( [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 or interval.
* `cond`: An optional Boolean expression filtering the rows used for aggregation.

## Returns

If `expr` is an integral number type, a `BIGINT`.
If `expr` is `DECIMAL(p, s)` the result is `DECIMAL(p + min(10, 31-p), s)`.
If `expr` is an interval the result type matches `expr`.
If `expr` is DECFLOAT, the result is `DECFLOAT(34)`.

Otherwise, a `DOUBLE`.

If `DISTINCT` is specified only unique values are summed up.

For DECFLOAT inputs, with the default rounding mode, overflow produces infinity instead of an error.

For other input types, if the result overflows the result type, Databricks raises an [ARITHMETIC\_OVERFLOW](https://docs.databricks.com/aws/en/error-messages/arithmetic-overflow-error-class.md) error. To return a `NULL` instead, use [try\_sum](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_sum.md).

> [!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 of `BIGINT` does not cause an error but “wraps” the result instead.

## Common error conditions

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

## Examples

```sql
> SELECT sum(col) FROM VALUES (5), (10), (15) AS tab(col);
 30

> SELECT sum(col) FILTER(WHERE col <15)
    FROM VALUES (5), (10), (15) AS tab(col);
 15

> SELECT sum(DISTINCT col) FROM VALUES (5), (10), (10), (15) AS tab(col);
 30

> SELECT sum(col) FROM VALUES (NULL), (10), (15) AS tab(col);
 25

> SELECT sum(col) FROM VALUES (NULL), (NULL) AS tab(col);
 NULL

-- try_sum overflows a BIGINT
> SELECT try_sum(c1) FROM VALUES(5E18::BIGINT), (5E18::BIGINT) AS tab(c1);
 NULL

-- In ANSI mode sum returns an error if it overflows BIGINT
> SELECT sum(c1) FROM VALUES(5E18::BIGINT), (5E18::BIGINT) AS tab(c1);
 Error: ARITHMETIC_OVERFLOW

-- try_sum overflows an INTERVAL
> SELECT try_sum(c1) FROM VALUES(INTERVAL '100000000' YEARS), (INTERVAL '100000000' YEARS) AS tab(c1);
  NULL

-- sum returns an error on INTERVAL overflow
> SELECT sum(c1) FROM VALUES(INTERVAL '100000000' YEARS), (INTERVAL '100000000' YEARS) AS tab(c1);
 Error: ARITHMETIC_OVERFLOW
```

## Related

* [`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)
* [`listagg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/listagg.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)
* [`string_agg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/string_agg.md)
* [`try_sum` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_sum.md)
* [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md)
* [`try_avg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_avg.md)
