>_ QueryLocal All recipes
SQL recipe

How do I total a column for each category with SQL GROUP BY?

SELECT country, SUM(quantity * unit_price) AS revenue
FROM data
GROUP BY country
ORDER BY revenue DESC;
Run this query in QueryLocal →

`GROUP BY country` collapses all rows sharing the same country into one row per country, and any aggregate function in the SELECT list (here `SUM`) is computed within each group rather than across the whole table. Adding `ORDER BY revenue DESC` then ranks the groups from highest to lowest total — this is the SQL equivalent of a pivot table's one-column summary.

A table named data with columns: order_id (integer), customer (text), country (text), category (text), quantity (integer), unit_price (real), order_date (text, YYYY-MM-DD).