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.
Syntax
SUM(DISTINCT expression)
Example & execution plan
select sum(salary) from salaries;
The above query will be handled by MySQL as follows:
- Read through the
salaries
table and extract the value from thesalary
column for each row. - For each value in the
salary
column, add it to a running total. - Return the running total as the value of the
SUM()
function.
By default 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.
Tagged:
MySQL
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment