Google Sheets QUERY Formula: Syntax, SQL Querying & Real Examples
The QUERY function is Google Sheets' most versatile formula, executing a SQL-like Google Visualization API Query Language string across spreadsheet tables to filter, aggregate, group, pivot, and sort data in a single formula.
=QUERY(data, query, [headers])Formula Parameters & Arguments
| Argument | Type | Required | Description |
|---|---|---|---|
| data | Range | Required | The cell range to query (e.g., A1:F500 or IMPORTRANGE). |
| query | Text (SQL String) | Required | The query string using clauses like SELECT, WHERE, GROUP BY, PIVOT, ORDER BY, LIMIT, and LABEL. |
| headers | Integer | Optional | The number of header rows at the top of data. Defaults to 1 or -1 (auto-detect). |
Real-World Spreadsheet Examples
Explore verified operational formulas used in enterprise reporting and database workflows:
Example 1: Filter High-Value Won Deals Sorted by Date
=QUERY(A1:E100, "SELECT A, B, D WHERE D > 10000 AND E = 'Won' ORDER BY C DESC", 1)Example 2: Departmental Expense Sum with Custom Label
=QUERY(A1:D500, "SELECT B, SUM(D) WHERE D is not null GROUP BY B LABEL SUM(D) 'Total Spend'", 1)Example 3: Pivot Sales by Region and Quarter
=QUERY(A1:D300, "SELECT A, SUM(D) GROUP BY A PIVOT B", 1)Common Errors & Solutions
Performance Pro Tips
- When querying array literals or IMPORTRANGE, refer to columns by index number (`Col1`, `Col2`) rather than letters (`A`, `B`).
- Always explicitly pass `1` for the headers argument to prevent Google from guessing header row counts incorrectly.
Frequently Asked Questions
Does QUERY recalculate automatically when data changes?
Yes. Google Sheets recalculates all dependent formulas immediately whenever referenced input cells are modified.
Is this formula compatible with Microsoft Excel?
Modern functions like XLOOKUP, FILTER, UNIQUE, SORT, and LET are natively supported in Excel 365. However, QUERY is unique to Google Sheets and uses the Google Visualization API syntax.
Related Google Sheets Formulas
ARRAYFORMULA
ARRAYFORMULA enables single spreadsheet formulas to output an entire matrix or column of calculated results automatically. When new rows are entered, ARRAYFORMULA calculates them instantly without requiring manual formula dragging.
Data TransformationFILTER
FILTER dynamically outputs only the rows or columns from a source range that satisfy one or more logical conditions. As source data updates, filtered results re-calculate automatically.
Data TransformationCHOOSEROWS
CHOOSEROWS extracts specific rows from an array or range by their index numbers. Supports negative indexing to pull rows from the bottom of a dataset.