> ## Documentation Index
> Fetch the complete documentation index at: https://docs.firebolt.io/llms.txt
> Use this file to discover all available pages before exploring further.

> Reference material for SUM

# SUM

Calculates the sum of an expression.

## Syntax

```sql theme={"theme":{"light":"css-variables","dark":"css-variables"}}
SUM ([ DISTINCT ] <value>) [FILTER ([WHERE] <condition>)]
```

## Parameters

| Parameter     | Description                                                       | Supported input types |
| :------------ | :---------------------------------------------------------------- | :-------------------- |
| `<value>`     | The expression used to calculate the sum.                         | Any numeric type      |
| `<condition>` | An optional boolean expression to filter rows used in aggregation | `BOOL`                |

Valid values for `<value>` include column names or expressions that evaluate to numeric values. When `DISTINCT` is being used, only the unique number of rows with no `NULL` values are summed.

## Return Types

`NUMERIC`

## Precision and Determinism

Applying `SUM` to REAL and DOUBLE PRECISION is subject to [floating point arithmetic accuracy limitations](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems) and its resulting error.
This error may add up when aggregating multiple values.

The order of operations while computing the aggregate is non-deterministic.
This can lead to varying total floating point error when running a query multiple times.
If this is not acceptable for your use-case, aggregate on [NUMERIC](/reference-sql/data-types/numeric) data instead.

## Examples

The `tournaments` table contains the following data:

<div className="query-window">
  ```
  SELECT name, totalprizedollars FROM tournaments ORDER BY name LIMIT 5;
  ```

  | name <span>text null</span>   | totalprizedollars <span>int null</span> |
  | :---------------------------- | :-------------------------------------- |
  | The Acceleration Championship | 19274                                   |
  | The Acceleration Trials       | 13877                                   |
  | The Accelerator Cup           | 19466                                   |
  | The African Grand Prix        | 895                                     |
  | The Alpine Adventure Rally    | 16810                                   |

  <p><span>Rows: 5</span><span>Execution time: 6.00ms</span></p>
</div>

The following example calculates the total prize money across all tournaments in the `tournaments` table:

<div className="query-window">
  ```
  SELECT SUM(totalprizedollars) FROM tournaments;
  ```

  | sum <span>long null</span> |
  | :------------------------- |
  | 2026880                    |

  <p><span>Rows: 1</span><span>Execution time: 6.43ms</span></p>
</div>

The `levels` table contains the following `maxpoints` values:

<div className="query-window">
  ```
  SELECT level, maxpoints FROM levels ORDER BY level;
  ```

  | level <span>int null</span> | maxpoints <span>int null</span> |
  | :-------------------------- | :------------------------------ |
  | 1                           | 20                              |
  | 2                           | 30                              |
  | 3                           | 40                              |
  | 4                           | 100                             |
  | 5                           | 150                             |
  | 6                           | 80                              |
  | 7                           | 70                              |
  | 8                           | 100                             |
  | 9                           | 250                             |
  | 10                          | 500                             |

  <p><span>Rows: 10</span><span>Execution time: 6.00ms</span></p>
</div>

`SUM(DISTINCT)` adds only unique values, skipping duplicates. `maxpoints` of `100` is shared by two levels (Nitro Narrows and Racing Ravine), so `SUM(DISTINCT maxpoints)` counts that value only once:

<div className="query-window">
  ```
  SELECT SUM(maxpoints) AS total_maxpoints, SUM(DISTINCT maxpoints) AS distinct_maxpoints FROM levels;
  ```

  | total\_maxpoints <span>long null</span> | distinct\_maxpoints <span>long null</span> |
  | :-------------------------------------- | :----------------------------------------- |
  | 1340                                    | 1240                                       |

  <p><span>Rows: 1</span><span>Execution time: 6.08ms</span></p>
</div>
