SQL Server · Querying

GROUP BY

GROUP BY turns many rows into summary rows — totals per category, orders per customer, sales per month.

Beginner 5 min Lesson 8 of 31Examples run on SQL Server 2022 and Azure SQL Database.
Before you start:SELECTAggregate Functions
0/31 in SQL Server
On this page · 5 min · Beginner

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#

SQL Illustrative
SELECT groupColumn, COUNT(*), SUM(x)
FROM Table
GROUP BY groupColumn;

Code example: Products per category#

SQLProducts per category Illustrative
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;
Expected output
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#

  1. Rows are split into three groups by Category.
  2. COUNT(*) counts rows in each group.
  3. MIN, MAX and SUM are calculated separately for each group.
  4. 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#

Selecting a column that is not grouped

SELECT Category, Name … GROUP BY Category fails: which Name should SQL Server show for the group?

Filtering groups in WHERE

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
SQL Illustrative
SELECT Status, COUNT(*) AS Orders FROM Orders GROUP BY Status;  -- Cancelled 1, Delivered 2, Pending 1

Mini quiz#

GROUP BY quizMultiple choice Question 1 of 1

With GROUP BY Category, which SELECT is valid?

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.