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
SUM(o.quantity)totals units sold per category.SUM(o.quantity * p.price)computes revenue per category by multiplying before summing.GROUP BY p.categorydefines 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
SUMof a text column.SUMrequires numbers; summing anameerrors.- Mixing SUM with non-grouped columns. Every selected column must be grouped or aggregated.
- Ignoring NULLs.
SUMignoresNULLvalues, which is usually what you want, but be aware it won't treat them as zero.