AI · 2 min read

QUERY

QUERY function in Google Sheets: Runs a database-style query on a range.

Google function · Google Sheets

QUERY function

QUERY runs a database-style statement on a range. One formula can filter, group, sort and total data.

Syntax

=QUERY(data, query, [headers])
On Excel: use GROUPBY. GROUPBY and PIVOTBY cover grouped totals. Power Query suits larger jobs.

Arguments

ArgumentRequiredMeaning
dataYesThe range to query.
queryYesThe statement in quotes, using column letters.
headersNoHow many top rows are headers.

Worked example

ABC
1RegionMonthSales
2EastJan1200
3WestJan950
4EastFeb1400
5WestFeb800
6EastFeb300
=QUERY(A1:C6,"select A, sum(C) group by A label sum(C) 'Total'",1)
Result: East 2900, West 1750

The statement groups rows by Region and adds the Sales column under a Total label.

Try it

QUERY is exclusive to Google Sheets, so the practice engine cannot run it. The result was checked by hand.

Availability

PlatformStatus
ExcelNot available. Use GROUPBY, PIVOTBY or Power Query.
Google SheetsAvailable.

Common mistakes

  • Mixed text and numbers in a column can lead to blank results.
  • Column letters must be capitals when the data is a range reference.
  • Text values use single quotes inside the double-quoted statement.

Do it with AI

Paste this prompt into Copilot, Gemini or another assistant. Check the result on a small sample first.

Write a QUERY for A1:E500 that totals column E by column B for rows where column C is "Paid", sorted from highest total.