Ad space (banner)
🗄️SQL Lessons
Lesson 25 / 50

GROUP BY with Multiple Columns

This lesson covers grouping by multiple columns at once, so you can analyze data at a finer level of detail. It's for anyone searching "SQL GROUP BY multiple columns."

Specifying GROUP BY col1, col2 groups by the combination of both columns — like "per class, and within that, per gender" — giving you a finer-grained breakdown than a single column allows.

The example groups by class_id, gender to count students for every class-and-gender combination. Compare this against grouping by just one column to really see how the number of result rows changes.

A common early mistake is confusing the order of columns in GROUP BY with the order results are displayed in — GROUP BY's column order only determines the grouping level, not the display order; for that you still need a separate ORDER BY. A single-column grouping is often too coarse for a real report, but combining columns gets you the kind of fine-grained breakdown that shows up constantly in sales analysis — and the order you list the columns in changes how the results read.

SQL
OUTPUT

💡 The SQL engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)