SUM() — Calculate the Total
The SUM() function returns the total (sum) of all values in a numeric column.
It works with numbers and is commonly used for totals in reports and dashboards.
Syntax
SELECT SUM(column) FROM table_name WHERE condition;
Example 1: Total Age of All Employees
SELECT SUM(age) FROM employees;
This returns the sum of the age values of all employees.
+----------+
| SUM(age) |
+----------+
| 570 |
+----------+
Example 2: Total Age per Department
SELECT department, SUM(age)
FROM employees
GROUP BY department;
This returns the total age of employees for each department.
+------------+----------+
| department | SUM(age) |
+------------+----------+
| HR | 52 |
| IT | 168 |
| Sales | 168 |
| Marketing | 59 |
| Finance | 123 |
+------------+----------+
Tip
SUM()ignoresNULLvalues.- Only works with numeric columns.
The SUM() function helps you calculate totals, which is very useful in data analysis and reporting!