# What does GROUP BY do in SQL?

> GROUP BY in SQL puts rows that share a value into one group so SUM or COUNT can total each group. With a revenue-by-month and top-customers example.

Source: https://learn.ynteractive.com/content/glossary/group-by · Updated 2026-10-24 · Free from Stepthrough (https://learn.ynteractive.com)

[Data, Sheets and Gmail](https://learn.ynteractive.com/content/glossary#topic-data-sheets-and-gmail) · [AI glossary](https://learn.ynteractive.com/content/glossary)

Quick answer

GROUP BY is the part of a SQL query that puts rows with the same value (the same month, the same customer) into one group, so that SUM, COUNT or MAX can give one result per group. It is how a list of orders becomes a report.

Last updated October 24, 2026

[Take the free module →](https://learn.ynteractive.com/courses/sql-for-business-reports/2)Module 2 of the free [SQL for Business Reports Jump Start](https://learn.ynteractive.com/courses/sql-for-business-reports) course, about 12 minutes. No account needed.

Updated October 24, 2026 · By [Robert Breen](https://learn.ynteractive.com/content/about)

## Why it matters for a small business

Most business questions are "per something": revenue per month, orders per customer, tickets per agent. In a spreadsheet that is a pivot table or a column of SUMIFs you rebuild every time. In SQL it is one line, GROUP BY month, that works on any number of rows.

Its partner is HAVING, which filters the groups after they are added up: HAVING SUM(amount) >= 3000 keeps only customers whose total reaches $3,000. WHERE, by contrast, filters single rows before the grouping.

You can group by more than one column. GROUP BY region, month gives one row for every region in every month, which is the shape most management reports need. Add ORDER BY to sort the groups, and LIMIT to keep only the top few, and one short query replaces a pivot table, a sort and a manual copy into an email.

## In a real lesson: SQL: Top Customers and Key Accounts with GROUP BY and HAVING

In the free [SQL top customers lesson](https://learn.ynteractive.com/content/sql-top-customers), Lakeside Office Supply (made up) totals its eight Q3 orders with SELECT customer, SUM(amount) AS revenue FROM orders GROUP BY customer: one row per customer.

Adding HAVING SUM(amount) >= 3000 and ORDER BY revenue DESC leaves the two key accounts: Cobalt Legal ($4,440 from 3 orders) and Northside Realty ($4,030 from 2).

[Try this lesson free](https://learn.ynteractive.com/modules/sql-top-customers) or [read the step-by-step guide](https://learn.ynteractive.com/content/sql-top-customers).

## Common confusions

### WHERE vs HAVING

WHERE filters single rows before GROUP BY. HAVING filters the groups after the totals are worked out, so it can use SUM or COUNT.

### GROUP BY vs ORDER BY

GROUP BY combines rows into groups. ORDER BY only sorts the result; it never changes how many rows there are.

## Tips

- Every column in the SELECT line must be in the GROUP BY or inside SUM, COUNT, MAX or MIN.
- The order of the lines is fixed: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT.

## Related terms

[SQL](https://learn.ynteractive.com/content/glossary/sql) · [LEFT JOIN (SQL)](https://learn.ynteractive.com/content/glossary/left-join) · [Pivot table](https://learn.ynteractive.com/content/glossary/pivot-table)

## More Data, Sheets and Gmail terms

[Header row](https://learn.ynteractive.com/content/glossary/header-row) · JSON · [JSON array](https://learn.ynteractive.com/content/glossary/json-array) · Keyword extraction

## Where you use it: free lessons

- [SQL: Top Customers and Key Accounts with GROUP BY and HAVING](https://learn.ynteractive.com/content/sql-top-customers) (DB Fiddle, 12 min)
- [SQL: Build a Monthly Revenue Report from an Orders Table](https://learn.ynteractive.com/content/sql-monthly-revenue-report) (DB Fiddle, 12 min)
- [SQL: Find Customers Who Have Gone Quiet with a LEFT JOIN](https://learn.ynteractive.com/content/sql-quiet-customers) (DB Fiddle, 15 min)
- [Python: Total a Sales Export by Customer in Google Colab](https://learn.ynteractive.com/content/python-sales-report) (Google Colab, 12 min)

## Frequently asked questions

**What is the difference between WHERE and HAVING?**

WHERE filters individual rows before they are grouped. HAVING filters the groups afterwards, so it can test totals such as SUM(amount) >= 3000.

**Can I group by more than one column?**

Yes. GROUP BY c.name, c.manager makes one row for each customer and manager pair.

**Is GROUP BY the same as a pivot table?**

It does the same job: rows in, one total per group out. The difference is that the query is text you can rerun on new data without rebuilding anything.

[All AI glossary terms, A to Z](https://learn.ynteractive.com/content/glossary) · [Free prompt templates](https://learn.ynteractive.com/content/prompts)
