10. GROUP BY: a summary for each group
Split the rows into groups and sum up each one: customers per city, sales per product, per month.
Splitting rows into groups with GROUP BY
In the last chapter count(*) found 10 customers. To know how many live in each city, the database has to split the rows into groups by city first, then count inside each group. That is what GROUP BY does:
SELECT city, count(*) AS customers
FROM customer
GROUP BY city;| city | customers |
|---|---|
| Bangkok | 4 |
| Chiang Mai | 2 |
| Khon Kaen | 2 |
| Phuket | 2 |
Rows with the same city go into one group, and each group comes out as one row. The 10 customers become 4 rows: Bangkok has 4, and Chiang Mai, Khon Kaen and Phuket 2 each.
Now city beside count(*) means something: inside a group, city has just one value.
Orders by status:
SELECT status, count(*) AS orders
FROM orders
GROUP BY status;| status | orders |
|---|---|
| cancelled | 2 |
| paid | 8 |
| shipped | 5 |
8 paid, 5 shipped, 2 cancelled: 15 orders in all.
Several summaries for each group
Every aggregate from chapter 9 works on groups, and you can use several at once. The products of each category:
SELECT category_id, count(*) AS products, min(price), max(price), sum(stock) AS in_stock
FROM product
GROUP BY category_id;| category_id | products | min(price) | max(price) | in_stock |
|---|---|---|---|---|
| 1 | 3 | 40.0 | 55.0 | 200 |
| 2 | 3 | 35.0 | 120.0 | 50 |
| 3 | 3 | 25.0 | 150.0 | 265 |
| 4 | 3 | 15.0 | 30.0 | 375 |
Each category has 3 products. Category 3 (snacks) has the widest spread of prices, from 25 to 150 baht.
Sales per product, from order_item, best first with ORDER BY (which comes after GROUP BY):
SELECT product_id, sum(qty) AS sold, sum(qty * price) AS sales
FROM order_item
GROUP BY product_id
ORDER BY sales DESC, product_id;| product_id | sold | sales |
|---|---|---|
| 10 | 18 | 540.0 |
| 11 | 35 | 525.0 |
| 1 | 7 | 385.0 |
| 6 | 3 | 360.0 |
| 8 | 4 | 360.0 |
| 9 | 2 | 300.0 |
| 7 | 11 | 275.0 |
| 2 | 5 | 225.0 |
| 4 | 6 | 210.0 |
| 5 | 3 | 180.0 |
| 3 | 2 | 80.0 |
Product 10 (Notebook) made the most money, 540 baht, but product 11 (Pen) sold the most items, 35. Product 12 doesn't appear at all: it never sold, so order_item has no row of it to group.
Products 6 and 8 both sold 360 baht, so product_id follows in ORDER BY to make the order certain every time. Without it, rows with equal values may come in any order.
Grouping by several columns, or by an expression
GROUP BY takes several columns; rows share a group when every one of them matches. How many orders of each status each customer has:
SELECT customer_id, status, count(*) AS orders
FROM orders
GROUP BY customer_id, status;| customer_id | status | orders |
|---|---|---|
| 1 | paid | 2 |
| 1 | shipped | 1 |
| 2 | paid | 1 |
| 2 | shipped | 1 |
| 3 | cancelled | 1 |
| 3 | paid | 1 |
| 4 | cancelled | 1 |
| 4 | paid | 1 |
| 5 | shipped | 1 |
| 6 | paid | 1 |
| 6 | shipped | 1 |
| 7 | paid | 2 |
| 8 | shipped | 1 |
Customer 1 has two rows: 2 paid and 1 shipped. The 15 orders make 13 groups.
An expression works too. By month, cutting the date down with strftime('%Y-%m', ...) from chapter 8:
SELECT strftime('%Y-%m', ordered) AS month, count(*) AS orders
FROM orders
GROUP BY month;| month | orders |
|---|---|
| 2026-09 | 4 |
| 2026-10 | 6 |
| 2026-11 | 5 |
4 in September, 6 in October, 5 in November. Note that GROUP BY month uses the name given with AS: SQLite and Phai SQL allow it. For SQL that may move to another database, repeat the whole expression in GROUP BY instead.
Sales per month and per category
Sales per month need two tables: the date is in orders, the quantity and price in order_item. Putting two tables together is a JOIN, which chapter 11 explains step by step. For now, just see that GROUP BY works on a join's result as it does on one table:
SELECT strftime('%Y-%m', o.ordered) AS month, sum(i.qty * i.price) AS sales
FROM orders AS o
JOIN order_item AS i ON i.order_id = o.id
WHERE o.status <> 'cancelled'
GROUP BY month;| month | sales |
|---|---|
| 2026-09 | 820.0 |
| 2026-10 | 1450.0 |
| 2026-11 | 940.0 |
Leaving out cancelled orders: 820 baht in September, 1,450 in October, 940 in November.
Sales per category, with the category's name from category:
SELECT c.name AS category, sum(i.qty * i.price) AS sales
FROM order_item AS i
JOIN product AS p ON p.id = i.product_id
JOIN category AS c ON c.id = p.category_id
GROUP BY c.name
ORDER BY sales DESC;| category | sales |
|---|---|
| Stationery | 1065.0 |
| Snacks | 935.0 |
| Bakery | 750.0 |
| Drinks | 690.0 |
Stationery sold the most, 1,065 baht, and drinks the least, 690 baht (this one doesn't leave out cancelled orders).
HAVING: filtering groups
WHERE filters rows, before the grouping. HAVING filters groups, after the summing up, so it can use aggregates. Orders worth 300 baht or more:
SELECT order_id, sum(qty * price) AS total
FROM order_item
GROUP BY order_id
HAVING sum(qty * price) >= 300;| order_id | total |
|---|---|
| 4 | 300.0 |
| 8 | 390.0 |
| 9 | 490.0 |
| 11 | 300.0 |
| 15 | 375.0 |
5 orders; order 9 is the largest, at 490 baht.
Both can be in one statement. Customers with 2 or more orders, not counting cancelled ones:
SELECT customer_id, count(*) AS orders
FROM orders
WHERE status <> 'cancelled'
GROUP BY customer_id
HAVING count(*) >= 2;| customer_id | orders |
|---|---|
| 1 | 3 |
| 2 | 2 |
| 6 | 2 |
| 7 | 2 |
WHERE drops the cancelled orders (2 of them) first; then the groups are counted; last, HAVING keeps the groups that counted 2 or more. That leaves 4 customers. Customers 3 and 4 drop out, because their other order was cancelled.
A condition that has nothing to do with a summary belongs in WHERE, not HAVING. The result is the same, but WHERE drops the rows at the start, so the database does less work, and the statement is easier to read. A condition on count, sum and the like can only go in HAVING.
The order you write and the order it works
A SELECT's parts are always written in this order, but the database works them out in another:
| Written | Worked out | What it does |
|---|---|---|
1. SELECT | 5 | picks columns, works out expressions, names them |
2. FROM | 1 | reads the tables (and JOINs) |
3. WHERE | 2 | filters rows |
4. GROUP BY | 3 | groups and sums up |
5. HAVING | 4 | filters groups |
6. ORDER BY | 6 | sorts |
7. LIMIT | 7 | keeps only some rows |
The working order explains a lot: WHERE can't use count(*) because nothing is grouped yet; ORDER BY can use the names given in SELECT because it comes after; and LIMIT cuts after the sorting, so you get the right "top few". The 3 best-selling products:
SELECT product_id, sum(qty * price) AS sales
FROM order_item
GROUP BY product_id
ORDER BY sales DESC
LIMIT 3;| product_id | sales |
|---|---|
| 10 | 540.0 |
| 11 | 525.0 |
| 1 | 385.0 |
A bare column in a grouped query
Standard SQL's rule: in a statement with GROUP BY, every column in SELECT must be one of the grouping columns or sit inside an aggregate. SQLite and Phai SQL don't enforce it:
SELECT customer_id, ordered, count(*) AS orders
FROM orders
GROUP BY customer_id;| customer_id | ordered | orders |
|---|---|---|
| 1 | 2026-09-02 | 3 |
| 2 | 2026-09-05 | 2 |
| 3 | 2026-09-20 | 2 |
| 4 | 2026-10-01 | 2 |
| 5 | 2026-10-03 | 1 |
| 6 | 2026-10-15 | 2 |
| 7 | 2026-10-22 | 2 |
| 8 | 2026-11-02 | 1 |
Customer 1 ordered 3 times (2 and 12 September, 28 October), but ordered shows only 2026-09-02: the value of some row in the group, not the first or latest date chosen on purpose.
Don't rely on a bare column like this. It isn't an error, so nothing warns you, but the value means nothing, and other databases refuse the statement. For the latest order date, say so: max(ordered).
SELECT customer_id, min(ordered) AS first_order, max(ordered) AS last_order, count(*) AS orders
FROM orders
GROUP BY customer_id;| customer_id | first_order | last_order | orders |
|---|---|---|---|
| 1 | 2026-09-02 | 2026-10-28 | 3 |
| 2 | 2026-09-05 | 2026-10-10 | 2 |
| 3 | 2026-09-20 | 2026-11-18 | 2 |
| 4 | 2026-10-01 | 2026-11-05 | 2 |
| 5 | 2026-10-03 | 2026-10-03 | 1 |
| 6 | 2026-10-15 | 2026-11-11 | 2 |
| 7 | 2026-10-22 | 2026-11-25 | 2 |
| 8 | 2026-11-02 | 2026-11-02 | 1 |
- Count the products of each category (
category_id), only those still in stock (stock > 0). - How many items (
sum(qty)) each order has, most first; equal ones by order number. - Which cities have more than 2 customers?
- Sales per product (
product_id), only products that sold 300 baht or more altogether.
Show the answer
SELECT category_id, count(*) FROM product WHERE stock > 0 GROUP BY category_id;
SELECT order_id, sum(qty) AS items FROM order_item GROUP BY order_id ORDER BY items DESC, order_id;
SELECT city, count(*) FROM customer GROUP BY city HAVING count(*) > 2;
SELECT product_id, sum(qty * price) AS sales FROM order_item GROUP BY product_id HAVING sales >= 300;| category_id | count(*) |
|---|---|
| 1 | 2 |
| 2 | 3 |
| 3 | 3 |
| 4 | 2 |
| order_id | items |
|---|---|
| 8 | 23 |
| 4 | 15 |
| 15 | 15 |
| 9 | 8 |
| 6 | 7 |
| 11 | 7 |
| 1 | 4 |
| 2 | 4 |
| 7 | 3 |
| 12 | 3 |
| 3 | 2 |
| 14 | 2 |
| 5 | 1 |
| 10 | 1 |
| 13 | 1 |
| city | count(*) |
|---|---|
| Bangkok | 4 |
| product_id | sales |
|---|---|
| 1 | 385.0 |
| 6 | 360.0 |
| 8 | 360.0 |
| 9 | 300.0 |
| 10 | 540.0 |
| 11 | 525.0 |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made