Lessons › SQL › Chapter 3: Summarising
SQL lesson
SUM()
SUM adds every number in a column and gives you one total. The whole table becomes one row. SUM only works on numbers, like revenue, not on text like category.
Try this lesson free →Example
Add up a column
SELECT SUM(revenue) AS total_revenue
FROM orders| total_revenue |
|---|
| 675.49 |
Step by step
- Adds every revenue together.
SELECT SUM(revenue) AS total_revenue FROM orders - The whole column becomes one number.
SELECT SUM(revenue) AS total_revenue FROM orders - SUM only works on number columns.
SELECT SUM(revenue) AS total_revenue FROM orders
Practice
1. Using orders, add up all the revenue.
SELECT SUM(revenue) FROM orders2. Using store_sales, add up all the revenue.
SELECT SUM(revenue) FROM store_sales3. Using inventory, how many units are in stock across all products? Name it total_stock.
SELECT SUM(stock_count) AS total_stock FROM inventory4. Using shipments, how much weight was shipped in total? Name it total_weight_kg.
SELECT SUM(weight_kg) AS total_weight_kg FROM shipments5. Using returns, show the total refund_amount and how many returns there were, in one row.
SELECT SUM(refund_amount), COUNT(*) FROM returnsLearn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →