Section: Excel & Google Sheets

QUERY in Google Sheets runs a query language over a range and returns the answer as a table. =QUERY(A2:E6,"select avg(A) pivot B") is one of Google's own samples: it averages one column and pivots it by another, with no pivot table and no helper columns. If you know a little SQL, QUERY is the closest thing Sheets has to it. This guide covers the syntax, the column names, the clauses that do the real work, the header argument, mixed data types and the mistakes that produce an error instead of a table.
What QUERY does
Google describes QUERY as running a Google Visualization API Query Language query across data. In plain terms: you point it at a range, write what you want in a string, and it builds the result as a live array that updates when the data changes.
That last part is the reason to use it. A filter hides rows, a pivot table needs refreshing by hand, and QUERY simply recalculates. A dashboard built on QUERY stays current as long as the source range covers the data.
The syntax
=QUERY(data, query, [headers])
- data is the range to query, such as A1:E500.
- query is the query text, either in quotation marks or a reference to a cell that holds it.
- headers is optional and is the number of header rows at the top of the data. If you omit it or pass -1, Google guesses from the content.
Putting the query text in a cell rather than inside the formula is the single best habit here. The formula stays short, and the query becomes something you can edit and test without opening the formula bar.

Naming the columns
The query language refers to columns by letter, as in select A, B, or by the Col form, as in select Col1, Col2. Google's page notes that QUERY accepts either notation.
The letter form is shorter and matches the sheet. The Col form is safer when the data range starts partway across the sheet, because Col1 always means the first column of the range you passed, not column A of the sheet.
Select and where
select chooses the columns and where filters the rows. A query that returns three columns for rows above a threshold looks like "select A, B, D where D > 100", and one that returns everything is "select *".
The comparison operators are the ones you expect, and text values need single quotes inside the double-quoted query, as in "select A where B = 'North'". That nesting of quotes is the first thing that trips people up: the outer quotes belong to the formula, the inner ones to the query.

Group by
group by collapses rows that share a value and lets you aggregate the rest. Google's own example aggregates salary values across rows using select and group by, which is a total per department in one line: "select B, sum(C) group by B".
The aggregate functions available are the usual ones: sum, count, avg, min and max. You can also label the output columns with a label clause, so the header reads Total rather than sum.
Order by and limit
order by sorts the result and takes desc for a reverse sort. Google's example aggregates departments and sorts by the maximum salary. A top-ten list is then "select B, sum(C) group by B order by sum(C) desc limit 10".
Sorting happens after grouping, so order by refers to the aggregated value, not to the raw column. Getting that order wrong produces an error rather than a wrong table, which is at least honest.
Pivot
pivot turns the distinct values of a column into new columns, which is how QUERY produces a cross-tab without a pivot table. Google's sample "select avg(A) pivot B" is the whole idea in five words: one aggregate, one column spread across the top.
The result spills to the right, so leave empty space beside the formula. A pivot that grows a new column when a new category appears will overwrite anything you placed there.
Headers and mixed data types
The headers argument matters when your data has more than one header row, or none. Passing 0 tells QUERY the range has no header and it will name the columns itself; passing 1 with a two-row header flattens the first row into names.
Google's page also states a rule worth knowing before you debug a strange result: each column can hold only boolean, numeric or string values, and when a column is mixed the majority type decides the column type for the query, with the minority values treated as null. A column of numbers with a few text entries therefore behaves as numeric, and those text entries disappear from sums without an error.

QUERY compared with FILTER
FILTER returns the rows that match a condition and keeps the original column order. QUERY can do that too, but its real advantage is everything FILTER cannot do: aggregate, group, sort by a total, limit the number of rows and pivot.
Use FILTER for a simple subset that other formulas read, and QUERY when the output is a report. For sorting alone, SORT is lighter. And if the point is a view rather than a formula, filter views let each person look at the sheet their own way.

Common problems
- Unable to parse query string. A missing quote, a misspelled clause, or an operator used with the wrong type. The error names the position, which is usually enough to find it.
- #VALUE! on a valid-looking query. The data range includes a column of mixed types, or a header row is being read as data. Set the headers argument.
- A number that should be a total but is blank. The column is text. Check the alignment in the source range.
- Columns in the wrong order. The select clause lists them in the order you wrote, not the sheet order.
- #REF! after adding data. The result spills into cells that already have content. Clear the space to the right and below.
- A query that is slow. Whole-column ranges on a large sheet. Limit the range to the data you have.
Questions and answers
Is QUERY available in Excel?
No. QUERY is a Google Sheets function, and Excel has no equivalent single formula. The nearest tools there are FILTER, SUMIFS and pivot tables.
Can I use a cell for the query text?
Yes. Point the second argument at the cell, and build the text with concatenation when a date or a name has to change.
Does QUERY work on another sheet?
Yes. Pass the range with the sheet name, such as Data!A1:E500, as the first argument.
Why does my pivot lose a column?
The result spills right and something is in the way. Leave the space empty.
Can QUERY write data back?
No. It reads a range and returns values. Editing has to happen in the source.
