LearnSQLSQL GROUP BY
ANALYSIS

SQL GROUP BY

Create one summary row per customer, material, supplier or category.

After this lessonCombine GROUP BY with aggregate functions.

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.

REAL WORK

Count orders by operator, sum quantity by material or calculate average delivery time by supplier.

SYNTAX

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;
TRY IT

Run the example

Edit the query and run it against the built-in demo tables. Nothing is sent to a server.

SQL playground

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.

QUICK CHECK

What does GROUP BY do?

Progress is stored only in this browser. No account required.