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 →Example
Adding Retail Revenue and Quantity with SUM()
SELECT SUM(revenue) AS total_revenue
FROM orders| total_revenue |
|---|
| 675.49 |
Step by step
- Run the query without SUM so you can see what the unfiltered values look like.
SELECT revenue FROM orders; - SUM totals the numbers in a column. Works on numeric columns only.
SELECT SUM(revenue) FROM orders; - AS gives the SUM result a friendly column name so the output reads cleanly.
SELECT SUM(revenue) AS total_revenue FROM orders;
Practice
1. Using orders, write a query to add up all values in the revenue column.
SELECT SUM(revenue) FROM orders2. Using store_sales, write a query to add up all values in the revenue column.
SELECT SUM(revenue) FROM store_sales3. Using products, write a query to add up all values in the price column.
SELECT SUM(price) FROM products4. Using reviews, write a query to add up all values in the rating column.
SELECT SUM(rating) FROM reviews5. Using employees, write a query to add up all values in the salary column.
SELECT SUM(salary) FROM employeesLearn 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 →