How to Use SUM in SQL
Use SUM() when you need the total of numeric values across matching rows. This is essential for revenue reports, quantity totals, expense dashboards, and score calculations.
What you’ll build or solve
You’ll learn how to use SUM in SQL for totals, filtered sums, and grouped calculations. You’ll also know how NULL values affect the result.
Learn SQL on Mimo
When this approach works best
This approach is the right choice when numeric values should be added together.
Common real-world scenarios include:
- Total revenue
- Units sold
- Expense reports
- Team points
- Inventory totals
This is a bad idea when the column is text-based or when row-level details are needed without aggregation.
Prerequisites
You only need:
- A SQL table with numeric columns
- Basic
SELECTandWHEREknowledge
Step-by-step instructions
Step 1: Wrap the numeric column in SUM()
Use SUM() around the numeric column.
SQL
SELECT SUM(total)
FROM orders;
This returns the total of all total values.
Add WHERE to sum only matching rows.
SQL
SELECT SUM(total)
FROM orders
WHERE status = 'paid';
This pattern works especially well for revenue reporting.
Combine it with GROUP BY for per-category totals.
SQL
SELECT country, SUM(total)
FROM orders
GROUP BY country;
What to look for:
SUM()adds numeric valuesWHEREfilters before summingGROUP BYcreates segmented totalsNULLvalues are ignored- Great for revenue and quantity reporting
Examples you can copy
Total revenue
SQL
SELECT SUM(amount)
FROM payments;
Paid revenue
SQL
SELECT SUM(total)
FROM orders
WHERE status = 'paid';
Revenue by country
SQL
SELECT country, SUM(total)
FROM orders
GROUP BY country;
Common mistakes and how to fix them
Mistake 1: Using SUM() on text columns
What the reader might do:
SQL
SELECT SUM(status)
FROM orders;
Why it breaks: text values cannot be added meaningfully.
Corrected approach:
Use only numeric columns.
Mistake 2: Forgetting GROUP BY with normal columns
What the reader might do:
SQL
SELECT country, SUM(total)
FROM orders;
Why it breaks: SQL needs grouping rules for the non-aggregated column.
Corrected approach:
SQL
GROUP BY country
Mistake 3: Expecting NULL rows to contribute zero automatically in all logic
What the reader might do:
SQL
SELECT SUM(discount)
FROM orders;
Why it breaks: NULL values are skipped, which may be correct or may hide missing data assumptions.
Corrected approach:
Use COALESCE(discount, 0) when needed.
Troubleshooting
If totals look low, check for filters or NULL values.
If the query errors, add GROUP BY for non-aggregated columns.
If the result is NULL, confirm matching rows exist.
If missing values should count as zero, use COALESCE().
Quick recap
- Use
SUM()for numeric totals - Add
WHEREfor filtered sums - Use
GROUP BYfor segmented totals NULLvalues are ignored- Use
COALESCE()when zero fallback is needed
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot