MySQL SUM() Function
MySQL SUM() Function
The SUM() function is used to calculate the total sum of values within a numeric column.
The SUM() function ignores NULL values in the column.
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;Demo Database
Below is a selection from the "OrderDetails" table in the Northwind sample database:
| OrderDetailID | OrderID | ProductID | Quantity |
|---|---|---|---|
| 1 | 10248 | 11 | 12 |
| 2 | 10248 | 42 | 10 |
| 3 | 10248 | 72 | 5 |
| 4 | 10249 | 14 | 9 |
| 5 | 10249 | 51 | 40 |
SUM() Example
The following SQL returns the sum of the Quantity field in the "OrderDetails" table:
Note: NULL values are ignored.
Add a WHERE Clause
You can add a WHERE clause to specify conditions.
The following SQL returns the sum of the Quantity field for the product with ProductID = 11, in the "OrderDetails" table: