
Filters in Google Sheets are fine until you want the same view in a second place, or a summary that updates itself. That is when the QUERY function earns its place. It reads your table with a short SQL-like sentence and returns only the rows and columns you asked for.
The syntax
QUERY(data, query, [headers])
- data is the range to read, such as A1:E200.
- query is the sentence, in quotation marks or pointing at a cell that holds it.
- headers is how many header rows sit at the top of the range. Leave it out or set -1 and Google guesses.
That definition comes from Google's page QUERY function.
Start with a filter
Say you have an order table: A is Order, B is Customer, C is Amount, D is Status, E is Date, with a header row. This returns only the paid orders over 500:
=QUERY(A1:E200,"select A, B, C where D = 'Paid' and C > 500",1)
Text values go in single quotes inside the double quotes. Numbers do not.
Sort and cut the list
Add order by and limit to get a ranked shortlist:
=QUERY(A1:E200,"select B, C where D = 'Paid' order by C desc limit 10",1)
This gives the ten biggest paid orders, largest first. Swap desc for asc for the other direction.
Summarise with group by
To total each customer, select the customer column and wrap the amount in an aggregate:
=QUERY(A1:E200,"select B, sum(C) where D = 'Paid' group by B order by sum(C) desc",1)
Google's query language supports the aggregates avg, count, max, min and sum, plus pivot when you want distinct values turned into columns.
Rename the output columns
Headers from sum(C) look ugly. Use label to fix them:
=QUERY(A1:E200,"select B, sum(C) group by B label sum(C) 'Total'",1)
Four things that break it
- Mixed types in a column. Google says each column can hold only boolean, numeric or text values. When a column is mixed, the majority type wins and the odd cells are treated as empty. That silently loses rows, so keep a column to one kind of value.
- Referring to a column by its heading. In a spreadsheet the query uses the column letter, not the label text.
- Forgetting the header count. If row 1 is a header, pass 1 or the header gets sorted into your data.
- Broken quotes. A missing single quote inside the double quotes returns an error rather than a wrong result, which at least tells you where to look.
When to use it
QUERY is worth the learning time when the same filtered view is needed on a dashboard tab, when the source data grows every week, or when you would otherwise rebuild a pivot table. For a one-off look at a small sheet, a filter is faster.
Related: How to Highlight Cells Automatically covers the colour rules that pair well with a query view.