The SQL SUM() function is used to get the total of a column or an expression.
SQL SUM() Function Syntax
SELECT SUM(ColumnName) FROM TableName WHERE predicates
SQL SUM() Function Example
Table: Employees
| EmployeeId | FirstName | LastName | Department | Salary |
|---|---|---|---|---|
| 203 | Yilfaee | Uyfda | Finance | 88000 |
| 204 | Bklda | Ylafaea | Finance | 51800 |
| 205 | Qfajfda | Nhjda | Finance | 47000 |
| 206 | Opad | Xjkdl | Finance | 62300 |
| 302 | Hgjfdkl | Bkldfa | Development | 95000 |
| 303 | Uiofda | Whfdjks | Development | 35000 |
| 304 | Pids | Vcds | Development | 41000 |
We want to know the total salary of all the employees:
SELECT SUM(Salary) AS TotalSalary FROM Employees
The result will look like:
| TotalSalary |
|---|
| 420100 |
Example 2, using GROUP BY
We want to know total salary for each department:
SELECT Department, SUM(Salary) AS TotalSalary FROM Employees GROUP BY Department
The result will look like:
| Department | TotalSalary |
|---|---|
| Finance | 249100 |
| Development | 171000 |
