0% found this document useful (0 votes)
23 views9 pages

SQL SUM Function

The SQL SUM() function calculates the total sum of a numeric column in a database table. It can be used with a WHERE clause to filter results and can also be combined with GROUP BY to summarize data across multiple rows. Examples include summing quantities from an OrderDetails table and using expressions to calculate total earnings.

Uploaded by

matias bahiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

SQL SUM Function

The SQL SUM() function calculates the total sum of a numeric column in a database table. It can be used with a WHERE clause to filter results and can also be combined with GROUP BY to summarize data across multiple rows. Examples include summing quantities from an OrderDetails table and using expressions to calculate total earnings.

Uploaded by

matias bahiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

7/15/25, 10:31 AM SQL SUM() Function

 Tutorials  Exercises  Services   Sign In

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

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;

https://www.w3schools.com/sql/sql_sum.asp 1/9
7/15/25, 10:31 AM SQL SUM() Function

 Tutorials  Exercises  Services   Sign In


Demo Database
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
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

ADVERTISEMENT

Add a WHERE Clause


You can add a WHERE clause to specify conditions:

Example

https://www.w3schools.com/sql/sql_sum.asp 2/9
7/15/25, 10:31 AM SQL SUM() Function

Return the sum of the Quantity field for the product with ProductID 11:
 Tutorials  Exercises  Services   Sign In

SELECT SUM(Quantity)
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
FROM OrderDetails
WHERE ProductId = 11;

Try it Yourself »

Use an Alias
Give the summarized column a name by using the AS keyword.

Example
Name the column "total":

SELECT SUM(Quantity) AS total


FROM OrderDetails;

Try it Yourself »

Use SUM() with GROUP BY


Here we use the SUM() function and the GROUP BY clause, to return the Quantity for
each OrderID in the OrderDetails table:

Example
SELECT OrderID, SUM(Quantity) AS [Total Quantity]
FROM OrderDetails
GROUP BY OrderID;

https://www.w3schools.com/sql/sql_sum.asp 3/9
7/15/25, 10:31 AM SQL SUM() Function

Try it Yourself »
Tutorials  Exercises  Services   Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
You will learn more about the GROUP BY clause later in this tutorial.

SUM() With an Expression


The parameter inside the SUM() function can also be an expression.

If we assume that each product in the OrderDetails column costs 10 dollars, we can
find the total earnings in dollars by multiply each quantity with 10:

Example
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 »

https://www.w3schools.com/sql/sql_sum.asp 4/9
7/15/25, 10:31 AM SQL SUM() Function

You will learn more about Joins later in this tutorial.


 Tutorials  Exercises  Services   Sign In

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

?
Exercise
What does the SQL SUM() function do?

Counts the number of rows in a table

Returns the total sum of a numeric column

Calculates the average value of a column

Returns the maximum value in a column

Submit Answer »

❮ Previous Next ❯

Track your progress - it's free! Sign Up Log in

ADVERTISEMENT

https://www.w3schools.com/sql/sql_sum.asp 5/9
7/15/25, 10:31 AM SQL SUM() Function

 Tutorials  Exercises  Services   Sign In

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

https://www.w3schools.com/sql/sql_sum.asp 6/9
7/15/25, 10:31 AM SQL SUM() Function

COLOR PICKER 
 Tutorials  Exercises  Services  Sign In

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



ADVERTISEMENT

ADVERTISEMENT

https://www.w3schools.com/sql/sql_sum.asp 7/9
7/15/25, 10:31 AM SQL SUM() Function

 Tutorials  Exercises  Services   Sign In

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

ADVERTISEMENT

 

 PLUS SPACES

GET CERTIFIED FOR TEACHERS

FOR BUSINESS CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial

https://www.w3schools.com/sql/sql_sum.asp 8/9
7/15/25, 10:31 AM SQL SUM() Function
C++ Tutorial

 Tutorials  jQuery Tutorial


Exercises 
Top References
Services   Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve
reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot
warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use,
cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

https://www.w3schools.com/sql/sql_sum.asp 9/9

You might also like