CodeNest

Counting and grouping

Aggregate functions summarise many rows into one value: COUNT(*), SUM(col), AVG(col), MIN, MAX.

SELECT COUNT(*) FROM products;
SELECT AVG(price) FROM products WHERE category = 'hardware';

GROUP BY computes one aggregate per group:

SELECT category, SUM(price) FROM products GROUP BY category;

Try it

Print each category and how many products it has, ordered by category name:

hardware|2
software|1
SQLite (SQL) in-browser
Loading editor…

Press Ctrl/ + Enter or click Run to see the output here.