The Google Sheets QUERY function runs a Google Visualization API Query Language query across your data, letting you filter, sort, group, and pivot with a single formula. Its syntax is QUERY(data, query, [headers]), where data is the range, query is the instruction in quotes, and headers is an optional number of header rows. For example, =QUERY(A1:C20, "select A, sum(C) group by A", 1) sums column C for each unique value in column A.
What the QUERY Function Does
QUERY is essentially a mini database query tool inside a spreadsheet. It uses the Google Visualization API Query Language, which resembles SQL but is simplified for spreadsheet use. You can select columns, filter rows with where, sort with order by, aggregate with sum, avg, count, max, min, group results, pivot data, and even label or format output columns. It returns a new table that updates automatically when the source data changes.
Unlike functions like FILTER or SORT, QUERY can combine multiple operations in one formula. For instance, you can filter, sort, and group in a single call, which would otherwise require nested functions or helper columns.
QUERY Syntax and Arguments
The full syntax is QUERY(data, query, [headers]).
- data – the range of cells to query, such as
A1:D100or a named range. - query – a string in double quotes containing the query language commands. For example,
"select A, B where C > 10". - headers – optional; the number of header rows at the top of the data. If omitted, Google Sheets guesses. If set to -1, it treats the entire range as data with no headers.
When referencing cell values inside the query string, you concatenate with &. For example, to filter by a date in cell F1: =QUERY(A1:D100, "select A, B where C = date '"&TEXT(F1,"yyyy-mm-dd")&"'", 1).
How to Write a Basic QUERY
Let’s walk through a common example. Suppose you have sales data in A1:D20, with headers: Product (A), Region (B), Sales (C), Date (D).
- Click an empty cell, for example F1.
- Type
=QUERY(A1:D20, "select A, B, C where C > 1000", 1). - Press Enter. The result shows rows where Sales > 1000, with columns A, B, and C.
To sort by Sales descending, change the query to "select A, B, C where C > 1000 order by C desc". To aggregate, use "select A, sum(C) group by A" to get total sales per product.
Common QUERY Clauses and Operators
Here are the most used clauses:
- select – choose columns. Use
*for all columns. - where – filter rows. Operators:
=,<>,>,<,>=,<=,like,contains,starts with,ends with,in,is null,is not null. - order by – sort by column (ascending or descending).
- group by – aggregate rows with identical values in the selected columns.
- pivot – turn unique values in a column into new columns (like a pivot table).
- limit – restrict the number of rows returned.
- offset – skip a number of rows.
- label – rename output column headers.
- format – apply number/date formats to output.
Logical operators and and or combine conditions in where. For example: "select A, B where C > 100 and D = 'East'".
Handling Dates, Text, and Special Characters
Dates in QUERY must be in the format date 'YYYY-MM-DD'. Text values must be enclosed in single quotes. If your text contains a single quote, double it (e.g., 'O''Brien'). For numbers, no quotes are needed.
When comparing to a cell, use & to insert the value. For text: "where B = '"&E1&"'". For numbers: "where C > "&E2. For dates, wrap with TEXT as shown earlier.
If your data contains a column with a space in the header, reference it with backticks, like select `Product Name`. This is a common gotcha.
Common Errors and How to Fix Them
QUERY errors are often cryptic. Here are the most frequent:
- #VALUE! – Often means a syntax error in the query string. Check for missing quotes, commas, or misspelled clause names.
- #REF! – The query references a column that doesn’t exist. For example,
select Zwhen your data only has 10 columns. - #DIV/0! – Division by zero inside an aggregate like
avgwhen there are no rows. - “Unable to parse query string” – Usually a missing or extra comma, or a reserved word used incorrectly.
- “No data” – The query is valid but returns zero rows. Check your
wherecondition.
A good debugging step is to test parts of the query separately. Start with select *, then add where, then order by, etc.
QUERY vs. FILTER and Other Functions
FILTER is simpler and more readable for basic row filtering. For example, =FILTER(A1:C20, B1:B20="East") is easier than a QUERY with where. However, QUERY wins when you need aggregation, grouping, or pivoting. You can also use QUERY to combine data from multiple sheets using {} arrays, though that requires consistent column counts.
For simple sorting, SORT function is fine. But QUERY can sort and filter in one step. For complex transformations, QUERY is often the only non-script option.
Performance and Best Practices
QUERY recalculates whenever the data changes, which can slow large sheets. To improve performance:
- Limit the data range to the actual used rows, not entire columns.
- Avoid using QUERY on very large datasets (hundreds of thousands of rows) unless necessary.
- Use
limitto reduce output size. - Consider using Pivot Tables for heavy aggregation, as they are often faster.
Also note that QUERY is case-insensitive for text comparisons by default. If you need case-sensitive, use EXACT or other functions.
Frequently Asked Questions
Can I use QUERY with data from another sheet?
Yes, reference the sheet with the range, like QUERY(Sheet2!A1:B10, "select *"). If the sheet name has spaces, wrap it in single quotes: QUERY('My Sheet'!A1:B10, "select *").
Why does my QUERY return a header row even when I don’t want one?
By default, QUERY assumes the first row is a header. If your data has no headers, set the third argument to -1: QUERY(A1:B10, "select *", -1). If you want to remove the header from the output, use label to rename, or use offset 1.
How do I use QUERY to count unique values?
Use count with group by. For example, =QUERY(A1:A20, "select A, count(A) group by A", 1) gives each unique value in A and its count. To count distinct across multiple columns, you may need count(distinct A).
Can I use QUERY with a dropdown or cell reference?
Yes, concatenate the cell value into the query string. For a dropdown in E1, use =QUERY(A1:C20, "select A, B where C = '"&E1&"'", 1). For numbers, omit the quotes.
Does QUERY work in Excel?
No. QUERY is a Google Sheets function and does not exist in Microsoft Excel. Excel has similar capabilities with Power Query, but the formula syntax is entirely different.
Why does my QUERY return #N/A when there are no matches?
That’s not typical; QUERY usually returns an empty table. If you see #N/A, check if you’re using a function like VLOOKUP inside the query, or if the query references a cell that contains an error. Ensure your data range is correct.
Related
- Best Time to Hire Data Entry Operators
- Java Random Number in Interval: How-To
- Google Sheets Query Select Multiple Columns
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel Keyboard Locked? How to Unlock ItIf your keyboard seems locked in Excel, first press Esc, check whether Scroll Lock is on, and see…
- What Number Column Is U in Excel?Column U is column number 21 in Excel. The count starts with A as 1, so U is…
- How to Delete Empty Rows in Excel Without Losing DataTo delete completely empty rows in Excel safely, add a helper column with =COUNTA(A2:Z2)=0, fill it down, filter…
- Balance of Payments Mind Map: Accounts and FlowsA balance of payments mind map organizes a country’s economic transactions with the rest of the world into…
- How to Create a Design Template in PowerPointTo create a reusable design template in PowerPoint, build the look and standard layouts in Slide Master view,…
- Shortcut Keys for Typing: The Essential ListThe most useful shortcut keys for typing are Ctrl+C (copy), Ctrl+X (cut), Ctrl+V (paste), Ctrl+Z (undo), and Ctrl+A…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.