SUM Function

Add up numeric values — with support for conditional totals

Intermediate 10 minSUMaggregate

Summing Values

SUM adds all non-NULL values in a numeric column. NULLs are skipped entirely — they are not treated as zero, they're simply ignored as if they don't exist. Here's what to know:

Numeric columns only — Attempting to SUM a text column returns 0 or an error depending on the database. Always verify your column type before using SUM to avoid silent incorrect results.
Empty set returns NULL, not 0 — SUM of zero matching rows returns NULL. This catches many people off guard. Use COALESCE(SUM(col), 0) to safely default to zero when no rows match your filter conditions.
Conditional totals with CASESUM(CASE WHEN status = 'delivered' THEN total ELSE 0 END) adds only delivered order amounts. This powerful pattern creates pivot-style calculations in a single query without restructuring your source data.
Knowledge Canvas

How SUM Works

Add up numeric values

  • SUM adds all non-NULL values in a column
  • NULL values are silently skipped
  • SUM of all NULLs = NULL (not 0)
  • SUM(DISTINCT col) adds only unique values

NULL Behavior

SUM + NULL interactions

  • SUM skips NULLs: SUM(10, NULL, 20) = 30
  • All NULLs → SUM returns NULL, not 0
  • Use COALESCE: COALESCE(SUM(col), 0)
  • SUM with LEFT JOIN: unmatched rows contribute NULL → safe

SUM Gotchas

Precision and type issues

  • Floating point: SUM may have rounding artifacts
  • Use ROUND(SUM(...), 2) for currency
  • SUM on text columns → error or unexpected behavior
  • Double-counting in joins: SUM inflates with duplicate rows

SUM Patterns

Common real-world uses

  • Revenue: SUM(quantity * price)
  • Conditional: SUM(CASE WHEN paid THEN amount ELSE 0 END)
  • Running total: SUM(amount) OVER (ORDER BY date)
  • Percentage: SUM(col) * 100.0 / SUM(SUM(col)) OVER ()
Syntax
Syntax Template
1-- Plain sum (NULLs are ignored)
2SELECT
3 SUM(column)
4FROM table;
5
6-- Conditional sum with CASE
7SELECT
8 SUM(
9 CASE
10 WHEN cond THEN col
11 ELSE 0
12 END
13 )
14FROM table;
SUM(column)Adds all non-NULL values in the column
SUM + CASEConditional summing — sum only matching rows
Sample Data
orders
12 rows
idstatustotal
5001delivered59.98
5002delivered449
5003shipped12.5
5004delivered89.99
5005pending245
5006delivered159
5007shipped45.5
5008cancelled79
5009delivered425
5010pending18.99
5011delivered134
5012cancelled65.5
products
10 rows
idnamecategorypricestock
P101Wireless MouseElectronics24.99150
P102Office DeskFurniture18925
P103LED Desk LampFurniture45.580
P104USB-C CableElectronics12.99500
P105Bluetooth SpeakerElectronics7960
P106Office ChairFurniture9515
P107Coffee MakerAppliances89.9940
P108Notebook SetStationery8.5200
P109Wireless HeadphonesElectronics15930
P110Standing DeskFurniture4258

Worked Example

Calculate total revenue from all orders, and separately the revenue from delivered orders only.

SQL
1SELECT
2 SUM(total) AS all_revenue,
3 SUM(
4 CASE
5 WHEN status = 'delivered' THEN total
6 ELSE 0
7 END ) AS delivered_revenue
8FROM orders;
SUM(total) adds every order's total regardless of status. The CASE expression inside the second SUM only contributes the total when the order is delivered — cancelled and shipped orders contribute 0.
Output
1 row
all_revenuedelivered_revenue
1783.461316.97

Key Concepts

1SUM of an empty set returns NULL, not 0
2Use COALESCE(SUM(col), 0) to safely default to zero
3SUM + CASE WHEN creates conditional totals in one query
4SUM ignores NULLs — they don't contribute 0, they're skipped

Pro Tip

Revenue, total quantity, cumulative costs — most business metrics are sums. Understanding SUM's NULL handling is essential for accurate reporting.

When to Use

Total revenue, sum of quantities ordered, inventory value, monthly sales figures.

Challenge

Solve the problem below

Calculate the total value of all products in stock (price × stock for each product, then sum everything). Name it total_inventory_value.

Your Query