SUM() Aggregate Function in SQL

Rumman Ansari   Software Engineer   2023-03-25   5565 Share
☰ Table of Contents

Table of Content:


The SUM() function returns the total sum of a numeric column.

Syntax: SUM()


SELECT SUM(column_name)
FROM table_name
WHERE condition;

We will apply SUM function in this below table:

We will find the SUM all employee salary

EmpId

EmpName

EmpAddress

EmpSalary

EmpDept

11

Rambo

Kolkata

30000

ADMIN

21

Inza

Bihar

31000

SALES

32

Samser

Kolkata

32000

IT

41

Kamran

Hydrabad

33000

ITIS

52

Azam

Kolkata

33000

ITIS

Example: COUNT

Code:


SELECT SUM(EmpSalary) AS SumSalary FROM EmployeeDetails

You have to remember EmpSalary is a numeric column.

Output:

The above code will produce the following result-


SumSalary
159000

Code: Required Code to create the table


USE SQLExamples

DROP TABLE EmployeeDetails

CREATE TABLE EmployeeDetails(
EmpId int,
EmpName VARCHAR(30),
EmpAddress VARCHAR(50),
EmpSalary int,
EmpDept VARCHAR(10)
)

INSERT INTO EmployeeDetails VALUES
(11, 'Rambo', 'Kolkata', 30000, 'ADMIN'),
(21, 'Inza', 'Bihar', 31000, 'SALES'),
(32, 'Samser', 'Kolkata', 32000, 'IT'),
(41, 'Kamran', 'Hydrabad', 33000, 'ITIS'),
(52, 'Azam', 'Kolkata', 33000, 'ITIS')

SELECT * FROM EmployeeDetails