0% found this document useful (0 votes)
4 views

SQL SUM Function

Uploaded by

Anum Masood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL SUM Function

Uploaded by

Anum Masood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL SUM()

Function
‹ Previous Next ›

The SQL SUM() Function


The SUM() function returns the total sum of a
numeric column.

Example Get your own SQL Server

Return the sum of all Quantity fields in the


OrderDetails table:

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:

OrderDetailID OrderID ProductID Quantity

1 10248 11 12

2 10248 42 10

3 10248 72 5

4 10249 14 9

5 10249 51 40

 Tutorials  Exercises  Certificates  Services  Search...

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS

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 Examples If we assume that each product in the


SQL Examples OrderDetails column costs 10 dollars, we can
SQL Editor find the total earnings in dollars by multiply each
SQL Quiz quantity with 10:

SQL Exercises
SQL Server
SQL Bootcamp
Example
SQL Certificate
Use an expression inside the SUM() function:

SELECT SUM(Quantity * 10)


FROM OrderDetails;

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:

SELECT SUM(Price * Quantity)


FROM OrderDetails
LEFT JOIN Products ON
OrderDetails.ProductID =
Products.ProductID;

Try it Yourself »

You will learn more about Joins later in this


tutorial.

Test Yourself With


Exercises

Exercise:
Use an SQL function to calculate the sum of all
the Price column values in the Products
table.

You might also like