Outdated Version

You are viewing an older version of this section. View current production version.

SUM

Aggregate function. Calculate the sum of a set of numbers. NULL values are ignored. If no values can be summed, this function returns NULL.

Syntax

SUM ( [DISTINCT] expression )

Arguments

  • DISTINCT: optional keyword. If present, will sum the unique values.
  • expression: any expression. This may be a column name, the result of another function, or a math operation.

Return Type

A double if the input type is double, otherwise decimal.

Examples

create table sumptuous (id int primary key, number1 int, number2 int);

insert into sumptuous values (1, 1024, 4096), (2, 23452, NULL);

select sum(number1), sum(number2) from sumptuous;
+--------------+--------------+
| sum(number1) | sum(number2) |
+--------------+--------------+
|        24476 |         4096 |
+--------------+--------------+