How to Use the QUERY Function in Google Sheets

By Gerard Fernandez · Updated · 2 min read

QUERY is the most powerful function in Google Sheets. With one formula you can pick columns, keep only matching rows, sort them and even total them by group. It uses a simple language that reads almost like English.

QUERY syntax

=QUERY(data, query, [headers])
  • data – the range to work on, for example A1:C7.
  • query – the instructions, in double quotes. Columns are referred to by their letter: A, B, C…
  • headers – optional. How many header rows the data has, usually 1.

Example: only the rows you want

An expense list has Category in column A, Item in B and Amount in C. To show only the Food purchases:

=QUERY(A1:C7, "SELECT A, B, C WHERE A = 'Food'", 1)
QUERY formula in Google Sheets returning only the Food rows from an expense list
QUERY copies the header and every row where Category is Food.

Quotes matter: the whole query goes in double quotes, and text values inside it go in single quotes: 'Food'.

The main clauses

ClauseWhat it doesExample
SELECTWhich columns to showSELECT A, C or SELECT *
WHEREKeep only matching rowsWHERE C > 20
ORDER BYSort the resultORDER BY C DESC
GROUP BYCombine rows into groupsGROUP BY A
LIMITShow only the first N rowsLIMIT 3

Clauses must appear in this order: SELECT, WHERE, GROUP BY, ORDER BY, LIMIT.

Useful examples

You want…Query
The 3 biggest expenses"SELECT B, C ORDER BY C DESC LIMIT 3"
Expenses over 20"SELECT * WHERE C > 20"
Total per category"SELECT A, SUM(C) GROUP BY A"
Items containing "pass""SELECT * WHERE B CONTAINS 'pass'"
Everything except Food"SELECT * WHERE A <> 'Food'"

Use a cell value in the query

To filter by whatever category is typed in F1, join it into the query text:

=QUERY(A1:C7, "SELECT * WHERE A = '"&F1&"'", 1)

Change F1 and the result updates.

Frequently asked questions

Why do I get "Unable to parse query string"?

Usually a quoting or order problem. Check that text values use single quotes, column letters are uppercase, and the clauses follow the order SELECT → WHERE → GROUP BY → ORDER BY → LIMIT.

Why are some values missing from the result?

QUERY expects one data type per column. If a column mixes numbers and text, the minority type is treated as empty. Keep each column consistent.

QUERY or FILTER?

For a simple "show rows where…", FILTER is shorter. Use QUERY when you also need to sort, group or total in the same step.