SQL GROUP BY
Create one summary row per customer, material, supplier or category.
How it works
GROUP BY partitions the input rows into groups, then aggregate functions calculate a result for each group. It is the foundation of many operational KPI queries.
Every selected field that is not aggregated should normally be part of the GROUP BY list.
Count orders by operator, sum quantity by material or calculate average delivery time by supplier.
The pattern
SELECT group_column, COUNT(*)
FROM table
GROUP BY group_column;Example
SELECT CustomerID, COUNT(*) AS Orders
FROM Orders
GROUP BY CustomerID
ORDER BY Orders DESC;Run the example
Edit the query and run it against the built-in demo tables. Nothing is sent to a server.
The learning runner supports SELECT, WHERE, LIKE, IN, BETWEEN, GROUP BY, HAVING, JOIN, aggregates, ORDER BY and LIMIT. Advanced lessons explain additional production SQL concepts even when the mini runner does not execute that syntax.
What does GROUP BY do?
Progress is stored only in this browser. No account required.