SQL Aggregates and GROUP BY

An aggregate function such as SUM, COUNT, AVG, MIN, or MAX combines many rows into one value. GROUP BY splits the rows into groups first, so each group produces one result row.

SQL
CREATE TABLE sales (item TEXT, qty INTEGER, price REAL);
INSERT INTO sales VALUES ('pen', 10, 1.5), ('pen', 5, 1.5), ('book', 2, 12.0);

SELECT item, SUM(qty) AS total_qty, COUNT(*) AS orders
FROM sales
GROUP BY item
ORDER BY item;
Read the full lesson