What QUERY does
QUERY runs a SQL-like query on a range of data and returns the rows and columns the query asks for. The syntax is:
=QUERY(data, query, [headers])
The query is text in double quotes. Text values inside the query use single quotes, because the whole query already sits inside double quotes.
A basic example
Suppose the data is in A1:D100, and row 1 holds headers. Column A is the region, B is the product, C is the status, and D is the amount. To list the Paid rows with an amount above 500:
=QUERY(A1:D100, “select A, B, D where C = ‘Paid’ and D > 500”, 1)
The select part chooses columns A, B and D. The where part keeps only the rows where the status is Paid and the amount is above 500. The 1 at the end tells QUERY that the first row is a header row.
The headers setting
The third argument controls how QUERY treats the first row:
- 1 means the first row is a header row.
- 0 means there’s no header row, so every row is data.
- -1, or leaving it out, lets Sheets guess. A guess can be wrong, so enter 1 or 0 when you know the answer.
Column letters
In a query, columns are named by letter: A, B, C and so on. The letters refer to the columns of the range you pass to QUERY, not to the columns of the sheet. If the range starts at column B, then B is the first column of that range. Check the letters against the range whenever you change the range.
Totals with group by
To total the amounts by product for the Paid rows:
=QUERY(A1:D100, “select B, sum(D) where C = ‘Paid’ group by B label sum(D) ‘Total amount’”, 1)
The group by part makes one row for each product, and sum(D) adds up the amounts in each group. The label part gives the total column a clearer heading.
Sorting and limiting
To list the five largest amounts:
=QUERY(A1:D100, “select A, B, D order by D desc limit 5”, 1)
The order by part sorts the results, desc puts the largest first, and limit keeps only the first rows after sorting.
Text and dates
To find rows where the product name contains a word, use contains with the word in single quotes:
=QUERY(A1:D100, “select A, B, D where B contains ‘Pro’”, 1)
Dates in a query use the date keyword, followed by a date in yyyy-mm-dd form in single quotes. If column A holds dates, the condition where A > date ‘2027-01-01’ keeps the rows dated after January 1, 2027.
Pivot
The pivot part turns the values in one column into new columns. For example, pivot C would make one column for each status. It’s useful for a grid of totals, but check the result, because the headings depend on the data.
Common errors
- Unable to parse query string appears when the query has a typo, a missing quote, keywords in the wrong order, or a column letter outside the range you passed in.
- Double quotes inside the query end the query text early. Use single quotes for text values, as shown above.
- A header name in place of a letter usually causes the same parse error. Use A, B and so on.
QUERY or FILTER
FILTER is simpler when you need one condition and no totals. QUERY is worth the extra syntax when you need grouping, sorting, limits or totals in one formula.
Imported data
QUERY works on imported data too. Readers who need to pull rows from another file first often look up importrange google sheets how to use, and the same QUERY formula then works on the imported block.