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 Id | EmpName | Salary | Department |
1 | Ankur | 1200 | Sales |
2 | David | 3400 | Sales |
3 | John | 1500 | HR |
4 | James | 1200 | Production |
5 | Mohan | 4500 | Production |
6 | Ram | 2300 | HR |
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 |