🔧 Quick Syntax
SELECT SUM(column_name)
FROM table_name;
This tells SQL:
“Go down the column and add up all the numbers you find.”
🧾 Here’s the Table You’re Working With
Your table is called orders
:
order_id | customer_name | total_amount |
---|---|---|
1 | Fenn Rask | 10.00 |
2 | Milo Kresh | 45.00 |
3 | Sora Dune | 15.99 |
4 | Lexa Vohn | 72.50 |
5 | Tovi Glint | 29.99 |
✅ Suppose You Want the Total of All Orders
SELECT SUM(total_amount)
FROM orders;
💡 Output:
173.48
SQL looked at the total_amount
column and added all the values together. Total revenue = done.
✅ What Happens If Some Values Are NULL?
Let’s say one row has total_amount = NULL
. SQL won’t freak out — it just skips it.
SELECT SUM(total_amount)
FROM orders;
The sum is still calculated from the non-null values. NULL
s are ignored by SUM()
.
✅ Suppose You Want Total Only for High-Value Orders
You only want to count orders where the total is greater than 30:
SELECT SUM(total_amount)
FROM orders
WHERE total_amount > 30;
💡 Output:
117.50
SQL added just the values that matched the condition — in this case, orders from Milo and Lexa.
🧃 Recap – What You Learned
SUM()
adds up numbers in a columnNULL
values are ignored- Use
WHERE
to total specific rows - Great for revenue, totals, points, quantity, budgets, and more