Skip to content
SQLSimplified
group by

SUM() Aggregation With GROUP BY

Sum total units ordered per product category using SUM() grouped by category.

Overview

SUM() adds up a numeric column across rows. Paired with GROUP BY, it answers "total X per Y" questions — such as total units sold per product category in the store.

The Query

Store (Customers/Orders/Products)

Loading database engine...

Step-by-Step Breakdown

  1. SUM(o.quantity) totals units sold per category.
  2. SUM(o.quantity * p.price) computes revenue per category by multiplying before summing.
  3. GROUP BY p.category defines the buckets.

Aggregate then compute

SUM(quantity * price) multiplies inside each row, then sums — different from SUM(quantity) * SUM(price), which would be wrong.

Variations

Sum order quantities per customer city:

Store (Customers/Orders/Products)

Loading database engine...

Common Mistakes

  • SUM of a text column. SUM requires numbers; summing a name errors.
  • Mixing SUM with non-grouped columns. Every selected column must be grouped or aggregated.
  • Ignoring NULLs. SUM ignores NULL values, which is usually what you want, but be aware it won't treat them as zero.

Search

Search lessons, functions, examples, and practice problems