🗃️GROUP BY Clause
Also known as GROUP BY, SQL GROUP BY, Grouping in SQL
The SQL part that puts rows with the same value into groups.
Think of it like
It is like putting socks in piles by colour. Then you count each pile.
Example
Lucy wants sales per city. She groups her orders by city and sums the amounts, and gets one line for each city.
Why it matters
Most reports need totals per something: per day, per shop, per product. GROUP BY is how SQL makes them.
Definition
Example code
sql
SELECT city, SUM(amount) AS total
FROM orders
GROUP BY city;Common mistake
Key takeaways
- 1Same value, one group. Rows that match are put together.
- 2One answer per group. An aggregate sums or counts each one.
- 3HAVING filters groups. WHERE filters rows, before grouping.