1. Home
  2. Functions
  3. Array
  4. QUERY

QUERY function

Runs a query (select, where, group by, order by, limit, offset, label) over a range with optional header rows.

Syntax

=QUERY(data, query, [headers])

Arguments

ArgumentRequiredDescription
dataRequiredThe range or array to query
queryRequiredThe query text, for example "select A, sum(B) where C > 10 group by A"
headersOptionalNumber of header rows (default: guessed)

How to use QUERY

When QUERY returns more than one value, the result spills into the cells below and to the right. Keep those cells empty, or you get #SPILL!.

QUERY takes 2 required arguments and 1 optional argument. Type =QUERY( in a cell and SheetsBat shows the arguments as you go. Optional arguments are shown in square brackets; leave them out to use their default.

Examples

Each result below was calculated by the SheetsBat formula engine.

=QUERY({"Name","Score";"Ann",5;"Bob",9;"Cy",7}, "select Col1 where Col2 > 6 order by Col2 desc", 1)Result 3 by 1 array
Spilled result
A
1Name
2Bob
3Cy
=QUERY({"a",1;"b",2;"a",3}, "select Col1, sum(Col2) group by Col1 label sum(Col2) 'Total'", 0)Result 3 by 2 array
Spilled result
AB
1Total
2a4
3b2

Every formula, right in your browser

SheetsBat calculates 536 functions, with charts, pivot tables and live collaboration. Free to start.