Updated March 8, 2023
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.
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:
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:
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:
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:
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:
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:
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.