The GROUP BY clause in SQL is used to group rows that have the same values into summary rows. It's typically used with aggregate functions like COUNT(), SUM(), AVG(), etc. to perform calculations on each group of rows.
Let's look at our Codémon shop sales data (there are quite a few rows for this example):
Loading data...
For example, if we wanted to know how many sales we've made in each category, we could use GROUP BY with COUNT():
↗️
Loading editor...
↶
↷
This query counts the number of sales records for each unique category. The GROUP BY category part tells SQL to group all rows with the same category value together.
GROUP BY vs ORDER BY
Don't confuse GROUP BY with ORDER BY. GROUP BY combines rows with the same values, while ORDER BY just sorts the results. They serve different purposes and can be used together in the same query.