QUERY Function in Google Sheets — Syntax, Arguments, Examples (2026)
Home› Google Sheets› Functions› QUERY
Google Sheets Function Reference

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

Function Signature
=QUERY(data, query_string, [headers])

Arguments

ArgumentTypeDescription
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

Return all data with headers
=QUERY(A:H, "SELECT *", 1)
Returns every row and column from range A:H, treating row 1 as the header
Filter and aggregate in one formula
=QUERY(A:H, "SELECT B, SUM(H) WHERE C = 'Software' GROUP BY B ORDER BY SUM(H) DESC", 1)
Groups by column B, sums column H, filtered to only Software rows, sorted highest first

Query language clauses

QUERY uses Google Visualization API Query Language, an SQL-inspired mini-language. Clauses must appear in this exact order:

ClausePurposeExample
SELECT
Required
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:

FunctionReturns
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

Top 10 rows sorted by amount
=QUERY(A:H, "SELECT * ORDER BY H DESC LIMIT 10", 1)
Filter by date range
=QUERY(A:H, "SELECT * WHERE A > DATE '2026-06-01'", 1)
Dates need the DATE keyword and ISO format (yyyy-mm-dd) inside the query string
Text search with LIKE wildcards
=QUERY(A:H, "SELECT * WHERE D LIKE '%Analytics%'", 1)
Use % for any characters, _ for single character. Case-sensitive.
Multiple aggregates by category
=QUERY(A:H, "SELECT B, COUNT(A), SUM(H), AVG(H) GROUP BY B", 1)
Deal count, total revenue, average deal — grouped by column B
Cross-tabulation with PIVOT
=QUERY(A:H, "SELECT B, SUM(H) GROUP BY B PIVOT E", 1)
Regions as rows, categories as columns, totals in cells
Dynamic filter from a cell
=QUERY(A:H, "SELECT * WHERE B = '"&E1&"'", 1)
Uses value from E1 as the filter — perfect for interactive dashboards with dropdowns
Reference columns by letters or Col numbers

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

ErrorCause & 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.

Read the Complete Guide

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.