MySQL numeric functions
MySQL string functions
MySQL Date/Time functions
MySQL aggregate functions
MySQL flow control functions
The SUM()
function in MySQL is used to return the summed value of an expression. Typically, it's used to add up numeric column values in a table.
The basic syntax of the SUM()
function is:
SUM(expression)
The expression
can be a column name, the result of another function, or a math operation.
Please note that the SUM()
function only includes non-NULL values. If all values are NULL, the SUM()
function returns NULL.
Here's an example. Let's say we have a sales
table with the following data:
id | product | amount |
---|---|---|
1 | A | 10 |
2 | B | 20 |
3 | A | 30 |
4 | C | 40 |
We could use the SUM()
function to find the total amount of all sales:
SELECT SUM(amount) AS total_sales FROM sales;
This would return 100, which is the sum of all values in the amount
column.
We could also use SUM()
in combination with the GROUP BY
statement to find the total sales for each product:
SELECT product, SUM(amount) AS product_sales FROM sales GROUP BY product;
This would return:
product | product_sales |
---|---|
A | 40 |
B | 20 |
C | 40 |
This shows the total sales for each product in the sales
table.
Keep in mind that SUM()
is an aggregate function, meaning it returns a single result value. It's often used in conjunction with GROUP BY
to aggregate results by certain columns.
Using SUM function in MySQL for total aggregation:
SUM
function in MySQL for aggregating and calculating the total sum of a column's values.SELECT SUM(column1) AS total_sum FROM my_table;
MySQL SUM function example:
SUM
function in MySQL to calculate the total sum of values.SELECT SUM(column1) AS total_sum FROM my_table;
How to sum values in MySQL:
SUM
function, specifying the column or expression to be summed.SELECT SUM(column1) AS total_sum FROM my_table;
Aggregate functions in MySQL: SUM usage:
SUM
function as one of the aggregate functions in MySQL for total aggregation.SELECT SUM(column1) AS total_sum FROM my_table;
MySQL summing values with SUM:
SUM
function to calculate the total sum.SELECT SUM(column1) AS total_sum FROM my_table;
Summation operations in MySQL using SUM:
SUM
function for calculating totals.SELECT SUM(column1) AS total_sum FROM my_table;
Totaling values with SUM in MySQL:
SUM
function.SELECT SUM(column1) AS total_sum FROM my_table;