MySQL SUM Function
Introduction to MySQL SUM Function
This MySQL tutorial explains how to use the MySQL SUM function with syntax and examples. The MySQL SUM function returns the summed value of an expression.
SUM() Syntax
SELECT SUM(column_name) FROM table_name WHERE condition;
To understand SUM function, consider an “OrderDetails” table, which is having the following records:
SELECT * FROM OrderDetails;
OrderDetailID | OrderID | ProductID | Quantity |
---|---|---|---|
1 | 6 | 44 | 6 |
1 | 2 | 44 | 12 |
2 | 6 | 45 | 7 |
3 | 5 | 78 | 8 |
3 | 4 | 44 | 12 |
4 | 2 | 46 | 10 |
7 | 7 | 32 | 10 |
8 | 6 | 59 | 18 |
The following MySQL statement finds the sum of the “Quantity” fields in the “OrderDetails” table:
SELECT SUM(Quantity) AS TotalQuantity FROM OrderDetails;
TotalQuantity |
---|
83 |