How to Use the QUERY Function in Google Sheets

QUERY runs SQL-style statements against your sheet data. The syntax is =QUERY(data, query, [headers]) — so =QUERY(A1:C100, "select A, C where B > 10") returns columns A and C, only for rows where B is above 10. It replaces a stack of filters and helper columns with one formula.

The three arguments

ArgumentWhat it isExample
dataThe range to queryA1:C100
queryThe statement, in quotes"select A, C where B > 10"
headersHow many header rows the data has1

The headers argument is optional but worth setting — 1 tells Sheets your first row holds column names.

Columns are letters, not Col numbers

This is the key difference from QUERY-inside-IMPORTRANGE:

Data sourceRefer to columns as
A range in the same sheet (A1:C100)A, B, C
Imported data (IMPORTRANGE(...))Col1, Col2, Col3

Get this wrong and you’ll see #VALUE!. If your QUERY wraps an IMPORTRANGE, you must use Col1 style. See how to use IMPORTRANGE in Google Sheets for that combination.

Worked examples

Select specific columns:

=QUERY(A1:D100, "select A, C", 1)

Filter rows by a condition:

=QUERY(A1:D100, "select * where B > 500", 1)

select * returns every column.

Sort the results:

=QUERY(A1:D100, "select A, B order by B desc", 1)

Group and total:

=QUERY(A1:D100, "select A, sum(D) group by A", 1)

That’s a pivot table in one formula — one row per category, with a total.

Filter and sort together:

=QUERY(A1:D100, "select A, B where B > 100 order by B desc limit 10", 1)

The ten highest rows above 100.

The clauses you can use

QUERY supports a readable subset of SQL:

ClauseDoes
selectWhich columns to return
whereFilter rows
group byCombine rows by a column
order bySort (asc or desc)
limitReturn only the first N rows
offsetSkip the first N rows
labelRename an output column header

A full statement reads in that order: select ... where ... group by ... order by ... limit ....

Filtering with conditions

The where clause takes the usual operators, plus a few extras:

To filterWrite
Equals textwhere A = 'North'
Not equalwhere A <> 'North'
Greater thanwhere B > 500
Containswhere A contains 'north'
Starts withwhere A starts with 'Nor'
Either/orwhere A = 'North' or A = 'South'
Bothwhere A = 'North' and B > 500

Text needs single quotes inside the double quotes. The whole statement is wrapped in ", so strings within it use '. =QUERY(A1:C100, "select A where B = 'North'", 1).

Dates need a different approach — use date literals:

=QUERY(A1:C100, "select A where B > date '2026-01-01'", 1)

Renaming output columns

The label clause tidies up the output:

=QUERY(A1:C100, "select A, sum(C) group by A label sum(C) 'Total'", 1)

Without it, aggregated columns are headed “sum Sales” or similar. With it, you control the wording.

Where QUERY beats a filter

TaskFilter viewQUERY
Hides rows in place✅❌ (outputs elsewhere)
Result you can reference❌✅
Group and total❌✅
Updates automatically✅✅
Works inside other formulas❌✅

Use a filter when you just want to look at a subset. Use QUERY when you need a result — something another formula or chart can read.

Common problems

ProblemCause
#VALUE!Usually column names — use Col1 not A when the data came from IMPORTRANGE
#ERROR! with “Unable to parse query string”A syntax slip: missing quotes, or a clause out of order
Only the first row appearsThe headers argument is missing — add , 1
Text conditions return nothingThey need single quotes: 'North', not "North"
Numbers treated as textThe source cells are text. Use VALUE() or fix the column format
Dates don’t filterUse date 'YYYY-MM-DD' syntax, not a plain string

QUERY pairs naturally with the rest of the lookup family — how to use VLOOKUP in Google Sheets for finding single values, and how to remove duplicates in Google Sheets when you need a clean list before querying it.

FAQ

What does QUERY do in Google Sheets?

It runs SQL-like statements against a range — selecting columns, filtering rows, grouping, sorting, and limiting results, all in one formula.

What’s the QUERY syntax in Google Sheets?

=QUERY(data, "select ... where ... group by ... order by ...", headers). The statement goes in double quotes, with text conditions in single quotes inside it.

Why does QUERY return #VALUE!?

Most often because the column references are wrong. Ranges in the same sheet use letters (A, B); data from IMPORTRANGE must use Col1, Col2.

Can QUERY replace a pivot table?

For many summaries, yes — =QUERY(data, "select A, sum(B) group by A", 1) produces a grouped total without building a pivot table. A pivot table is still better when you want to click and drag the layout.

How do I filter by date in QUERY?

Use a date literal: where B > date '2026-01-01'. A plain string like '2026-01-01' often won’t compare correctly.