SUM() Function in SQL Server

SUM() function returns the total of a column’s values. It is very important that the Columns must be numeric for this function to work properly.We can use the CONVERT() function to change numbers stored as text to the required numeric data type, which must also be specified in the function.

Summary:

SELECT SUM(Column_name)
FROM table_name

Example:

Suppose we have a table ‘tbl_employee’

Table name: tbl_Employee

Employee IdEmpNameSalaryDepartment
1Ankur1200Sales
2David3400Sales
3John1500HR
4James1200Production
5Mohan4500Production
6Ram2300HR

 

1. Now we want to calculate sum of salary for all employee  then we use following query with select statement:

SELECT SUM(salary) FROM tbl_Employee

And output will look like this:

14100

 

2.  Suppose if you want to calculate sum of the salaries of the employees of the ‘HR’ department, then you can use where clause in the above query like as:

SELECT SUM(salary) FROM tbl_Employee where Department=’HR’

And output will look like this:

3800