Main content

Google Sheets QUERY: Select Filter and Group

By Szabó Gergő · Updated

QUERY runs a small SQL-like statement against a range. It is the fastest way to filter, sort, and aggregate a sheet without helper columns, if column types are consistent.

Syntax and arguments

=QUERY(data, query, [headers])
data
The source range, including a header row when headers is 1.
query
The statement in quotes, such as "select A, C where B = 'Paid'".
headers
Number of header rows; 1 is typical. -1 lets Sheets guess.

QUERY examples

01
Select and filter

Table is A1:D200 with headers in row 1.

=QUERY(A1:D200,"select A, C, D where B = 'Paid' order by D desc",1)

Returns name, date, and amount for Paid rows, largest amount first.

02
Group totals

Region in B, amount in D.

=QUERY(A1:D200,"select B, sum(D) where D is not null group by B label sum(D) 'Total'",1)

One row per region with a summed amount.

03
Date window

Dates in C, start in G1, end in G2.

=QUERY(A1:D200,"select * where C >= date '"&TEXT(G1,"yyyy-mm-dd")&"' and C <= date '"&TEXT(G2,"yyyy-mm-dd")&"'",1)

QUERY date literals must be yyyy-mm-dd text inside the statement.

Common mistakes

  • Mixed numbers and text in one column

    QUERY types a column from the majority of values. Clean the column or FILTER instead.

  • Wrong header count

    If the first data row is treated as a header, set headers to 0 or 1 explicitly.

  • Unescaped quotes in WHERE

    Wrap text criteria in single quotes inside the double-quoted statement.

QUERY FAQ

Does QUERY exist in Excel?

Not as a worksheet function. Excel users typically use FILTER, SUMIFS, or Power Query.

Why is a numeric column ignored in WHERE?

A few text cells can make QUERY treat the column as text. Convert or split the column.

Can QUERY pull another spreadsheet?

Wrap IMPORTRANGE as the data argument, and grant access once when prompted.