Skip to content

SQL SUM Function

Syntax

The SUM function in SQL is used to calculate the total value of a numeric column.

-- Calculate the total of a numeric column
SELECT SUM(column1)
FROM table_name;
-- Calculate the total for each group
SELECT column2, SUM(column1)
FROM table_name
GROUP BY column2;

Example

-- Calculate the total salary of all employees
SELECT SUM(salary)
FROM employees;
/* Output:
| SUM(salary) |
|-------------|
| 150000 |
*/
-- Calculate the total salary for each department
SELECT department, SUM(salary)
FROM employees
GROUP BY department;
/* Output:
| department | SUM(salary) |
|-------------|-------------|
| Sales | 50000 |
| Marketing | 40000 |
| HR | 60000 |
*/