EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL SUM()
 

SQL SUM()

Updated March 8, 2023

SQL SUM()

 

 

Introduction to SQL SUM()

SQL SUM() is one of the aggregate functions available in SQL that helps us fetch the total value among multiple values specified in the column values of records, the expression consisting of the column that is mentioned. When a query is used to retrieve the data that report related and contains a group by statement, the SUM() function is used to get the total value of a particular column or columns based on the grouping function. This article will see about the syntax and usage of SQL SUM() function and then implement the function in various examples involving column values, column values with grouping statements, etc., to get a clear idea about its usage.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax and Usage

Given below is the syntax of the SUM function in SQL:

SELECT SUM(expression)
FROM table_name
[WHERE restriction];

Where expression can be any name of the column of the table or a formula built up using column names and static literal values or variables. The table_name is the name of the table from which you want to retrieve the records and calculate the total value from one of their columns. The use of the FROM table name clause is required. One optional thing is the use of a where clause to mention the conditions and restrictions that the records of the table should fulfil to consider that record’s column value for calculating the total value.

When where clause is used, only filtered out data is considered for the SUM() function calculation. The SUM() function collects all the values of the expression mentioned in it and adds them up to find out the final total value. For example, consider that we have to find out a total of 50, 100, 150, and 200. Then total function will internally calculate 50 + 100 + 150 + 200 which evaluates to 500.

Examples of SQL SUM()

Given below are the examples of SQL SUM():

Example #1 – Using a single column.

First, consider a simple example that we used above to see the working of the SUM() function. We will calculate the total value of SQL numbers using the SUM() function. Let us create one simple table named numbers and store the num column value in it.

We will use the following query statement to create our table.

Code:

CREATE TABLE numbers (num INT) ;

Now, we will insert the above records in the table.

Code:

INSERT INTO numbers(num) VALUES (50), (100), (150), (200);

Let us now retrieve the records once.

Code:

SELECT * FROM numbers ;

Output:

SQL sum() 1

Now, we will calculate the total of num column of numbers table using SUM() function using the following query statement.

Code:

SELECT SUM(num) FROM numbers ;

Output:

SQL sum() 2

Example #2 – Using the distinct function.

We can use the distinct function in the SUM() function to consider the column’s repetitive values only once while calculating the total value.

Suppose that we insert some more records in the numbers table using the following query statement.

Code:

INSERT INTO numbers(num) VALUES (350), (800), (150), (300),(450), (100), (250);

If we use SELECT SUM(num) FROM numbers; statement to calculate the total value of num column, then each of the values will be considered while calculating the total value if we use the following statement.

Code:

SELECT SUM(DISTINCT(num)) FROM numbers ;

The output will be different from that of the first query without a distinct function because internally, the calculation of total value by SUM() function will only consider the repeated column values, such as 100 and 150, only once.

The output of both of the above queries is as shown below:

Output:

Using distinct function

Example #3 – Using formula.

We can use the expressions in the SUM() function to consider the value evaluated by calculating each of the formula or expression values containing column value to calculate the total value. Let us consider one example; we will calculate the total of all the columns after they are multiplied by 10 and added by 1.

Code:

SELECT SUM((num * 10) + 1) FROM numbers ;

Output:

Using formula

We can even use the existing functions such as AVG() and COUNT() inside the SUM() function.

Example #4 – Using group by.

When we have complex tables and relations between multiple tables, we have to query those tables using joins to retrieve data, usually for reporting purposes that consist of summarized data. Even in some scenarios, the data from a single table need to be manipulated to get summarized data. Suppose that we have one table named workers consisting of the following records in it that are retrieved by executing a simple select query on that table.

Code:

SELECT * FROM workers;

Output:

Using group by

Now, the situation is such that we have to calculate the total salary of the workers per team. The output should consist of the team id and the total salary of that team. For this, we will have to use the group by statement and group the records based on team id and calculate the total salary by using the SUM() function.

Code:

SELECT
team_id,
SUM(salary)
FROM
workers
GROUP BY team_id ;

The execution of the above query statement will give the following output along with team ids and their respective total salaries.

Output:

SQL sum() 6

Conclusion

We can use the SUM() function in SQL to calculate the total value of the columns of the tables or the total of expressions that involve column values and even calculate the total value of columns in the grouped manner using GROUP BY statement.

Recommended Articles

We hope that this EDUCBA information on “SQL SUM()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. SQL ORDER BY Alphabetical
  2. SQL ORDER BY Ascending
  3. SQL Temporary Table
  4. SQL Table Partitioning

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW