SQL: SUM() reference
19 March 2023 (Updated 19 March 2023)
On this page
SUM() is an aggregate function that lets you calculate the sum of values in a set.
Example & execution plan
select sum(salary) from salaries;
The above query will be handled by MySQL as follows:
- Read through the
salariestable and extract the value from the
salarycolumn for each row.
- For each value in the
salarycolumn, add it to a running total.
- Return the running total as the value of the
SUM() will calculate the sum of all values in the set, including duplicate values. You can prefix your expression with
DISTINCT to tell MySQL only to sum the values that are distinct.