SUM
- SQL course
- Intermediate
- Lesson 37/115
- 4 min
Sum numeric values with SUM.
SUM
SUM returns the total of a column. It works with numeric columns.
SELECT SUM(price) FROM products;This query gives the total of all prices.
SUM(price) is selected from products.
SUM — details
SUM skips NULL values. It errors on non-numeric columns.
For example, SELECT SUM(amount) FROM orders returns the total of all orders; with no rows the result is NULL, not 0.
Practice SUM
Compute the total of prices in products with SUM(price).
Starter code
-- price total
SELECTSample solution
SELECT SUM(price) FROM products;Expected output
sum 3500
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Add SUM
SELECT ____(price) FROM products;
SELECT SUM(price) FROM products;
Explanation: SUM(price) returns the total of all prices.
2 Which columns does SUM work with?
- Everyone
- Text
- A date
- Numeric Correct answer
Explanation: SUM works with numeric columns.
3 SUM: match the terms
| Term | Meaning |
|---|---|
SUM |
sum |
numeric |
numeric |
NULL |
skip |
COUNT |
count |