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 SUM() to return 0 when there are no values

What the reader might do:

SELECT SUM(discount)
FROM orders;

Why it breaks: SUM already skips NULL values, so replacing them with zero does not increase the total when at least one value is present. SUM returns NULL (not 0) only when no non-NULL values match.

Corrected approach:

SELECT COALESCE(
  SUM(discount),
  0
)
FROM orders;

Troubleshooting

If totals look low, check for filters.

If the query errors, add GROUP BY for non-aggregated columns.

If the result is NULL, confirm matching rows exist, or that not every matching value is NULL.

If you want 0 instead of NULL when no non-NULL values match, use COALESCE(SUM(discount), 0).

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