Stop 05 · Aggregation & GroupingTopics · 5/9

Grouping by Multiple Columns

Part of the SQL Roadmap

In short

Managers rarely ask for 'sales per city'. They ask for 'sales per city per month' or 'orders per store per category'. Adding a second column to GROUP BY is how you answer those.

Grouping by several columns creates one group per unique combination, such as sales per city per month.

The jaadu climbs a wooden ladder in a busy shoe shop, sliding shoe boxes into a wall of cubbyholes sorted by colour across and size down.

Think of it like this

The shoe shop shelves

A shoe shop in Linking Road doesn't keep one pile per brand. It keeps one box shelf for every brand-and-size pair: Bata size 7, Bata size 8, Puma size 7. Each pair gets its own slot, and you count the boxes in each slot. GROUP BY brand, size does exactly that.

Watch it happen

Six orders grouped by city and month

Pune·Jan ₹500
0
Pune·Jan ₹300
1
Pune·Feb ₹200
2
Delhi·Jan ₹400
3
Delhi·Feb ₹100
4
Delhi·Feb ₹600
5

1/5Six orders, each with a city, a month and an amount.

The big ideas

What you need to know

One group per combination

GROUP BY city, month makes one row for every city-month pair that actually exists in the data.

More columns, more rows

Each extra grouping column splits groups further. 10 cities and 12 months can give up to 120 rows.

Missing pairs vanish

If Nagpur had no orders in March, there is no Nagpur-March row at all, not a zero. You need a join to a full grid to show zeros.

Order is for display

GROUP BY city, month and GROUP BY month, city make the same groups. ORDER BY decides how they are shown.

See it in code

Walk through the code

sql
1SELECT2  city,3  DATE_TRUNC('month', order_date)::date AS month,4  COUNT(*)    AS orders,5  SUM(amount) AS revenue6FROM orders7WHERE order_date >= DATE '2026-01-01'8GROUP BY city, DATE_TRUNC('month', order_date)9ORDER BY city, month;

1/4The two grouping columns. Every order date is rounded down to the first of its month.

Orders and revenue per city per month for 2026. In MySQL, replace DATE_TRUNC with DATE_FORMAT(order_date, '%Y-%m-01').

Save yourself the pain

Common mistakes

Oops

GROUP BY city, order_date when you wanted monthly numbers.

Do this instead

Group by DATE_TRUNC('month', order_date). Grouping by the raw date gives one row per day.

Oops

Grouping by EXTRACT(MONTH FROM order_date) across two years.

Do this instead

January 2025 and January 2026 would merge. Group by DATE_TRUNC('month', ...) or by both year and month.

Oops

Assuming every city appears in every month.

Do this instead

Count the rows you get and compare with cities times months. Missing pairs had no orders.

Your mission

A city-by-month table

~20 min

Quick check

Orders come from 3 cities and 2 payment methods, and every combination has at least one order. How many rows does GROUP BY city, payment_method return?

Go deeper

  1. 1Group by year and month to build a monthly trend.
  2. 2Notice how the row count grows as you add grouping columns.
  3. 3Check for combinations you expected but don't see, and ask why.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge