BBampaa

Lessons › SQL › Chapter 3: Summarising

SQL lesson

SUM()

SUM() adds up every number in a column and hands you one grand total.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Adding Retail Revenue and Quantity with SUM()

SELECT SUM(revenue) AS total_revenue
FROM orders

Result:

total_revenue
675.49

Step by step

  1. Run the query without SUM so you can see what the unfiltered values look like.
    SELECT revenue
    FROM orders;
  2. SUM totals the numbers in a column. Works on numeric columns only.
    SELECT SUM(revenue)
    FROM orders;
  3. AS gives the SUM result a friendly column name so the output reads cleanly.
    SELECT SUM(revenue) AS total_revenue
    FROM orders;

Practice

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

1. Using orders, write a query to add up all values in the revenue column.
SELECT SUM(revenue) FROM orders
2. Using store_sales, write a query to add up all values in the revenue column.
SELECT SUM(revenue) FROM store_sales
3. Using products, write a query to add up all values in the price column.
SELECT SUM(price) FROM products
4. Using reviews, write a query to add up all values in the rating column.
SELECT SUM(rating) FROM reviews
5. Using employees, write a query to add up all values in the salary column.
SELECT SUM(salary) FROM employees

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