BeginnerCoder

Lesson 1 — GROUP BY in depth

GROUP BY on several columns

You already know GROUP BY with one column. List several columns and you get one result row per combination: per region and product, per year and month. Every non-aggregated column in SELECT must appear in GROUP BY.

SELECT year, month, SUM(amount) FROM payments GROUP BY year, month

These lessons run a real SQLite database through Python's built-in sqlite3 module, in your browser.

Why it matters: real reports are almost always two-dimensional — "sales per store per week".

Want it another way?
Task

Group by both region and product (and sort the same way). Expected: 4 rows, one per region + product pair.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.