FormulaCraft

How to use QUERY to sum by group in Excel and Google Sheets

Topic:QUERY (Google Sheets SQL)

Heads up: Excel and Google Sheets do this differently.

Excel
=SUMIF($A$2:$A$6,A2,$B$2:$B$6)
Google Sheets
=QUERY(A2:B6,"SELECT A,SUM(B) GROUP BY A",1)

Verified example

Computed by a real spreadsheet engine on the sample data below.

RegionSales
North120
South80
North200
South150
East90

=SUMIF($A$2:$A$6,A2,$B$2:$B$6)320

Try it with your data

Edit the grid or formula, then run it through a real spreadsheet engine — no signup.

Sample data — click any cell to edit

Runs server-side · free · no signup

Step by step

  1. 1In Google Sheets, enter =QUERY(A2:B6,"SELECT A,SUM(B) GROUP BY A",1) in an empty area. QUERY groups all rows by column A and sums column B for each group.
  2. 2The result is a compact summary table that updates automatically when source data changes.
  3. 3In Excel, use SUMIF for each category: =SUMIF($A$2:$A$6,"North",$B$2:$B$6). Or use a PivotTable for a full grouped summary.
  4. 4In Excel 365+, SUMIF combined with UNIQUE can replicate the dynamic grouped output.

Tips

Need it for your exact data?

Describe your columns in plain English and get the precise formula for your sheet, with the right Excel or Sheets syntax.

Frequently asked

Can QUERY count rows per group instead of summing?

Yes. Replace SUM(B) with COUNT(B) in the QUERY string: SELECT A,COUNT(B) GROUP BY A.

How do I sum by group with two grouping columns in QUERY?

Add the second column to both SELECT and GROUP BY: SELECT A,B,SUM(C) GROUP BY A,B.

More on QUERY (Google Sheets SQL)

See all →

Formulas used

Written and reviewed by FormulaCraft Team. Each formula on this page is run through our verification engine before publishing.

Last reviewed: