Open In App

SQL – SELECT SUM

Improve
Improve
Like Article
Like
Save
Share
Report

SELECT SUM is used to calculate the total value of an expression in SQL. It is the same as using the AGGREGATE function SUM ( ) in SQL.

In this article, we are going to see how to use “SELECT SUM” in SQL using suitable examples.

Syntax :

SELECT SUM(expr)
FROM Table_Name
WHERE condition;

expr : Expression or column name

Implementation :

1. Creating a Database: 

Use the following syntax to create a database:

CREATE DATABASE database_name

2. Creating a Table:

Use the below syntax to create a table:

CREATE TABLE Table_name(
col_1 TYPE col_1_constraint,
col_2 TYPE col_2 constraint
.....
)

col: Column name
TYPE: Data type whether an integer, variable character, etc
col_constraint: Constraints in SQL like PRIMARY KEY, NOT NULL, UNIQUE, REFERENCES, etc

3. Inserting into a Table:

Use the below syntax to insert data to the table:

INSERT INTO Table_name
VALUES(val_1, val_2, val_3, ..........)

val: Values in particular column

4. View The Table:

Use the below syntax to view the contents of the table:

SELECT * FROM Table_name

Now let’s look into some example use cases of the SELECT SUM function in SQL:

Example 1: Consider the purchase details of mobile phones from an E-commerce site as shown below :

Purchase Information Table

Query : 

Find the sum of all the mobile phones sold on Big Billion Days whose price is less than 55,000 INR.

Example 2: Let’s see another example using the GROUP BY clause. Consider the employee details table of an organization shown below which consists of information about the salary, name, department in which employees are working.

Employee Details

Query : 

Find out the total expenditure the company has to provide to the employees of every department.

In the above result, we can see that the number of employees in HR is 3 and each employee gets a salary of 50,000 INR per month. So, the total salary the company has to provide in the HR department is 1,50,000 INR per month.


Last Updated : 21 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads