SQL - SUM Function
The SUM() function is an aggregate function that is used to find the sum (addition) of the given column or an expression. It can be applied on the numeric values or numeric columns only.
Syntax:
SELECT SUM(column_name) FROM table_name [WHERE condition];
For the demo purpose, we will use the following Employee
table in all examples.
EmpId | FirstName | LastName | Salary | DeptId | |
---|---|---|---|---|---|
1 | John | King | '[email protected]' | 24000 | 10 |
2 | James | Bond | 17000 | 20 | |
3 | Neena | Kochhar | '[email protected]' | 15000 | 20 |
4 | Lex | De Haan | '[email protected]' | 9000 | 30 |
5 | Amit | Patel | 60000 | 30 | |
6 | Abdul | Kalam | '[email protected]' | 4800 | 40 |
The following query calculates the total salary of all the employees.
SELECT SUM(Salary) AS "Total Salary" FROM Employees;
Total Salary |
---|
129800 |
You can also calculate the total salary of each department using the following query:
SELECT DeptId, SUM(Salary) AS "Department wise Total Salary" FROM Employee GROUP BY DeptId;
Employee Table
DeptId | Department wise Total Salary |
---|---|
10 | 24000 |
20 | 32000 |
30 | 69000 |
40 | 4800 |