SQL SUM Function
SQL SUM Function
Function
‹ Previous Next ›
SELECT SUM(Quantity)
FROM OrderDetails;
Try it Yourself »
Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Demo Database
Below is a selection from the OrderDetails table
used in the examples:
1 10248 11 12
2 10248 42 10
3 10248 72 5
4 10249 14 9
5 10249 51 40
SQL Tutorial
SQL HOME
SQL Intro
Acrobat's got it.
SQL Syntax
SQL Select
SQL Select Distinct
SQL Where Secure new business fast with e-signatures
in Adobe Acrobat.
SQL Order By
SQL And
SQL Or
SQL Not
SQL Insert Into
SQL Null Values Add a WHERE Clause
Add a WHERE Clause
SQL Update
SQL Delete You can add a WHERE clause to specify conditions:
SQL Select Top
SQL Aggregate Functions
SQL Min and Max Example
SQL Count
SQL Sum Return the sum of the Quantity field for the
product with ProductID 11:
SQL Avg
SQL Like
SELECT SUM(Quantity)
SQL Wildcards
FROM OrderDetails
SQL In WHERE ProductId = 11;
SQL Between
SQL Aliases Try it Yourself »
SQL Joins
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join Use an Alias
SQL Self Join
SQL Union Give the summarized column a name by using the
SQL Group By AS keyword.
SQL Having
SQL Exists
SQL Any, All
Example
SQL Select Into
Name the column "total":
SQL Insert Into Select
SQL Case SELECT SUM(Quantity) AS total
SQL Null Functions FROM OrderDetails;
SQL Stored Procedures
SQL Comments Try it Yourself »
SQL Operators
SQL Database
SQL Create DB
SQL Drop DB Use SUM() with GROUP
SQL Backup DB
SQL Create Table
BY
SQL Drop Table
Here we use the SUM() function and the GROUP
Here we use the SUM() function and the GROUP
SQL Alter Table
BY clause, to return the Quantity for each
SQL Constraints
OrderID in the OrderDetails table:
SQL Not Null
SQL Unique
SQL Primary Key
Example
SQL Foreign Key
SQL Check
SELECT OrderID, SUM(Quantity) AS [Total
SQL Default Quantity]
SQL Index FROM OrderDetails
SQL Auto Increment GROUP BY OrderID;
SQL Dates
SQL Views Try it Yourself »
SQL Injection
SQL Hosting
SQL Data Types
You will learn more about the GROUP BY clause
later in this tutorial.
SQL References
SQL Keywords
MySQL Functions
SQL Server Functions
SUM() With an Expression
MS Access Functions
The parameter inside the SUM() function can also
SQL Quick Ref
be an expression.
SQL Exercises
SQL Server
SQL Bootcamp
Example
SQL Certificate
Use an expression inside the SUM() function:
Try it Yourself »
We can also join the OrderDetails table to the
Products table to find the actual amount,
instead of assuming it is 10 dollars:
Example
Join OrderDetails with Products , and use
SUM() to find the total amount:
Try it Yourself »
Exercise:
Use an SQL function to calculate the sum of all
the Price column values in the Products
table.