Loading lesson path
The SUM() function is used to calculate the total sum of values within a specified numeric column. The SUM() function ignores NULL values in the column. The following SQL returns the sum of the Quantity field in the "OrderDetails" table:
FROM OrderDetails;column_name )
table_name
condition
;OrderDetails table used in the examples:
10248 11 12
10248 42 10
10248 72
10249 14
10249 51 40
clause to specify conditions. The following SQL returns the sum of the Quantity field for the product with
Formula
ProductID = 11, in the "OrderDetails" table:WHERE ProductId = 11;Give the summarized column a name by using the AS keyword.
Name the column "total":
FROM OrderDetails;Here we use the SUM() function and the GROUP BY
clause, to return the Quantity for EACH OrderID in the "OrderDetails" table:SELECT OrderID, SUM(Quantity) AS [Total Quantity]GROUP BY OrderID;You will learn more about the GROUP BY clause later in this tutorial.