QUERY
Run SQL-like queries against a range in Google Sheets. Filter, sort, group, aggregate, and pivot data — all in a single formula. The most powerful function in Google Sheets, with no direct Excel equivalent.
⚡ Quick Answer
QUERY runs an SQL-inspired query against a data range and returns the filtered, sorted, grouped, or pivoted result. It combines what would take FILTER + SORT + SUMIFS + PivotTable in Excel into one expression.
Syntax
Arguments
| Argument | Type | Description |
|---|---|---|
| dataRequired | Range or Array | The source range to query. Can be a sheet reference (A:H), an array, or the output of another function like IMPORTRANGE. |
| query_stringRequired | Text | The SQL-like query in quotes. Uses Google Visualization API Query Language. Combines clauses like SELECT, WHERE, GROUP BY. Case-insensitive for keywords. |
| headersOptional | Number | Number of header rows in the source data. Options: 0 (no headers), 1 (one header row), 2 (two header rows). Default is -1 (auto-detect, often unreliable — set to 1 explicitly). |
Basic usage
Query language clauses
QUERY uses Google Visualization API Query Language, an SQL-inspired mini-language. Clauses must appear in this exact order:
| Clause | Purpose | Example |
|---|---|---|
SELECTRequired |
Choose which columns to return | SELECT B, D, H or SELECT * |
WHERE |
Filter rows by conditions | WHERE B = 'North' AND H > 500 |
GROUP BY |
Aggregate rows by column values | SELECT B, SUM(H) GROUP BY B |
PIVOT |
Cross-tabulate (unique to Sheets) | SELECT B, SUM(H) GROUP BY B PIVOT E |
ORDER BY |
Sort results by column | ORDER BY H DESC |
LIMIT |
Restrict to top N results | LIMIT 10 |
OFFSET |
Skip N rows (paginate) | LIMIT 10 OFFSET 20 |
LABEL |
Rename output headers | LABEL SUM(H) 'Total Revenue' |
FORMAT |
Format numeric output | FORMAT SUM(H) '$#,##0' |
Aggregation functions
Used inside SELECT with GROUP BY to aggregate values across rows:
| Function | Returns |
|---|---|
SUM(col) | Sum of all values in the column |
COUNT(col) | Count of non-empty values |
AVG(col) | Average of numeric values |
MIN(col) | Smallest value in the column |
MAX(col) | Largest value in the column |
Common patterns
Use column letters (A, B, C…) when data comes from a sheet range. Use Col1, Col2, Col3 when data comes from an array or IMPORTRANGE. Getting this wrong causes #REF!.
Common errors
| Error | Cause & fix |
|---|---|
#VALUE! |
Invalid query syntax. Check for typos in keywords, missing quotes, or wrong clause order. |
#REF! |
Referenced column doesn't exist. Or output would overwrite existing cells — clear the spill area. |
#N/A |
Query returned no rows. WHERE clause too restrictive or headers argument wrong. |
| "Unable to parse" | Curly quotes from copy-paste, missing GROUP BY when using aggregates, or invalid DATE format. |
| Aggregate without GROUP BY error | Any non-aggregated column in SELECT must appear in GROUP BY. Add it to GROUP BY or remove from SELECT. |
📚 Want the complete deep dive?
The full pillar guide covers every clause in depth, IMPORTRANGE combinations, interactive dashboards, and includes a free companion workbook with 20+ examples.
Related functions
Frequently asked questions
Does QUERY work in Microsoft Excel?
No. QUERY is native to Google Sheets. Excel's equivalent workflow combines FILTER, SORT, SUMIFS, and PivotTables across multiple formulas or features — no single Excel function matches QUERY's expressiveness.
What order do QUERY clauses go in?
Strict order: SELECT → WHERE → GROUP BY → PIVOT → ORDER BY → LIMIT → OFFSET → LABEL → FORMAT. Not all are required, but any you include must appear in this position relative to the others.
How do I reference columns from IMPORTRANGE inside QUERY?
Use Col1, Col2, Col3 instead of letters. =QUERY(IMPORTRANGE(url, "Sheet!A:H"), "SELECT Col2, SUM(Col8) GROUP BY Col2"). The letters approach only works when data comes directly from a sheet range.
Can QUERY reference cells for dynamic filtering?
Yes, using ampersand concatenation. For text: "SELECT * WHERE B = '"&A1&"'". For numbers: "SELECT * WHERE H > "&A1. Combine with dropdowns for interactive dashboards.
Is QUERY slow?
For small ranges (under 1,000 rows), imperceptible. For medium ranges, often faster than multiple separate formulas because QUERY processes data in one pass. For very large ranges (100,000+), consider Data Connectors or BigQuery instead.