Tutorialsteacher

Follow Us

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 Employeetable in all examples.

EmpIdFirstNameLastNameEmailSalaryDeptId
1JohnKing'[email protected]'2400010
2JamesBond1700020
3NeenaKochhar'[email protected]'1500020
4LexDe Haan'[email protected]'900030
5AmitPatel6000030
6AbdulKalam'[email protected]'480040

The following query calculates the total salary of all the employees.

SQL Script: SUM()
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:

SQL Script: SUM()
SELECT DeptId, SUM(Salary) AS "Department wise Total Salary" FROM Employee GROUP BY DeptId;
Employee Table
DeptIdDepartment wise Total Salary
1024000
2032000
3069000
404800