How do I calculate an average per group in SQL?
SELECT category, AVG(unit_price) AS avg_price
FROM data
GROUP BY category
ORDER BY avg_price DESC;
Run this query in QueryLocal →
`AVG()` is an aggregate function just like `SUM()` or `COUNT()` — combined with `GROUP BY category`, it computes the mean `unit_price` separately for each category instead of one overall average. Use this whenever the question is 'what's typical within each group', not 'what's typical overall'.
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).