The complete QUERY function guide for Google Sheets (2026)
QUERY is Google Sheets' most powerful function — a single formula that uses SQL-inspired syntax to filter, sort, aggregate, and pivot your data. One QUERY often replaces 10+ Excel formulas. This is the complete guide to mastering it: every clause (SELECT, WHERE, ORDER BY, GROUP BY, PIVOT, LABEL, FORMAT), how to combine them, common errors, and real-world patterns you'll use daily. Free companion workbook included.
📊 Free companion workbook
All examples in this guide use the same sample datasets. Open the workbook in Google Sheets and follow along.
📚 Table of contents
- What is QUERY?
- Anatomy of a QUERY formula
- Our sample dataset
- SELECT — choosing columns
- WHERE — filtering rows
- ORDER BY — sorting results
- LIMIT — top N results
- GROUP BY — aggregation
- PIVOT — cross-tabulation
- LABEL & FORMAT — polish output
- Combining clauses in real queries
- Combining QUERY with IMPORTRANGE
- Dynamic filters — cell references in queries
- Common QUERY errors
- When NOT to use QUERY
- QUERY vs Excel equivalents
- Frequently asked questions
What is QUERY?
QUERY is a Google Sheets function that runs an SQL-like statement against a range of data and returns the result. It's unique among spreadsheet functions because it uses its own mini-language — Google Visualization API Query Language — inspired by SQL but tailored for spreadsheet ranges.
What makes QUERY revolutionary is the sheer number of operations you can express in one formula. A typical business question — "show me the top 10 sales reps in the North region for products over $500, grouped by product category" — takes 5+ formulas in Excel. In Google Sheets, it's a single QUERY.
QUERY doesn't exist in Microsoft Excel. Excel users approach the same problems by combining FILTER, SORT, SUMIFS, and PivotTables — powerful but fragmented across multiple formulas and features. For dual-platform teams, this is one of the few functions where Google Sheets clearly wins.
⚡ Why QUERY is a spreadsheet superpower
A single QUERY formula can filter, sort, group, aggregate, pivot, rename headers, and format output — all at once, in a single expression. What takes 10 formulas + a PivotTable in Excel takes one QUERY in Sheets. Once you internalize the syntax, you'll reach for QUERY constantly and wonder how you ever built dashboards without it.
Anatomy of a QUERY formula
Every QUERY formula has the same three-part structure:
Three arguments. That's the whole function. All the complexity lives inside the query string — the second argument — which is where you'll spend all your learning time.
The query string is composed of clauses, each performing one operation. Clauses combine in a strict order: SELECT → WHERE → GROUP BY → PIVOT → ORDER BY → LIMIT → OFFSET → LABEL → FORMAT. You don't need all clauses in every query — most queries use 2-4.
Our sample dataset
Every example in this guide uses the same sample data from the companion workbook. Here's what the primary dataset (Sales_Data) looks like:
| A: Date | B: Region | C: Rep | D: Product | E: Category | F: Qty | G: Price | H: Total | I: Customer |
|---|---|---|---|---|---|---|---|---|
| 2025-03-15 | North | Ali Khan | Analytics Pro | Software | 3 | $299 | $897 | Acme Corp |
| 2025-04-02 | South | Sara Ahmed | Consulting Package | Services | 1 | $2,500 | $2,500 | TechFlow Inc |
| 2025-05-18 | East | James Miller | Dashboard Suite | Software | 2 | $499 | $998 | DataWorks Ltd |
| 2025-06-30 | West | Lisa Wong | Training Session | Services | 4 | $750 | $3,000 | PixelSoft |
| 2025-07-12 | Central | David Chen | Excel Templates | Templates | 5 | $79 | $395 | GlobalTech |
Notice that we reference columns by their letters (A, B, C…) in QUERY formulas when the data comes from a sheet range. If we were pulling data from IMPORTRANGE or an array, we'd use Col1, Col2, Col3 instead. This is one of the most common QUERY confusions — remember: sheet ranges use letters, array data uses Col numbers.
Choosing columns
RequiredSELECT specifies which columns from your data appear in the result. Every QUERY starts with SELECT. Use * to return all columns, or list specific column letters to return only those:
| Region | Product | Total |
|---|---|---|
| North | Analytics Pro | $897 |
| South | Consulting Package | $2,500 |
| East | Dashboard Suite | $998 |
You can also reorder columns in the SELECT statement — SELECT H, B, D returns Total, Region, Product in that order, regardless of the source column order.
Filtering rows
OptionalWHERE filters rows based on conditions — the QUERY equivalent of Excel's FILTER function. Supports comparison operators (=, <, >, <=, >=, !=, <>), logical operators (AND, OR, NOT), and pattern matching (LIKE, CONTAINS, STARTS WITH, ENDS WITH):
Text values inside the query string go in single quotes. Numeric values need no quotes. Date values require the DATE keyword and ISO format:
Combining conditions with AND / OR
Combine multiple conditions using AND, OR, and parentheses for grouping:
Pattern matching with LIKE
LIKE lets you match text patterns using wildcards: % for any string of any length, _ for a single character:
Unlike some SQL dialects, QUERY's LIKE is case-sensitive. Use UPPER() or LOWER() inside WHERE for case-insensitive matching: WHERE UPPER(D) LIKE '%ANALYTICS%'. Alternatively, CONTAINS is case-sensitive too but sometimes reads more naturally.
Sorting results
OptionalORDER BY sorts the result by one or more columns. Use ASC for ascending (default) or DESC for descending. Multiple sort columns are separated by commas:
Top N results
OptionalLIMIT restricts the number of rows returned — perfect for "top 10" style queries. Almost always used with ORDER BY:
Pair LIMIT with OFFSET for pagination — LIMIT 10 OFFSET 20 returns rows 21-30. Useful for building "Load more" style dashboards.
Aggregation and summarization
OptionalGROUP BY is where QUERY becomes truly powerful. Combined with aggregation functions, it turns raw transaction data into summaries. The five aggregations are SUM(), AVG(), COUNT(), MIN(), and MAX():
| Region | sum Total |
|---|---|
| Central | $28,450 |
| East | $34,120 |
| North | $41,890 |
| South | $29,750 |
| West | $36,220 |
HAVING — filtering aggregated results
WHERE filters rows before aggregation. HAVING filters after aggregation — used when you want to filter based on the aggregate values themselves:
Cross-tabulation
OptionalPIVOT is unique to Google Sheets — it doesn't exist in standard SQL. It takes distinct values from a column and turns them into column headers in the output, creating a cross-tabulated table (like a PivotTable in Excel). One of QUERY's most useful features:
| Region | Services | Software | Templates |
|---|---|---|---|
| Central | $18,500 | $6,940 | $3,010 |
| East | $22,200 | $7,895 | $4,025 |
| North | $25,000 | $11,880 | $5,010 |
| South | $18,750 | $8,275 | $2,725 |
| West | $22,500 | $10,290 | $3,430 |
PIVOT works best with 3-8 distinct pivot values. With too many distinct values, the result becomes unreadably wide. If your pivot column has 50+ values, use GROUP BY without PIVOT instead.
PIVOT is always used with GROUP BY. The GROUP BY column becomes the row labels, the PIVOT column becomes the column headers, and the aggregate (SUM, COUNT, etc.) fills the intersection cells. This is exactly how PivotTables work — but delivered as a live formula that updates automatically.
Polish the output
OptionalBy default, aggregated columns get technical headers like "sum Total" or "count Date". LABEL renames them to something readable. FORMAT applies number formats to columns:
LABEL and FORMAT come at the end of the query string. You can chain both for polished output — this is how professional QUERY dashboards look clean.
Combining clauses in real queries
Real-world QUERY formulas combine multiple clauses. Here's how a business question translates to a full QUERY:
Question: "Show me total software revenue by rep in the North region, sorted by revenue, only reps with over $2,000 in sales, formatted as currency."
That single formula does the work of 6+ formulas in Excel: FILTER by region, FILTER by category, SUMIFS by rep, IF filter for threshold, SORT, and format cells. QUERY collapses it into one expression that recalculates automatically as source data changes.
QUERY clauses must appear in this exact order: SELECT → WHERE → GROUP BY → PIVOT → ORDER BY → LIMIT → OFFSET → LABEL → FORMAT. Reordering causes syntax errors. Not every clause is required, but any clause you include must be in this position relative to the others.
Combining QUERY with IMPORTRANGE
One of the most powerful patterns in Google Sheets is combining QUERY with IMPORTRANGE — this lets you query data across workbooks. Instead of copying data, you pull it live from another spreadsheet and filter/aggregate it in a single formula:
When wrapping IMPORTRANGE, column references change from letters (A, B, C) to Col numbers (Col1, Col2, Col3). This is because IMPORTRANGE returns an array without column letters — QUERY needs a way to reference columns positionally. The columns still map in order: Col1 is the first column, Col2 is the second, etc.
This pattern enables "single source of truth" data architectures in Google Sheets. Keep master data in one workbook, then use IMPORTRANGE + QUERY to pull filtered views into department-specific dashboards. When the master data updates, every downstream dashboard updates too.
The first time you use IMPORTRANGE against a source workbook, you'll see a #REF! error until you authorize the connection. Click the cell, click the "Allow access" button in the popup, and the data starts flowing.
Dynamic filters — cell references in queries
Hardcoded query strings work fine for static reports. For interactive dashboards, you want users to pick filter values from dropdowns and have the query update automatically. This is done by concatenating cell references into the query string:
Here E1 is a cell containing a region name (perhaps set with data validation as a dropdown). The user picks "North" from the dropdown, and the QUERY updates to show only Northern transactions. Change to "South", and the query recalculates instantly.
Notice the quote juggling: text values in queries need single quotes inside the query string, and the cell reference must be concatenated with &. The pattern is: "'"&CellRef&"'" — that's an open single quote, an ampersand, the cell reference, another ampersand, and a close single quote.
For numeric filters (no quotes needed), the pattern is simpler:
Combine dynamic filters with dropdowns (Data → Data Validation → List of items) to build fully interactive dashboards. Users pick criteria from dropdowns and the QUERY updates the entire report. Add multiple dropdowns for multi-criteria filtering. This is how professional Sheets dashboards achieve interactivity without scripting.
Common QUERY errors and how to fix them
QUERY errors are usually cryptic. Here's what the common ones actually mean:
| Error | What it means | Common causes |
|---|---|---|
#VALUE! |
Query string is invalid | Typo in keyword (SLECT vs SELECT), missing quotes, wrong clause order, unmatched parentheses |
#REF! |
Referenced column doesn't exist | Column letter beyond your data range (referencing K when data only goes to I), or QUERY output would overwrite existing data |
#N/A |
Query returned no rows | WHERE clause too restrictive, data doesn't contain matching rows, headers argument wrong |
#ERROR! |
Generic query error | Missing closing quote in query string, invalid date format (must be YYYY-MM-DD), reference to a nonexistent function |
| "Unable to parse" error box | QUERY couldn't parse the query string | Missing DATE keyword before date literal, wrong quote type (curly quotes from copy-paste), missing GROUP BY when using aggregates |
If you use an aggregate function (SUM, COUNT, etc.) alongside a non-aggregated column in SELECT, you must GROUP BY that non-aggregated column. SELECT B, SUM(H) without GROUP BY B throws an error. This is the single most common QUERY error for beginners.
Debugging strategy
When a QUERY errors, work backward:
1. Remove clauses one by one until the error disappears. Start with FORMAT, then LABEL, then ORDER BY, then GROUP BY, then WHERE. The clause that fixes the error is the broken one.
2. Check quote characters. Copy-pasting from documents sometimes converts straight quotes (' ") to curly quotes (' " " ") which QUERY doesn't recognize. Retype quotes directly in Sheets.
3. Verify column references. If your data is in A:I, you can't reference column J. If your data starts at column C, references still use A, B, C… — QUERY renumbers based on the range you passed.
4. Explicitly set the headers argument. QUERY's auto-detect for headers often misfires. Pass 1 as the third argument if your data has one header row. This alone fixes many mysterious errors.
When NOT to use QUERY
QUERY is powerful but not always the right tool. Use something else when:
Single-value lookups. For "find this value in that column and return the corresponding value", VLOOKUP, INDEX/MATCH, or XLOOKUP are simpler and faster. QUERY is overkill for single lookups.
Very large datasets (100,000+ rows). QUERY parses the query string on every recalculation, which slows down with huge datasets. For very large data, consider using BigQuery connectors or Sheets' data connectors instead.
Complex text transformations. QUERY has minimal string manipulation. If you need substring extraction, splitting, or complex text operations, use dedicated text functions (LEFT, MID, SPLIT, REGEXEXTRACT) either instead of or alongside QUERY.
Cross-sheet joins. QUERY can't natively JOIN like SQL. To combine data from two sheets by matching keys, use INDEX/MATCH, VLOOKUP, or XLOOKUP alongside QUERY. Or restructure the data with ARRAYFORMULA + VLOOKUP into a single table first, then QUERY that.
Real-time streaming data. QUERY doesn't handle streaming or event-based updates well. For live external data, use Sheets' native connectors or Apps Script.
QUERY vs Excel equivalents
For dual-platform teams, here's what QUERY operations translate to in Excel:
| QUERY operation | Excel equivalent | Simpler in? |
|---|---|---|
| SELECT columns | Copy specific columns to new range | Sheets (QUERY) |
| WHERE filtering | FILTER function | Similar |
| ORDER BY sorting | SORT function | Similar |
| GROUP BY + SUM aggregation | SUMIFS with UNIQUE for the grouping column | Sheets (QUERY) |
| PIVOT cross-tabulation | PivotTable (UI feature, not a formula) | Sheets (QUERY is a live formula) |
| LABEL rename headers | Manual cell edit | Sheets (QUERY) |
| FORMAT output | Format cells manually | Sheets (QUERY) |
| Combining 5+ operations | Multiple formulas + PivotTable | Sheets (QUERY, dramatically) |
| Cross-workbook query | Power Query (UI-based, not a formula) | Sheets (QUERY + IMPORTRANGE, live) |
QUERY wins decisively for complex, combined operations. Excel wins for very large datasets (via Power Query) and for scenarios where PivotTable's interactive UI is preferred over a formula-driven output.
Frequently asked questions
What does the QUERY function do in Google Sheets?
QUERY runs an SQL-like statement against a range in Google Sheets and returns the filtered, sorted, grouped, or pivoted result. A single QUERY formula can replace what would take 10+ formulas in Excel — combining FILTER, SORT, SUMIFS, and pivot-table functionality into one expression. Syntax: =QUERY(data, query_string, [headers]).
Does the QUERY function work in Microsoft Excel?
No. QUERY is native to Google Sheets and has no direct equivalent in Microsoft Excel. Excel users typically achieve similar results by combining FILTER, SORT, SUMIFS, and PivotTables — but no single Excel function matches QUERY's SQL-inspired expressiveness. Power Query in Excel is different (an ETL tool, not a formula) and requires the UI rather than a cell formula.
Do I need to know SQL to use QUERY in Google Sheets?
No, but familiarity helps. QUERY uses Google Visualization API Query Language which is inspired by SQL but simpler. The core keywords (SELECT, WHERE, ORDER BY, GROUP BY, LIMIT) work the same way as SQL. You can learn QUERY effectively in a few hours without any SQL background. This guide covers everything you need.
How do I reference columns in a QUERY formula?
Reference columns by their letter (A, B, C) when the data range starts from column A of the sheet, or by Col1, Col2, Col3 when the data comes from an array or IMPORTRANGE. For example, =QUERY(Sales!A:I, "SELECT B, SUM(H) GROUP BY B") uses letters because it references sheet columns directly. When wrapping with IMPORTRANGE, use Col1, Col2 syntax instead.
Can QUERY combine data from multiple sheets or workbooks?
Yes, in two ways. Use curly braces to stack ranges from multiple sheets: =QUERY({Sheet1!A:D; Sheet2!A:D}, "SELECT *"). Use IMPORTRANGE to pull from another workbook: =QUERY(IMPORTRANGE("sheet_url", "A:D"), "SELECT Col1, Col2 WHERE Col3 > 100"). The IMPORTRANGE combination is one of the most powerful patterns in Google Sheets — enabling live queries across workbooks.
Why does my QUERY return an error?
The most common QUERY errors are: #VALUE! meaning the query syntax is invalid (usually a typo in the query string), #REF! meaning a referenced column does not exist, and #N/A meaning the query executed but returned no rows. Check the query string for typos, ensure column references match your data range, and verify the data actually contains rows matching your filter.
Can I use cell references inside a QUERY string?
Yes, by concatenating with the ampersand operator. For example, =QUERY(A:D, "SELECT * WHERE B = '" & E1 & "'"). This lets you build dynamic filters where users select a value from a dropdown and the query updates. Note the single quotes must be included for text values. For numeric filters, no quotes are needed: "SELECT * WHERE C > " & E1.
What is the PIVOT clause in QUERY?
PIVOT is a QUERY clause unique to Google Sheets that creates a pivot-table-like output. It takes distinct values from a column and turns them into column headers in the result. For example, =QUERY(Sales!A:I, "SELECT B, SUM(H) GROUP BY B PIVOT E") creates a table with regions as rows and product categories as column headers with sum totals in each cell. It combines GROUP BY and cross-tabulation in one clause.
Is QUERY slower than regular formulas?
QUERY can be slower than simple formulas on large datasets (10,000+ rows) because it must parse and execute the query string on every recalculation. For simple single-value lookups, use INDEX/MATCH or VLOOKUP instead. QUERY shines when you need multiple operations at once (filter + sort + aggregate) — one QUERY often outperforms 5 separate formulas because it processes data in a single pass.
Can QUERY output be used in other formulas?
Yes. QUERY returns an array of values that spills into adjacent cells. Other formulas can reference the spilled range using the # hash notation (Excel 365) or by referencing specific output cells. You can also nest QUERY inside functions like COUNTA, SUM, or wrap QUERY output as INDEX arguments. For chained transformations, some users nest one QUERY inside another for multi-step processing.
Related resources
Master Google Sheets — the complete library
QUERY is one of many Sheets-only superpowers. Explore other essential Google Sheets functions with dedicated guides.
All Sheets Guides → 12 Sheets-Only Functions → Excel vs Sheets Comparison →Bottom line: QUERY is Google Sheets' most powerful single function. Master SELECT, WHERE, GROUP BY, and PIVOT and you can build any dashboard, report, or filtered view your team needs — all as live, auto-updating formulas.