SQL Basics | Functions | SUM
SUM can retrieve the total of data returned from a table.
SUM syntax
SELECT SUM("field_name")
FROM "table_name";
SUM example
For example, suppose you want to calculate the total of the sales field in the example table.
store_information table
| store_name | sales | txn_date |
|---|---|---|
| Los Angeles | 1500 | Jan-05-2018 |
| San Diego | 250 | Jan-07-2018 |
| Los Angeles | 300 | Jan-08-2018 |
| Boston | 700 | Jan-08-2018 |
Enter the following command.
SELECT SUM(sales) FROM store_information;
The result is as follows.
| SUM(sales) |
|---|
| 2750 |
2750 is the total of all sales fields: 1500 + 250 + 300 + 700.
In addition to functions, SQL can also perform simple mathematical operations such as addition (+) and subtraction (-). SQL also has several string processing functions for character data, such as concatenation, trim, and substring. Functions differ by database. To check how to use those functions, refer to the information for the database you are using.