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.

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 SELECT and WHERE knowledge

Step-by-step instructions

Step 1: Wrap the numeric column in SUM()

Use SUM() around the numeric column.

SELECT SUM(total)
FROM orders;

This returns the total of all total values.

Add WHERE to sum only matching rows.

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.

SELECT country, SUM(total)
FROM orders
GROUP BY country;

What to look for:

  • SUM() adds numeric values
  • WHERE filters before summing
  • GROUP BY creates segmented totals
  • NULL values are ignored
  • Great for revenue and quantity reporting

Examples you can copy

Total revenue

SELECT SUM(amount)
FROM payments;

Paid revenue

SELECT SUM(total)
FROM orders
WHERE status = 'paid';

Revenue by country

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:

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:

SELECT country, SUM(total)
FROM orders;

Why it breaks: SQL needs grouping rules for the non-aggregated column.

Corrected approach:

GROUP BY country

Mistake 3: Expecting NULL rows to contribute zero automatically in all logic

What the reader might do:

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 WHERE for filtered sums
  • Use GROUP BY for segmented totals
  • NULL values are ignored
  • Use COALESCE() when zero fallback is needed