How to Write a Google Sheets QUERY Formula Without Syntax Errors
A practical guide to the Google Sheets QUERY function: select, where, group by, pivot and order by, quoting rules for text and dates, and common errors.

How to Write a Google Sheets QUERY Formula Without Syntax Errors
QUERY is the formula that turns Google Sheets into something close to a database. One cell can filter thousands of rows, group them, total them, pivot them and sort the result, and it updates whenever the source changes. The catch is that the second argument is a string written in a separate query language, and small slips in that string produce errors that say very little. This guide explains the parts of a query, the quoting rules that cause most failures, and a way to build and test formulas before they reach your sheet.
The shape of the formula
A QUERY formula has three arguments: the range to read, the query text in double quotes, and the number of header rows. A typical example reads columns A to F of a Sales tab, returns region and total revenue for rows from February onwards, grouped by region and sorted by revenue. Inside the quotes the clauses must appear in a fixed order: select, where, group by, pivot, order by, limit, offset, label, format. Writing them in another order is the first common cause of a parse error.
Columns are letters, not headers
The query refers to columns by their sheet letter. If your range starts at column C, then the first column of the range is still called C, not A. When the data comes from an array, from IMPORTRANGE or from another function rather than a plain range, letters are not allowed at all and you must write Col1, Col2 and so on. The Google Sheets QUERY Builder shows each column's letter next to its header and switches between the two styles with one menu, which removes a whole class of mistakes.
Quoting text, numbers and dates
Inside the query, text values go in single quotes: where B = 'North'. Numbers have no quotes: where F > 500. Dates need the date keyword and ISO format: where A >= date '2026-02-01'. Typing a date as 01/02/2026 or comparing it with plain quotes silently returns nothing, which is worse than an error.
Text containing an apostrophe, such as O'Neil, cannot sit in single quotes. Use double quotes inside the query, and because the whole query is already inside double quotes in the formula, those inner quotes must be doubled. The builder writes all of this for you and shows the query text separately from the formula so you can see both layers.
Aggregates and group by
As soon as one selected column is aggregated with sum, avg, count, min or max, every other selected column must appear in group by. This rule trips up almost everyone at first. If you select region, rep and sum of revenue but only group by region, Sheets cannot decide which rep to show and returns an error. The builder's preview enforces the same rule and tells you which column is missing.
Sorting by an aggregate is allowed and useful: order by sum(F) desc lists the best regions first. Labels rename the result headers, so sum Revenue can become Total revenue, and format applies number patterns such as #,##0 to the result.
Pivot for cross tables
Adding pivot with a column turns each distinct value of that column into its own result column. Group by rep and pivot by region, summing revenue, and you get a rep-by-region table that updates itself. Remember that the pivot column must not also be selected on its own.
Text matching is case sensitive
Operators such as contains, starts with and ends with match case exactly, so contains 'desk' does not find Desk. Use like with % as a wildcard for any run of characters, or matches with a regular expression for more flexible patterns. If you need case-insensitive matching, the usual trick is to wrap the column in lower() inside the query after copying the formula.
Mixed column types
QUERY gives each column a single type based on the majority of its values. In a column of numbers with a few text notes, the text cells become empty in the result. If values go missing unexpectedly, check for numbers stored as text or stray words in a numeric column. The builder shows the type it detected for each column of your sample.
A workflow that avoids surprises
Paste a few representative rows into the builder, assemble the clauses from the menus, and watch the preview and the plain-English explanation as you go. When the result looks right, copy the formula into an empty cell beside your data. Test it on the full sheet once, because real data often contains the odd value a sample does not. Keep the explanation text in a note next to the formula, and the next person to open the sheet will understand it too.



