How to Use GROUP BY in SQL
Use GROUP BY when rows should be combined into summary groups before applying aggregate functions like COUNT(), SUM(), AVG(), MAX(), or MIN(). This is the foundation of reporting and analytics queries.
What you’ll build or solve
You’ll learn how to use GROUP BY in SQL to summarize rows by one or more columns. You’ll also know how to combine it safely with aggregate functions.
Learn SQL on Mimo
When this approach works best
This approach is the right choice when totals or averages are needed per category instead of across the whole table.
Common real-world scenarios include:
- Revenue by country
- Orders by status
- Users by plan
- Average rating by product
- Highest score by class
This is a bad idea when you need raw row-level details without aggregation.
Prerequisites
You only need:
- A SQL table with rows
- Basic
SELECTand aggregate function knowledge
Step-by-step instructions
Step 1: Group rows before applying aggregates
Place the grouping column after GROUP BY.
SQL
SELECT country, COUNT(*)
FROM users
GROUP BY country;
This returns one row per country.
Combine it with other aggregates.
SQL
SELECT status, SUM(total)
FROM orders
GROUP BY status;
This works especially well for reporting dashboards.
You can also group by multiple columns.
SQL
SELECT country, plan, COUNT(*)
FROM users
GROUP BY country, plan;
What to look for:
- One result row per group
- Aggregates calculate inside each group
- Multiple columns create nested grouping
- Great for reporting queries
- Every non-aggregated selected column must be grouped
Examples you can copy
Orders by status
SQL
SELECT status, COUNT(*)
FROM orders
GROUP BY status;
Revenue by country
SQL
SELECT country, SUM(total)
FROM orders
GROUP BY country;
Average score by class
SQL
SELECT class_id, AVG(score)
FROM exams
GROUP BY class_id;
Common mistakes and how to fix them
Mistake 1: Selecting non-grouped columns
What the reader might do:
SQL
SELECT country, email, COUNT(*)
FROM users
GROUP BY country;
Why it breaks: email is neither grouped nor aggregated.
Corrected approach:
Add it to GROUP BY or remove it.
Mistake 2: Using GROUP BY without aggregates unnecessarily
What the reader might do:
SQL
SELECT country
FROM users
GROUP BY country;
Why it breaks: this works, but DISTINCT may communicate intent more clearly.
Corrected approach:
Use DISTINCT for uniqueness-only queries.
Mistake 3: Grouping too many columns
What the reader might do:
SQL
GROUP BY country, city, plan, email
Why it breaks: over-grouping can fragment the result into almost row-level detail.
Corrected approach:
Group only by the reporting dimension that matters.
Troubleshooting
If SQL errors, check that every normal selected column is grouped.
If the result has too many rows, reduce grouped columns.
If only uniqueness is needed, use DISTINCT.
If totals look wrong, verify the grouping dimension.
Quick recap
- Use
GROUP BYfor summary groups - Combine it with aggregates
- Group every non-aggregated selected column
- Multiple columns create nested groups
- Use
DISTINCTfor uniqueness-only cases
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot