Function reference · Array & filter

QUERY

Runs a small SQL-like query over a range — select, filter, sort and group in one formula.

QUERY(data, query, [headers])

QUERY is the most powerful function in Google Sheets and the one with the steepest cliff, because the second argument is a language of its own rather than ordinary formula syntax.

It pays off when a job would otherwise take a stack of nested FILTER, SORT and SUMIF formulas: one QUERY does the lot, and it reads almost like a sentence.

Arguments

ArgumentRequiredWhat it does
data Yes The range to query.
query Yes The query string, in quotes. Columns are referred to by letter: A, B, C.
headers Optional How many header rows the data has. Pass 1 when the first row is headers.

Worked example

RegionRepSales
WestAna1200
EastBen900
WestCara1500
NorthDan600
EastEve1100
=QUERY(A1:C6, "select A, sum(C) where C > 800 group by A order by sum(C) desc", 1)

Returns West 2700, East 2000.

Regions with sales above 800, totalled per region, biggest first. Doing the same thing without QUERY takes a UNIQUE, a SUMIF and a SORT.

Gotchas

Related functions