>_QueryLocalCSV & JSON tools that run in your browser

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 on the demo dataset →

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).

Want to run it on your own file? Open it with Query CSV with SQL: your file becomes the table data, and you can paste this query with your own column names.

Related recipes