How to use sum in MYSQL
How to use sum in MYSQL
137
27-Jul-2023
Aryan Kumar
28-Jul-2023The
SUM()
function in MySQL is used to calculate the sum of a set of values. It can be used with any set of numeric values, such as integers, floats, doubles, and decimals.The syntax for the
SUM()
function is as follows:SQL
The
column_name
parameter is the name of the column that you want to sum. For example, the following SQL statement will calculate the sum of all product prices in theproducts
table:SQL
This will return a single value, which is the sum of all product prices in the
products
table.Here is an example of how to use the
SUM()
function in MySQL:SQL
This will return the following result:
As you can see, the
SUM()
function has calculated the sum of all product prices in theproducts
table, which is 1500.Here are some additional things to keep in mind when using the
SUM()
function:SUM()
function ignores NULL values.DISTINCT
keyword to calculate the sum of distinct values in a column.GROUP BY
clause to calculate the sum of values in a group.