The Google Sheets QUERY function syntax is =QUERY(data, query, [headers]), where data is the range to search, query is a string written in the Google Visualization API Query Language, and the optional [headers] tells Sheets how many header rows your data has. In plain terms, you write the data range, then a comma, then a text string that starts with a SQL-like command such as SELECT, WHERE, or ORDER BY — all inside double quotes.
What the QUERY Function Does
QUERY is one of the most powerful functions in Google Sheets because it lets you pull, filter, sort, group, and summarize data with a single formula. Instead of combining multiple functions like FILTER, SORT, and SUMIF, you write one compact query string. For example, if you have sales data with columns A (region), B (product), and C (amount), you can get total sales per region with:
=QUERY(A1:C100, "SELECT A, SUM(C) GROUP BY A LABEL SUM(C) 'Total'", 1)
That single formula replaces a pivot table or a handful of helper columns. The function returns a new table that updates automatically when your source data changes.
Breaking Down the Syntax
Every QUERY formula has three parts, and the third is optional.
- data — the range you are searching, like
A1:C100. It can also be a named range or another QUERY result. - query — a text string in double quotes that starts with a clause like
SELECT,WHERE,ORDER BY,GROUP BY,PIVOT, orLIMIT. You can combine clauses in one string, e.g.,"SELECT A, B WHERE C > 100 ORDER BY A". - [headers] — a number that tells Sheets how many top rows are headers. Use
1if the first row contains column labels,0if there are no headers, or-1if you want Sheets to guess. If you omit it, Sheets guesses from the data.
You can also reference a cell that contains the query string. For instance, if cell E1 holds "SELECT A, B", then =QUERY(A1:C100, E1) works the same way. This is handy when you want to change the query without editing the formula.
Writing the Query String: Clauses You Can Use
The query string uses the Google Visualization API Query Language, which is similar to SQL but with some differences. Here are the main clauses and how they work in practice.
- SELECT — chooses which columns to return. Use letters (
A,B) or*for all columns. You can also use aggregate functions likeSUM,COUNT,AVG,MIN,MAX. - WHERE — filters rows based on conditions. For example,
WHERE C > 100orWHERE B = 'Widget'. Text values must be in single quotes inside the query string. - ORDER BY — sorts the result. Use
ASC(default) orDESC. Example:ORDER BY C DESC. - GROUP BY — groups rows that have the same value in a column, usually with an aggregate. Example:
GROUP BY A. - PIVOT — turns unique values in one column into new columns, similar to a pivot table. Example:
PIVOT B. - LIMIT — returns only the first N rows. Example:
LIMIT 10. - OFFSET — skips the first N rows. Example:
OFFSET 5. - LABEL — renames column headers in the output. Example:
LABEL SUM(C) 'Total Sales'. - FORMAT — changes the display format of numbers and dates. Example:
FORMAT C '$#,##0.00'.
You can chain clauses in a specific order: SELECT, WHERE, GROUP BY, PIVOT, ORDER BY, LIMIT, OFFSET, LABEL, FORMAT. If you put them in the wrong order, the formula returns an error.
Concrete Example: Sales Data
Suppose you have this data in A1:C6:
Region Product Amount
North Widget 100
South Widget 150
North Gadget 200
South Gadget 120
North Widget 80
To get total sales per region, sorted descending, use:
=QUERY(A1:C6, "SELECT A, SUM(C) GROUP BY A ORDER BY SUM(C) DESC LABEL SUM(C) 'Total'", 1)
This returns:
North 380
South 270
To filter for only Widget sales above 100, use:
=QUERY(A1:C6, "SELECT A, C WHERE B = 'Widget' AND C > 100", 1)
That returns South 150 because North’s Widget sales are 100 and 80, neither of which is above 100.
Common Mistakes and How to Fix Them
QUERY errors are usually easy to diagnose if you know what to look for.
- #VALUE! error — often means your query string has a syntax error, like a missing comma or an unclosed quote. Check that every clause is in the correct order.
- #REF! error — the data range is invalid, perhaps because you deleted a column or the range is outside the sheet.
- #DIV/0! error — you are dividing by zero inside an aggregate, like
AVGover an empty set. - Mixed data types in a column — QUERY assumes a column has one type. If you have numbers and text in the same column, it may treat numbers as text and sort incorrectly. Keep data types consistent.
- Single vs. double quotes — in the query string, text values must be in single quotes (
'Widget'). Double quotes are used to enclose the entire query string. If you need a literal single quote in your data, double it ('') inside the string.
Another common issue: using WHERE with a column that has a header. If your data has headers and you use WHERE A = 'North', it works. But if you use SELECT * and then try to reference the header name in WHERE, you must use the column letter, not the header text. QUERY does not recognize header names in conditions unless you use LABEL or FORMAT.
QUERY vs. FILTER and Other Functions
QUERY is not always the best tool. For simple filtering, FILTER is often more readable and faster. For example, =FILTER(A1:C6, B1:B6="Widget") returns all rows where column B equals Widget. QUERY would be overkill for that. However, QUERY shines when you need aggregation (SUM, COUNT, AVG) or grouping, because FILTER cannot do that without additional functions like SUMIF or ARRAYFORMULA.
Another comparison: UNIQUE returns distinct values, but QUERY with SELECT DISTINCT A does the same thing. For large datasets, QUERY can be slower because it processes the entire range, but it is still efficient for typical spreadsheet sizes. If you need to combine data from multiple sheets, you might use {} arrays or VSTACK (in newer versions) instead of QUERY, because QUERY does not directly accept multiple ranges unless you combine them first.
Frequently Asked Questions
Why does my QUERY formula return a #VALUE! error?
This usually means the query string is malformed. Common causes: missing a comma between clauses, using double quotes inside the string instead of single quotes for text values, or putting clauses in the wrong order. Double-check your syntax and ensure the string starts with a valid clause like SELECT or WHERE.
Can I use a cell reference in the query string?
Yes, but you need to concatenate it with &. For example, if cell D1 contains the text Widget, you can write =QUERY(A1:C6, "SELECT A, C WHERE B = '"&D1&"'", 1). The ampersand joins the parts of the string, and you must include the single quotes around the value.
How do I use QUERY with dates?
Dates in QUERY need special formatting. Instead of using a cell reference with a date, you can use the DATE function inside the query string. For example, WHERE A > DATE '2024-01-01' works. If you want to reference a cell, you must convert it to text in the right format, like WHERE A > DATE '
Related
- Cool Shortcut Keys: 30+ Time-Savers for Windows
- Data Entry Job Information: Work, Pay & Safety
- Google Sheets Rank If: How to Rank with Conditions
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- SOLIDWORKS Split Part: Create Two Separate Bodies GuideYes. In SOLIDWORKS, use Insert > Features > Split with a plane, face, surface, or suitable sketch as…
- Can You Split Just One Cell in Excel? What Actually WorksNo: Excel cannot divide one worksheet cell into two smaller, independent cells while leaving the surrounding grid unchanged.…
- Remove Duplicates in Google Sheets: Steps and OptionsTo remove duplicates in Google Sheets, select the data range, choose Data > Data cleanup > Remove duplicates,…
- How to Add a Checkbox in Google Sheets on Android or iPhoneTo add checkboxes in the Google Sheets mobile app, select the cell or range, tap the three-dot More…
- Creating a Flowchart in Word: Steps and Practical TipsTo create a flowchart in Microsoft Word for Microsoft 365 on Windows, choose Insert > Shapes, add shapes…
- Excel Budget Audit Template: How to Build OneThe fastest way to audit a budget in Excel is to build a template that compares your planned…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.