GROUP BY
GROUP BY turns many rows into summary rows — totals per category, orders per customer, sales per month.
On this page · 5 min · Beginner
On this page
- In simple words
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 27, 2026
In simple words#
GROUP BY puts students into piles by class, then counts or totals each pile: “How many students and total fees per class?”
- GROUP BY
- Make one row per group.
- Aggregate
- COUNT, SUM, AVG, MIN, MAX per group.
- Rule
- Selected columns must be grouped or aggregated.
Learning objectives#
- Group rows by one or more columns
- Compute aggregates per group
- Know which columns you may select
- Group by expressions such as month
Explanation#
GROUP BY collects rows that share the same values into one group. Aggregate functions — COUNT, SUM, AVG, MIN, MAX — then calculate one value per group.
Every column in the SELECT list must either be in the GROUP BY or be inside an aggregate function.
You can group by expressions, for example YEAR(OrderDate) and MONTH(OrderDate) for monthly reports.
Syntax#
SELECT groupColumn, COUNT(*), SUM(x)
FROM Table
GROUP BY groupColumn;
Code example: Products per category#
SELECT Category,
COUNT(*) AS Products,
MIN(Price) AS Cheapest,
MAX(Price) AS Priciest,
SUM(Price) AS TotalValue
FROM Products
GROUP BY Category
ORDER BY Category;
Category Products Cheapest Priciest TotalValue Electronics 2 700.00 55000.00 55700.00 Furniture 2 4500.00 12000.00 16500.00 Stationery 2 20.00 60.00 80.00
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Rows are split into three groups by Category.
- COUNT(*) counts rows in each group.
- MIN, MAX and SUM are calculated separately for each group.
- ORDER BY sorts the summary rows.
Real-world use case#
A sales dashboard shows revenue per month and per region — both are GROUP BY queries.
Common mistakes#
SELECT Category, Name … GROUP BY Category fails: which Name should SQL Server show for the group?
WHERE runs before grouping and cannot use aggregates. Use HAVING to filter groups.
Best practices#
- Group by the smallest set of columns that answers the question
- Name aggregate columns with aliases
- Filter rows in WHERE before grouping to reduce work
- Use HAVING only for conditions on aggregates
Remember this#
GROUP BY = one row per group.
Every selected column: grouped or aggregated.
COUNT, SUM, AVG, MIN, MAX work per group.
Practice exercise#
Your task: Count how many orders each status has.
Show solution
SELECT Status, COUNT(*) AS Orders FROM Orders GROUP BY Status; -- Cancelled 1, Delivered 2, Pending 1
Mini quiz#
With GROUP BY Category, which SELECT is valid?
Want more? Practise all SQL Server MCQs · SQL Server interview questions
My notes#
Saved in this browser.