GROUP BY
Aggregate data by category
✅ What You Will Learn
GROUP BY divides rows into groups based on one or more columns, then applies an aggregate function to each group separately. This is how you answer "total sales per product", "number of orders per customer", "average salary per department".
It is one of the most used clauses in real analytics work. Almost every summary report — daily revenue by region, monthly orders by product category, top customers by spend — uses GROUP BY.
📋 The orders table used in examples
| order_id | customer_name | product | amount | city |
|---|---|---|---|---|
| 1001 | Rahul Sharma | Laptop | 45000 | Delhi |
| 1002 | Priya Verma | Mobile Phone | 18000 | Noida |
| 1003 | Amit Kumar | Headphones | 3500 | Gurgaon |
| 1004 | Sneha Kapoor | Laptop | 52000 | Delhi |
| 1005 | Vikram Singh | Tablet | 28000 | Noida |
| 1006 | Neha Joshi | Laptop | 48000 | Delhi |
Syntax
Examples
📌 Key Points to Remember
- ✓GROUP BY splits rows into groups before aggregation
- ✓Every non-aggregated column in SELECT must be in GROUP BY
- ✓ORDER BY can sort the grouped result
- ✓GROUP BY runs after WHERE — WHERE filters rows, then GROUP BY groups them
- ✓You can GROUP BY multiple columns to get more granular breakdowns
🏢 Real-World Application
GROUP BY is what transforms raw transactional data into business intelligence. "Total sales by region" — GROUP BY region, SUM(sales). "Number of orders per customer" — GROUP BY customer_id, COUNT(*). "Average order value by product category" — GROUP BY category, AVG(order_value). Every pivot table in Excel is essentially a GROUP BY query. Power BI and Tableau dashboards that show data broken down by dimension (city, product, month, salesperson) are all running GROUP BY queries behind the scenes. A data analyst who cannot write GROUP BY queries cannot build reports — it is that fundamental.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
What does GROUP BY do in SQL?
GROUP BY collapses multiple rows that share the same value in a column into a single summary row. It is always used with aggregate functions like COUNT, SUM, AVG, MIN, MAX to calculate totals per group.
Can I GROUP BY multiple columns in SQL?
Yes. GROUP BY col1, col2 creates groups for each unique combination of col1 and col2. For example, GROUP BY product, city groups by product within each city, giving revenue per product per city.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY. You must use HAVING (not WHERE) with aggregate functions: HAVING SUM(amount) > 50000.
Can I use ORDER BY with GROUP BY?
Yes. Add ORDER BY at the end to sort grouped results. A common pattern is GROUP BY product ORDER BY SUM(amount) DESC to show products ranked by total revenue.
What happens to NULL values in GROUP BY?
NULL values are grouped together into their own group. All rows where the GROUP BY column is NULL will be combined into one group, and the aggregate functions will be applied to that NULL group.
✏️ Practice Exercise
Write a query to find the number of orders and total revenue for each product, only for orders above ₹5,000, sorted by total revenue highest first.
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.