BBampaa

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 →
About 5–10 minutes · no sign-up needed

Example

Add up a column

SELECT SUM(revenue) AS total_revenue
FROM orders

Result:

total_revenue
675.49

Step by step

  1. Adds every revenue together.
    SELECT SUM(revenue) AS total_revenue FROM orders
  2. The whole column becomes one number.
    SELECT SUM(revenue) AS total_revenue FROM orders
  3. SUM only works on number columns.
    SELECT SUM(revenue) AS total_revenue FROM orders

Practice

Try each one in your head first, then open the answer.

1. Using orders, add up all the revenue.
SELECT SUM(revenue) FROM orders
2. Using store_sales, add up all the revenue.
SELECT SUM(revenue) FROM store_sales
3. Using inventory, how many units are in stock across all products? Name it total_stock.
SELECT SUM(stock_count) AS total_stock FROM inventory
4. Using shipments, how much weight was shipped in total? Name it total_weight_kg.
SELECT SUM(weight_kg) AS total_weight_kg FROM shipments
5. Using returns, show the total refund_amount and how many returns there were, in one row.
SELECT SUM(refund_amount), COUNT(*) FROM returns

Learn 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 →
About 5–10 minutes · no sign-up needed