Summary -

In this topic, we described about the Select Sum with detailed example.

SELECT SUM() is used to return the sum of all values in one specified column. SUM function is applies on numeric or numeric related fields.

Syntax -

SELECT SUM(column_name)
FROM table_name
[WHERE condition]; 
  • column_name – Represents the name of the column on which sum operation performed.
  • table_name - Represents the name of the table.

Example -

Let us consider below table(s) as an example table(s) to frame the SQL query for getting the desired results.

emp_id emp_name designation manager_id date_of_hire salary dept_id
001 Employee1 Director 2019-07-11 45000.00 1000
002 Employee2 Director 2019-07-11 40000.00 2000
003 Employee3 Manager Employee1 2019-07-11 27000.00 1000
004 Employee4 Manager Employee2 2019-10-08 25000.00 2000
005 Employee5 Analyst Employee3 2019-07-11 20000.00 1000
006 Employee6 Analyst Employee3 2019-10-08 18000.00 1000
007 Employee7 Clerk Employee3 2019-07-11 15000.00 1000
008 Employee8 Salesman Employee4 2019-09-09 14000.00 2000
009 Employee9 Salesman Employee4 2019-10-08 13000.00 2000

Scenario – Sum data without any condition.

Requirement– Get the sum of all employee salaries. The query was as follows –

SELECT SUM (salary) AS "Total Salary" FROM employee_details; 

By executing above query, we can get results as shown below -

Total employees
9

Scenario – Sum data with WHERE condition.

Requirement - Get total salary for department 1000. The query was as follows –

SELECT SUM (salary) AS 'Total salary' 
FROM employee_details
WHERE dept_id = 1000; 

By executing above query, we can get results as shown below –

Total salary
125000.00

Scenario – Sum data using GROUP BY clause.

Requirement – Get department wise total salaries. The query was as follows –

SELECT dept_id, SUM(Salary) AS "Total salary" 
FROM employee_details GROUP BY dept_id; 

By executing above query, we can get results as shown below –

dept_id Total salary
1000 125000.00
2000 92000.00