SQL SUM() for Beginners

In SQL, the SUM() function is an aggregate function that returns the sum of all values in a given expression.

It can also be used to return the sum of all distinct (unique) values in an expression.

The expression must be numeric (it cannot be character string, bit string, or datetime).

Below are some basic examples to demonstrate how it works.

Sample Table

Suppose we have the following table:

SELECT * FROM Products;

Result:

+-------------+------------+---------------------------------+----------------+-----------------------------------------+
| ProductId   | VendorId   | ProductName                     | ProductPrice   | ProductDescription                      |
|-------------+------------+---------------------------------+----------------+-----------------------------------------|
| 1           | 1001       | Left handed screwdriver         | 25.99          | Purple. Includes left handed carry box. |
| 2           | 1001       | Long Weight (blue)              | 14.75          | Includes a long wait.                   |
| 3           | 1001       | Long Weight (green)             | 11.99          | Approximate 30 minute waiting period.   |
| 4           | 1002       | Sledge Hammer                   | 33.49          | Wooden handle. Free wine glasses.       |
| 5           | 1003       | Chainsaw                        | 245.00         | Orange. Includes spare fingers.         |
| 6           | 1003       | Straw Dog Box                   | NULL           | Tied with vines. Very chewable.         |
| 7           | 1004       | Bottomless Coffee Mugs (4 Pack) | 9.99           | Brown ceramic with solid handle.        |
| 8           | 1001       | Right handed screwdriver        | 25.99          | Blue. Includes right handed carry box.  |
+-------------+------------+---------------------------------+----------------+-----------------------------------------+

Example

We can use the following query to get the sum of all prices.

SELECT SUM(ProductPrice)
FROM Products;

Result:

+--------------------+
| (No column name)   |
|--------------------|
| 367.20             |
+--------------------+

In this case, price information is stored in the ProductPrice column, and so we pass that as an argument to the SUM() function, which then calculates the sum and returns the result.

Using Column Aliases

You’ll notice that the previous results don’t include a column name. This is to be expected, because the SUM() function doesn’t return any columns. You can easily provide a column name by assigning an alias.

SELECT SUM(ProductPrice) AS Sum
FROM Products;

Result:

+--------+
| Sum    |
|--------|
| 367.20 |
+--------+

Filtered Results

The SUM() function operates on the rows returned by the query. So if you filter the results, the result of SUM() will reflect that.

SELECT SUM(ProductPrice) AS Sum
FROM Products
WHERE VendorId = 1001;

Result:

+-------+
| Sum   |
|-------|
| 78.72 |
+-------+

In this case, 78.72 is the sum of all the products offered by the specified vendor.

NULL Values

The SUM() function ignores any NULL values. In our sample table above, product number 6 has got NULL in its ProductPrice column, but that was ignored in our SUM() example.

Depending on your DBMS and your settings, you may or may not see a warning that NULL values were eliminated in the result set.

Here’s an example of what you might see:

SELECT SUM(ProductPrice) AS Sum
FROM Products;

Result:

+--------+
| Sum    |
|--------|
| 367.20 |
+--------+
Warning: Null value is eliminated by an aggregate or other SET operation.

All this tells us is that the column contained at least one NULL value, and that it was ignored when calculating the results.

Date/Time Data

The SUM() function does not accept date/time expressions.

Suppose we have the following table:

SELECT PetName, DOB 
FROM Pets;

Result:

+-----------+------------+
| PetName   | DOB        |
|-----------+------------|
| Fluffy    | 2020-11-20 |
| Fetch     | 2019-08-16 |
| Scratch   | 2018-10-01 |
| Wag       | 2020-03-15 |
| Tweet     | 2020-11-28 |
| Fluffy    | 2020-09-17 |
| Bark      | NULL       |
| Meow      | NULL       |
+-----------+------------+

If we try to use SUM() on the DOB column, we’ll get an error.

SELECT SUM(DOB) AS Sum
FROM Pets;

Result:

Msg 8117, Level 16, State 1, Line 1
Operand data type date is invalid for sum operator.

Character Data

The SUM() function also does not accept character string expressions.

Here’s what happens if we try to use SUM() on the ProductName column of our Products table (which uses a data type of varchar):

SELECT SUM(ProductName) AS Sum
FROM Products;

Result:

Msg 8117, Level 16, State 1, Line 1
Operand data type varchar is invalid for sum operator.

The DISTINCT Keyword

You can use the DISTINCT keyword with SUM() to calculate only distinct values. That is, if there are any duplicate values, they are treated as one value.

Example:

SELECT SUM(DISTINCT ProductPrice) AS DistinctSum
FROM Products;

Result:

+---------------+
| DistinctSum   |
|---------------|
| 341.21        |
+---------------+

We can see that this result is lower than the result we got without the DISTINCT keyword.

To recap, we got 367.20 without the DISTINCT keyword, and 341.21 with the DISTINCT keyword.

This is because there are two items that share the same price (the left handed screwdriver and right handed screwdriver are both priced at 25.99). Therefore, the SUM() function, when used with the DISTINCT keyword, treats both of those values as one, and calculates its result accordingly.

Window Functions

Depending on your DBMS, you may be able to use an OVER clause with your SUM() function to create a window function.

A window function performs an aggregate-like operation on a set of query rows. It produces a result for each query row. This is in contrast to an aggregate operation, which groups query rows into a single result row.

Here’s an example to demonstrate the concept.

We’ve already seen the Products table. Our database also has a Customers table, and it contains the following data:

+--------------+----------------------+-------------------+------------+-----------------+------------+-----------+----------------+
| CustomerId   | CustomerName         | PostalAddress     | City       | StateProvince   | ZipCode    | Country   | Phone          |
|--------------+----------------------+-------------------+------------+-----------------+------------+-----------+----------------+
| 1001         | Palm Pantry          | 20 Esplanade      | Townsville | QLD             | 2040       | AUS       | (308) 555-0100 |
| 1002         | Tall Poppy           | 12 Main Road      | Columbus   | OH              | 43333      | USA       | (310) 657-0134 |
| 1003         | Crazy Critters       | 10 Infinite Loops | Cairns     | QLD             | 4870       | AUS       | (418) 555-0143 |
| 1004         | Oops Media           | 4 Beachside Drive | Perth      | WA              | 1234       | AUS       | (405) 443-5987 |
| 1005         | Strange Names Inc.   | 789 George Street | Sydney     | NSW             | 2000       | AUD       | (318) 777-0177 |
| 1006         | Hi-Five Solutionists | 5 High Street     | Highlands  | HI              | 1254       | AUS       | (415) 413-5182 |
+--------------+----------------------+-------------------+------------+-----------------+------------+-----------+----------------+

We can retrieve data from these tables and present them as one result set by using a join.

We can also use the SUM() function with the OVER clause to apply a window function to the data.

SELECT 
    v.VendorName,
    p.ProductName,
    p.ProductPrice,
    SUM(ProductPrice) OVER (PARTITION BY v.VendorName) AS "Sum For This Vendor"
FROM Products p 
INNER JOIN Vendors v 
ON v.VendorId = p.VendorId
ORDER BY VendorName, ProductPrice, "Sum For This Vendor";

Result:

+---------------+---------------------------------+----------------+-----------------------+
| VendorName    | ProductName                     | ProductPrice   | Sum For This Vendor   |
|---------------+---------------------------------+----------------+-----------------------|
| Katty Kittens | Bottomless Coffee Mugs (4 Pack) | 9.99           | 9.99                  |
| Mars Supplies | Long Weight (green)             | 11.99          | 78.72                 |
| Mars Supplies | Long Weight (blue)              | 14.75          | 78.72                 |
| Mars Supplies | Right handed screwdriver        | 25.99          | 78.72                 |
| Mars Supplies | Left handed screwdriver         | 25.99          | 78.72                 |
| Pedal Medals  | Straw Dog Box                   | NULL           | 245.00                |
| Pedal Medals  | Chainsaw                        | 245.00         | 245.00                |
| Randy Roofers | Sledge Hammer                   | 33.49          | 33.49                 |
+---------------+---------------------------------+----------------+-----------------------+

In this case we used the OVER clause with our SUM() function to partition the result by vendor name.

By doing this, we were able to return price information for each product, as well as the sum of all products from that given vendor. The sum changes as the vendor changes (unless multiple vendors happen to have the same sum), but remains the same for all products from the same vendor.

This concept can also be applied to other aggregate functions in SQL, such as AVG(), MIN(), MAX(), and COUNT().