SQL > SQL Functions > SUM Function

The SUM function is used to calculate the total for an expression.

Key Takeaway: The SQL SUM function adds up all numeric values in a column or expression, ignoring NULLs. Combine it with GROUP BY to compute subtotals for each group of rows.

Syntax

The syntax for the SUM function is,

SELECT SUM(<expression>)
FROM "table_name";

<expression> can be a column name or an arithmetic operation. An arithmetic operation can include more than one column, such as ("column1" - "column2").

It is also possible to have one or more columns in addition to the SUM function in the SELECT statement. In those cases, these columns need to be part of the GROUP BY clause as well:

SELECT "column_name1", "column_name2", ... "column_nameN", SUM("column_nameN+1")
FROM "table_name";
GROUP BY "column_name1", "column_name2", ... "column_nameN";

Examples

We use the following table for our examples.

Table Store_Information

 Store_Name  Sales  Txn_Date 
 Los Angeles 1500  Jan-05-1999 
 San Diego 250  Jan-07-1999 
 Los Angeles 300  Jan-08-1999 
 Boston 700  Jan-08-1999 

Example 1: SUM function on a column

To get the sum of all sales from Store_Information, we type in,

SELECT SUM(Sales) FROM Store_Information;

Result:

SUM(Sales)
2750

2750 represents the sum of all Sales entries: 1500 + 250 + 300 + 700.

Example 2: SUM function on an arithmetic operation

Assume that sales tax is 10% of the sales amount, we use the following SQL statement to get the total sales tax amount:

SELECT SUM(Sales*0.1) FROM Store_Information;

Result:

SUM(Sales*0.1)
275

SQL will first calculate "Sales*0.1" and then apply the SUM function to the result.

Example 3: SUM function with a GROUP BY clause

To get the sum of sales for each store, we type in,

SELECT Store_Name, SUM(Sales) FROM Store_Information GROUP BY Store_Name;

Result:

Store_Name SUM(Sales)
Los Angeles 
1800
San Diego 
250
Boston 
700

Frequently Asked Questions

What does the SQL SUM function do?
SUM adds up all numeric values in a column or expression across all rows that match the query's conditions, returning a single total value.
Can SQL SUM be used with GROUP BY?
Yes. Adding a GROUP BY clause makes SUM compute a separate total for each group. For example, grouping by Store_Name gives you total sales per store.
Can SUM be applied to arithmetic expressions?
Yes. You can apply SUM to a calculated expression such as SUM(Sales * 0.1), which first computes the expression for each row and then totals the results.
Does SQL SUM include NULL values in its calculation?
No. SUM automatically ignores NULL values. Only rows with a non-NULL numeric value in the expression are included in the total.

Next: SQL ROUND Function

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact