Google Sheets QUERY syntax lets you run SQL-like queries over spreadsheet ranges, enabling powerful filtering, aggregation, and transformation without leaving Sheets.
The Google Sheets QUERY function brings the power of SQL-style data manipulation directly into your spreadsheet. With a single formula, you can filter rows, select specific columns, aggregate, pivot, and even join ranges—dramatically reducing the need for helper columns or complex nested formulas.
Anyone who spends time in Google Sheets eventually bumps into the limitations of chaining FILTER
, SUMIF
, VLOOKUP
, and PIVOT TABLE
functions. QUERY consolidates much of that logic into one expressive statement that resembles ANSI SQL. This reduces formula sprawl, improves maintainability, and makes your spreadsheet logic far more transparent to other collaborators.
=QUERY(data, query_string, [headers])
data
.Think of data
as the table, query_string
as the SQL statement, and headers
as metadata that helps Sheets resolve column labels.
Chooses which columns (labeled A, B, C…) to output. Omitting SELECT returns all columns.
"select B, D"
Filters rows based on conditions. Supports >
, <
, =
, !=
, contains
, matches
(regex), logical and/or/not
, and date comparisons with date 'YYYY-MM-DD'
.
"where C > 100 and lower(E) contains 'widget'"
Sorts results ascending (default) or descending (desc
).
"order by C desc"
Paginate results just like in SQL.
"limit 10 offset 20"
Combine rows and compute aggregates with sum()
, count()
, avg()
, min()
, max()
.
"select A, sum(C) group by A"
Turn row values into columns—functionally similar to a pivot table.
"select sum(C) pivot B"
Rename output columns without touching the original data.
"label sum(C) 'Total Sales'"
Apply number or date formatting directly inside the query.
"format sum(C) '#,##0.00'"
=QUERY(Transactions!A1:F,
"select B, sum(E)
where C = 'Completed'
and date F > date '2024-01-01'
group by B
order by sum(E) desc
label sum(E) 'Revenue'", 1)
The above formula answers: “Show me total revenue by product for all completed transactions in 2024, sorted from highest to lowest.” One query, one cell.
Add the keyword Col1
, Col2
or wrap labels in backticks when headers have spaces:
"select `Product Name`, sum(E) group by `Product Name`"
QUERY lacks a native JOIN, but you can simulate it by vertically concatenating data sets:
=QUERY({Sales!A:F; Returns!A:F}, "select Col1, sum(Col6) group by Col1", 1)
Combine IMPORTRANGE
to query data living in another Google Sheet:
=QUERY(IMPORTRANGE("https://docs.google.com/…", "Raw!A1:H"),
"select Col4, count(Col1) where Col7 contains 'error' group by Col4", 1)
Transactions!$A$1:$F
) to prevent shifting as rows are inserted."select … \n where …"
).date 'YYYY-MM-DD'
to avoid locale issues.The entire query must be wrapped in double quotes. Escape internal double quotes with \"
or alternate quotes around string literals.
If your data range starts below row 1, set the headers
argument properly; otherwise Sheets mislabels columns.
QUERY expects .
for decimals regardless of your spreadsheet’s locale. Convert commas with SUBSTITUTE
or adjust locale settings.
Although QUERY’s syntax looks like SQL, it executes client-side inside Google Sheets and can’t connect to external databases. If you outgrow the spreadsheet and need to query actual warehouse tables, a dedicated SQL editor such as Galaxy offers auto-complete, versioning, and an AI copilot for writing true SQL against Postgres, Snowflake, or BigQuery. Think of QUERY as a lightweight on-ramp; Galaxy is the professional power tool once your data lives outside a sheet.
Google Sheets QUERY is a Swiss-army knife that can replace several other spreadsheet functions while giving you a taste of relational thinking. Mastering its syntax unlocks faster analyses, cleaner workbooks, and a smoother transition to full-blown SQL environments down the line.
Mastering QUERY lets analysts perform SQL-style data wrangling directly inside Google Sheets, avoiding complex nested formulas and speeding up insight discovery without exporting data to a database or BI tool.
No. It borrows much of SQL’s syntax but runs on the Google Visualization API. Some clauses (e.g., JOIN, sub-selects) are missing or limited.
Wrap the header in backticks, e.g., select `Order Date`
. Alternatively, use the generic Col1
, Col2
numbering.
Yes—use open-ended ranges like A:F
or combine with ARRAYFORMULA
to auto-extend as rows are appended.
QUERY is still a spreadsheet function, so very large ranges (>50k rows) may recalculate slowly. Consider exporting to BigQuery or using a tool like Galaxy for heavier workloads.