The SUM function is used to calculate the total for an expression.
The syntax for the SUM function is,
<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:
We use the following table for our examples.
Table Store_Information
To get the sum of all sales from Store_Information, we type in,
Result:
2750 represents the sum of all Sales entries: 1500 + 250 + 300 + 700.
Assume that sales tax is 10% of the sales amount, we use the following SQL statement to get the total sales tax amount:
Result:
SQL will first calculate "Sales*0.1" and then apply the SUM function to the result.
To get the sum of sales for each store, we type in,
Result:
| Store_Name | SUM(Sales) |
|---|---|
| Los Angeles | 1800 |
| San Diego | 250 |
| Boston | 700 |
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.