SQL SUM() Function
The SQL SUM() is a function, and return total sum of a table column from query result.
The SQL SUM() function is supports only numeric column or an numeric field based expression.
It can be used in SQL SELECT statement as well in SQL WHERE clause.
Related Links
SQL SUM() Syntax
The below syntax is used to make sum of a numeric value or numeric expression.
SELECT SUM(numeric_value or numeric expression);
The below syntax is used to make sum of a specific column from the specific table.
SELECT SUM(column_name or expression) FROM table_name;
Sample Database Table - Employee
ID | EmpName | Designation | Dept | JoinYear | Salary |
---|---|---|---|---|---|
1 | Ranjani Mai | Big Data | ASP.Net | 2014 | 15020 |
2 | Harish Karthik | Mobile Database | MS Access | 2013 | 8090.8 |
3 | Devi Mai | Project Manager | MySQL | 2013 | 18170.5 |
4 | Pandurengan | Relational DBMS | SQL Server | 2014 | 11240 |
5 | Vidyavathi | SQL Mining | PHP | 2013 | 15860 |
6 | Hari Krishnan | Project Lead | SQL Server | 2015 | 7880 |
SQL SUM() Example - With Single Column
The following SQL SELECT statement find the total salary of all employees from the "Employee" table:
SELECT SUM(Salary) As 'Total Salary'
FROM Employee;
The result of above query is:
Total Salary |
---|
76261.3 |
SQL SUM() Example - Using Expression Or Formula
The following SQL SELECT statement find the total salary, 20% bonus of salary, and then new total salary of all employees from the "Employee" table:
SELECT
SUM(Salary) As 'Total Salary',
SUM((Salary / 100) * 20) As 'Bonus 20%',
SUM(Salary + ((Salary / 100) * 20)) As 'New Total Salary'
FROM Employee;
The result of above query is:
Total Salary | Bonus 20% | New Total Salary |
---|---|---|
76261.3 | 15252.26 | 91513.56 |
SQL SUM() Example - Using Group By Clause
The following SQL SELECT statement find the total salary by grouping "Dept" column of all employees from the "Employee" table:
SELECT
Dept, SUM(Salary) As 'Total Salary'
FROM Employee
GROUP BY Dept;
The result of above query is:
Dept | Total Salary |
---|---|
ASP.Net | 15020 |
MS Access | 8090.8 |
MySQL | 18170.5 |
PHP | 15860 |
SQL Server | 19120 |
Related Links