MySQL SUM() Function

The SUM() function in MySQL calculates the total of a set of numeric values. It's a fundamental aggregate function used for summarizing numerical data within a table or a result set from a query.



SUM(): Definition and Usage

SUM() is incredibly useful for getting totals from your data. It automatically ignores any NULL values in the column you're summing, so you don't need to pre-filter out NULLs before using SUM().

Syntax

Syntax

SUM(expression)
      

Parameter Values

Parameter Description
expression A numeric column or an expression that evaluates to a number. This is required.

Example

Calculating the Total Quantity Ordered

This example calculates the total quantity of items ordered from the 'OrderDetails' table (assuming it has a 'Quantity' column).

Syntax

SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;
      
Output

TotalItemsOrdered
-----------------
865  (Example output; this will vary based on your data)
      

**Note:** The example output assumes the existence of an 'OrderDetails' table with a 'Quantity' column containing numeric values. The number 865 is a sample; your output will reflect the sum of the 'Quantity' values in your actual 'OrderDetails' table.