Beginner 8 min read

QUERY GROUP BY in Google Sheets (Summarize Data)

Learn how to use QUERY GROUP BY in Google Sheets to summarize data with SUM, COUNT, AVG, MIN, and MAX. Step-by-step examples and common mistakes.

SB

Sheets Bootcamp

March 2, 2026

QUERY GROUP BY in Google Sheets collapses rows into groups and applies aggregate functions like SUM, COUNT, and AVG to each group. It turns a table of individual transactions into a summary report — one row per category, region, or person. It is one of the most useful clauses in the QUERY function and does the work of multiple SUMIF formulas in a single formula.

In This Guide

GROUP BY Syntax

GROUP BY appears after WHERE (if used) in the query string:

Formula
=QUERY(data, "SELECT column, AGGREGATE(column) GROUP BY column")

Every column in SELECT must either be in the GROUP BY clause or wrapped in an aggregate function. You cannot include a raw column that is not grouped or aggregated.

Important

Every non-aggregated column in SELECT must appear in GROUP BY. SELECT B, SUM(F) GROUP BY C breaks because B is not grouped. Either change it to SELECT C, SUM(F) GROUP BY C or add B to GROUP BY.

How to Use GROUP BY: Step-by-Step

We’ll use the sales records table from the QUERY guide. The goal: total revenue by region.

Sample Data

ABCDEFG
1DateSalespersonRegionProductUnitsRevenueCommission
21/5/2026Fred WeasleyDiagon AlleyExtendable Ears12$239.88$24.00
31/7/2026Ginny WeasleyHogsmeadeSelf-Stirring Cauldron8$360.00$36.00
41/8/2026Lee JordanDiagon AlleyRemembrall15$525.00$52.50
51/10/2026Fred WeasleyDiagon AlleyOmnioculars5$325.00$32.50
61/12/2026George WeasleyHogsmeadeSneakoscope20$570.00$57.00
71/14/2026Ginny WeasleyHogwartsNimbus 200025$624.75$62.50
81/15/2026Lee JordanHogsmeadeExtendable Ears10$199.90$20.00
91/18/2026Fred WeasleyDiagon AlleyInvisibility Cloak3$269.97$27.00
101/20/2026George WeasleyHogwartsFirebolt8$336.00$33.60
111/22/2026Ginny WeasleyHogsmeadeDeluminator6$330.00$33.00

Sales records table with 10 transactions in Google Sheets

1

Review your data range

Your data is in A1:G11. Row 1 is the header. The Region column (C) has three unique values: Diagon Alley, Hogsmeade, and Hogwarts.

2

Write a GROUP BY query with SUM

Select an empty cell and enter:

Formula
=QUERY(A1:G11, "SELECT C, SUM(F) GROUP BY C")

QUERY groups the 10 rows by Region and totals the Revenue for each group:

Regionsum Revenue
Diagon Alley$1,359.85
Hogsmeade$1,459.90
Hogwarts$960.75

QUERY GROUP BY showing total revenue per region

3

Add a LABEL clause

The default header “sum Revenue” is not readable. Add LABEL to rename it:

Formula
=QUERY(A1:G11, "SELECT C, SUM(F) GROUP BY C LABEL SUM(F) 'Total Revenue'")

The header now reads “Total Revenue” instead of “sum Revenue.”

QUERY GROUP BY with LABEL renaming the sum column header

4

Sort the results

Add ORDER BY to sort by total revenue, highest first:

Formula
=QUERY(A1:G11, "SELECT C, SUM(F) GROUP BY C ORDER BY SUM(F) DESC LABEL SUM(F) 'Total Revenue'")

Hogsmeade ($1,459.90) now appears first, followed by Diagon Alley ($1,359.85) and Hogwarts ($960.75).

QUERY GROUP BY sorted by total revenue descending

Tip

ORDER BY in a GROUP BY query sorts by the aggregated values, not the individual row values. ORDER BY SUM(F) DESC sorts regions by their total revenue, not by any single transaction.

Aggregate Functions Reference

FunctionWhat It ReturnsExample
SUM(col)Total of all valuesSUM(F) — total revenue
COUNT(col)Number of non-empty cellsCOUNT(D) — number of transactions
AVG(col)Arithmetic averageAVG(F) — average revenue per transaction
MIN(col)Smallest valueMIN(F) — lowest revenue
MAX(col)Largest valueMAX(F) — highest revenue

Multiple Aggregations

You can include multiple aggregate functions in a single SELECT:

Formula
=QUERY(A1:G11, "SELECT C, SUM(F), AVG(F), COUNT(F) GROUP BY C")

This returns a summary table with total revenue, average revenue, and transaction count per region — all in one formula.

QUERY GROUP BY with SUM, AVG, and COUNT in one formula

COUNT Example

Formula
=QUERY(A1:G11, "SELECT B, COUNT(D) GROUP BY B LABEL COUNT(D) 'Sales Count'")

This counts transactions per salesperson. Fred Weasley: 3. Ginny Weasley: 3. George Weasley: 2. Lee Jordan: 2.

AVG Example

Formula
=QUERY(A1:G11, "SELECT C, AVG(F) GROUP BY C LABEL AVG(F) 'Avg Revenue'")

This returns the average revenue per transaction for each region.

GROUP BY with Multiple Columns

GROUP BY accepts multiple columns separated by commas. This creates one row for each unique combination:

Formula
=QUERY(A1:G11, "SELECT B, C, SUM(F) GROUP BY B, C LABEL SUM(F) 'Revenue'")

This groups by both Salesperson and Region. Fred Weasley in Diagon Alley gets his own row ($834.85). George Weasley in Hogsmeade gets a separate row ($570.00). Each unique pair is its own group.

QUERY GROUP BY two columns showing salesperson-region combinations

Note

Grouping by multiple columns increases the number of rows in the result. Grouping by Region alone gives 3 rows. Grouping by Salesperson and Region gives up to 12 rows (4 people times 3 regions), though only combinations that exist in the data appear.

GROUP BY + WHERE (Filter Before Grouping)

WHERE runs before GROUP BY. Use it to exclude rows before they get summarized:

Formula
=QUERY(A1:G11, "SELECT C, SUM(F) WHERE F > 300 GROUP BY C LABEL SUM(F) 'Revenue (>$300)'")

This first filters out transactions under $300, then groups the remaining rows by region. The totals are lower because small transactions are excluded.

RegionRevenue (>$300)
Diagon Alley$850.00
Hogsmeade$1,260.00
Hogwarts$960.75

Only transactions over $300 are included in each total. Diagon Alley drops from $1,359.85 to $850.00 because two transactions ($239.88 and $269.97) are under the $300 threshold.

The WHERE clause guide covers all filter types — text, numeric, date, AND/OR.

GROUP BY + ORDER BY (Sort the Summary)

ORDER BY works with aggregate results. You can sort by the grouped column or by an aggregated value:

Sort by aggregate value

Formula
=QUERY(A1:G11, "SELECT C, SUM(F) GROUP BY C ORDER BY SUM(F) DESC")

Sorts regions by total revenue, highest first.

Sort by grouped column

Formula
=QUERY(A1:G11, "SELECT C, SUM(F) GROUP BY C ORDER BY C ASC")

Sorts regions alphabetically: Diagon Alley, Hogwarts, Hogsmeade.

Rename Headers with LABEL

Aggregated columns get automatic headers like “sum Revenue” or “avg Revenue.” LABEL lets you rename them:

Formula
=QUERY(A1:G11, "SELECT C, SUM(F), COUNT(F) GROUP BY C LABEL SUM(F) 'Total Revenue', COUNT(F) 'Transactions'")

This renames both aggregated columns. Separate multiple LABEL assignments with commas.

You can also rename the grouped column:

Formula
=QUERY(A1:G11, "SELECT C, SUM(F) GROUP BY C LABEL C 'Sales Region', SUM(F) 'Total Revenue'")
Note

LABEL text uses single quotes inside the query string: LABEL SUM(F) 'Total Revenue'. Double quotes would conflict with the outer formula quotes.

Common Mistakes

Non-aggregated column not in GROUP BY

SELECT B, C, SUM(F) GROUP BY C breaks because B is in SELECT but not in GROUP BY and not aggregated. Either add B to GROUP BY (GROUP BY B, C) or wrap it in an aggregate function (COUNT(B)).

Using GROUP BY without an aggregate function

SELECT C GROUP BY C works but returns the same result as SELECT DISTINCT C. GROUP BY is designed for aggregation. If you only need unique values, SELECT C GROUP BY C gets the job done, but know that it is a pattern for deduplication.

Wrong column letter in aggregate function

SELECT C, SUM(B) GROUP BY C tries to sum the Salesperson column, which contains text. This returns null or zero for every group. Make sure the column inside the aggregate function contains numeric data.

Forgetting LABEL for readable headers

The default headers “sum Revenue”, “avg Revenue”, and “count Product” are technically correct but not presentation-ready. Always add a LABEL clause when sharing the output.

Tips

1. Use GROUP BY instead of multiple SUMIF formulas. A single QUERY formula with GROUP BY produces the same summary table that would take one SUMIF formula per row. When you have more than 3 categories, GROUP BY is more efficient.

2. Add WHERE before GROUP BY to filter outliers. If a few extreme values skew your averages, filter them out first: WHERE F > 0 AND F < 10000 GROUP BY C.

3. Combine GROUP BY with LIMIT for “top N” reports. GROUP BY C ORDER BY SUM(F) DESC LIMIT 3 returns only the top 3 regions by revenue.

4. Use QUERY SELECT to control which columns appear. GROUP BY determines the grouping. SELECT determines which columns and aggregations appear in the output.

Tip

For a visual summary, build a chart from your GROUP BY output. Select the result table and insert a bar or pie chart. The chart updates automatically when the source data changes.

Frequently Asked Questions

What does GROUP BY do in Google Sheets QUERY?

GROUP BY groups rows that share the same value in a column and applies aggregate functions like SUM, COUNT, or AVG to the other columns. For example, GROUP BY C with SUM(F) totals revenue for each unique region.

Can you GROUP BY multiple columns in QUERY?

Yes. List multiple columns after GROUP BY separated by commas. For example, GROUP BY B, C groups rows by both Salesperson and Region, creating one row for each unique combination.

What aggregate functions work with QUERY GROUP BY?

QUERY supports five aggregate functions: SUM (total), COUNT (number of rows), AVG (average), MIN (smallest value), and MAX (largest value). Use them in the SELECT clause with the column letter in parentheses.

How do you rename GROUP BY column headers in QUERY?

Use the LABEL clause at the end of the query string. For example, LABEL SUM(F) 'Total Revenue' renames the default “sum Revenue” header to “Total Revenue”.

What is the difference between QUERY GROUP BY and SUMIF?

SUMIF totals one value for one condition. QUERY GROUP BY returns a complete summary table with rows for every unique value, multiple aggregations, and custom column headers in a single formula. Use SUMIF for a single total and QUERY GROUP BY for a full summary.

Frequently Asked Questions

Next Steps

Continue learning with these related tutorials: