SQL GROUP BY for Absolute Beginners (Explained Slowly & Clearly)
If you’ve ever looked at a report — like “Total sales per customer” or “Number of logins per day” — then you’ve already seen what GROUP BY does.
GROUP BY turns messy, unorganized rows into clean summaries.
And the best part?
It’s easier than most beginners think.
Let’s walk through this together.
⭐ Why Do We Use GROUP BY?
Imagine a table named orders:
| id | customer | product | amount |
|---|---|---|---|
| 1 | Alice | Laptop | 1200 |
| 2 | Alice | Mouse | 25 |
| 3 | Bob | Keyboard | 50 |
| 4 | Alice | Monitor | 200 |
If you want to answer:
-
How many products did each customer buy?
-
How much total money did each customer spend?
You need GROUP BY.
It groups rows based on a column, then applies functions like:
-
COUNT() -
SUM() -
AVG() -
MAX() -
MIN()
⭐ 1. Count How Many Orders Each Customer Has
Result:
| customer | total_orders |
|---|---|
| Alice | 3 |
| Bob | 1 |
Simple, right?
⭐ 2. Sum Total Spending Per Customer
Result:
| customer | total_spent |
|---|---|
| Alice | 1425 |
| Bob | 50 |
GROUP BY + SUM is one of the most common combinations in real-world apps.
⭐ 3. Average Amount Spent Per Order (Per Customer)
This is useful for analytics dashboards or business intelligence reports.
⭐ 4. Count Orders per Product
You can group by ANY column — not just customer.
⭐ Common GROUP BY Mistake (Every Beginner Does This)
❌ You must include every non-aggregated column in GROUP BY.
This will fail:
Because product is not grouped or aggregated.