The Google Sheets QUERY function lets you filter, sort, group and summarize a table with one SQL-style formula, with no scripts, add-ons or database. This guide shows the syntax on sample data with the exact result, then gives copy-paste patterns and fixes for the errors that dates, headers and multi-sheet data cause.
Quick answer: Type =QUERY(range, "select … where … order by …", 1) in an empty cell. The first argument is your table, the second is a SQL-like query that names columns by letter, and 1 says the table has one header row.
- Click an empty cell with free space below and to the right for the results.
- Enter, for example,
=QUERY(Sales!A1:G, "select C, sum(G) where E = 'Closed Won' group by C", 1). - Refer to columns by letter (
A,C) for a normal range, orCol1,Col2for arrays and IMPORTRANGE. - Wrap text values in single quotes and write dates as
date 'yyyy-mm-dd'. - Press Enter: the result table fills the cells below and to the right and updates when the source changes.
In this guide
- What the QUERY function does
- QUERY syntax and column letters
- Worked example: QUERY on sample sales data
- Core patterns you’ll use daily (filter, sort, limit)
- Aggregation, GROUP BY, and LABEL
- PIVOT tables via QUERY
- Text search, wildcards, and matches
- Dates and numbers that don’t misbehave
- Multi‑sheet analytics with array literals
- Formatting, labels, and user‑friendly output
- Practical applications and templates
- Performance and maintenance tips
- QUERY vs FILTER vs PIVOT vs SQL
- Implementation guide: build a live sales dashboard in 30 minutes
- Common QUERY errors and how to fix them
- Frequently asked questions
- Next steps
- Official docs and trusted sources
What the QUERY function does
QUERY runs a short SQL-style statement against a range and returns the matching rows and columns as a new table. The statement is a text string written in the Google Visualization API query language, a cut-down version of SQL.
- One formula replaces FILTER, SORT, UNIQUE, and pivot logic.
- Readable statements:
select,where,group by,order by,label,limit,offset,pivot. - Works across tabs and even combined ranges—perfect for reports.
QUERY syntax and column letters
The syntax is =QUERY(data, query, [headers]):
- data: your table, e.g.,
Sales!A1:G. - query: SQL‑like string wrapped in quotes.
- headers: number of header rows in
data. Use1if your first row has headers; otherwise0.
- With a plain range such as
Sales!A1:G, refer to columns by their sheet letters:A,C,G. - When
datais an array (a{...}literal,IMPORTRANGE, or the output of another function) there are no letters, so use positional names instead:Col1,Col2, and so on. - QUERY cannot reference a column by its header text, and the identifiers are case-sensitive (
A,Col1).
=QUERY(Sales!A1:G, "select A, C, G where E > 100 order by G desc limit 10", 1)Read it as: from Sales columns A to G, show columns A, C and G for rows where E is above 100, largest G first, at most 10 rows. Enter the formula in a cell with empty space below and to the right; the result spills into those cells.
Worked example: QUERY on sample sales data
Here is a small Sales tab. Row 1 holds the headers and the data runs from A2 to G7:
| A | B | C | D | E | F | G | |
|---|---|---|---|---|---|---|---|
| 1 | Date | Region | Rep | Product | Status | Units | Amount |
| 2 | 2026-01-05 | East | Ana | Pro | Closed Won | 2 | 1200 |
| 3 | 2026-01-09 | West | Ben | Basic | Open | 1 | 800 |
| 4 | 2026-01-14 | East | Ana | Basic | Closed Won | 1 | 600 |
| 5 | 2026-02-02 | West | Cara | Pro | Closed Won | 3 | 1500 |
| 6 | 2026-02-11 | East | Ben | Pro | Closed Won | 2 | 900 |
| 7 | 2026-02-20 | West | Ben | Basic | Lost | 1 | 400 |
To list every deal worth 900 or more, biggest first, enter this in an empty cell on another tab:
=QUERY(Sales!A1:G, "select B, C, G where G >= 900 order by G desc", 1)The cell should now show this table. The header row comes from your source headers:
| Region | Rep | Amount |
|---|---|---|
| West | Cara | 1500 |
| East | Ana | 1200 |
| East | Ben | 900 |
To total closed revenue per rep, add where, group by and label:
=QUERY(Sales!A1:G, "select C, sum(G) where E = 'Closed Won' group by C order by sum(G) desc label sum(G) 'Revenue'", 1)Result:
| Rep | Revenue |
|---|---|
| Ana | 1800 |
| Cara | 1500 |
| Ben | 900 |
Ben’s open (800) and lost (400) deals are left out by the where clause. Without the label clause, the second header would read sum Amount.
Core patterns you’ll use daily (filter, sort, limit)
Filter and sort your pipeline
=QUERY(Pipeline!A1:F, "select A, B, E where D = 'Qualified' and E >= 10000 order by E desc", 1)Return top N by metric
=QUERY(Products!A1:G, "select A, B, F order by F desc limit 20", 1)Exclude blank rows
=QUERY(Leads!A1:E, "select * where B is not null and C is not null", 1)Aggregation, GROUP BY, and LABEL
Summarize revenue by sales rep
=QUERY(Sales!A1:G, "select C, sum(G) where E = 'Closed Won' group by C order by sum(G) desc label sum(G) 'Revenue'", 1)Monthly rollups (date bucketing)
Bucket raw dates by month inside the query with themonth() scalar function.
=QUERY(Sales!A1:G, "select month(A)+1, sum(G) where E = 'Closed Won' group by month(A)+1 label month(A)+1 'Month', sum(G) 'Revenue'", 1)month() returns 0–11, so add 1 for the usual 1–12. If the data spans more than one year, add year(A) to both the select and group by clauses; otherwise the same month of different years is summed together. On the sample data above, this returns two rows: month 1 with 1800 and month 2 with 2400. Column A must hold real dates, not text that looks like dates.
Average deal size by industry
=QUERY(Accounts!A1:H, "select D, avg(G) where G is not null group by D order by avg(G) desc label avg(G) 'Avg Deal'", 1)PIVOT tables via QUERY
Build a dynamic pivot without the Pivot Table UI.
=QUERY(Orders!A1:H, "select C, sum(G) group by C pivot E", 1)Text search, wildcards, and matches
Case‑insensitive contains
=QUERY(Customers!A1:F, "select * where lower(B) contains 'enterprise'", 1)Regex matching with matches
=QUERY(Emails!A1:B, "select * where A matches '.*@example\.(com|io)'", 1)matches has to match the whole cell value, not just part of it, so add .* wherever other text may come before or after.
Dates and numbers that don’t misbehave
- Literal dates:
where A >= date '2026-01-01'(alwaysyyyy-mm-dd, whatever your spreadsheet locale) - Between:
where A >= date '2026-01-01' and A < date '2027-01-01' - Mixed columns: QUERY treats each column as a single type (the majority) and returns values of the other type as blanks, so convert text-stored numbers with
VALUE()in a helper column, or clean them upstream.
=QUERY(Events!A1:F, "select B, count(B) where A >= date '2026-01-01' group by B", 1)Multi‑sheet analytics with array literals
You can union ranges before querying. Stack identical schemas vertically with; or combine columns horizontally with ,. (Those are the separators for English-style locales. Where the decimal separator is a comma, function arguments are separated by ; and array columns by \.)
=QUERY(
{Jan!A1:G; Feb!A2:G; Mar!A2:G},
"select Col3, sum(Col7) where Col3 is not null group by Col3 order by sum(Col7) desc",
1
)Col1-style names, and where Col3 is not null drops the blank rows that open-ended ranges bring in.
Formatting, labels, and user‑friendly output
Uselabel to rename computed columns and format to change display.
=QUERY(Sales!A1:G, "select C, sum(G) group by C label C 'Rep', sum(G) 'Revenue' format sum(G) '$#,##0'", 1)Practical applications and templates
1) Live KPI summary
Counts events since the first day of the current month:=QUERY(Activity!A1:F, "select B, count(B) where A >= date '" & TEXT(EOMONTH(TODAY(), -1) + 1, "yyyy-mm-dd") & "' group by B label count(B) 'Events'", 1)2) Clean product catalog
=QUERY(Catalog!A1:H, "select A, B, F where F is not null and G = 'Active' order by B", 1)3) Revenue leaderboard (rolling 90 days)
=QUERY(Sales!A1:G, "select C, sum(G) where A >= date '" & TEXT(TODAY()-90, "yyyy-mm-dd") & "' group by C order by sum(G) desc label sum(G) 'Revenue'", 1)Performance and maintenance tips
- Keep ranges tight: Limit
A1:Zto only needed columns to reduce recalculation cost. - Clean once, use many: Put data cleaning (TRIM, VALUE, date coercion) in a prep tab. Query the clean output for speed.
- Document column mappings: Next to your formula, add a note: A=Date, B=Owner, G=Revenue.
- Filter with a dropdown cell: Slicers do not apply to formulas, so they will not change a QUERY result. Put a dropdown in a cell and build its value into the query string instead, e.g.
"select C, sum(G) where D = '" & B1 & "' group by C".
QUERY vs FILTER vs PIVOT vs SQL
- FILTER: quick, great for simple one‑condition pulls. QUERY wins when you need sort/label/group/pivot in one place.
- Pivot Table UI: excellent for ad‑hoc summaries. QUERY builds repeatable, formula‑driven pivots in reports.
- External SQL: BigQuery/DBs are best for large datasets and joins. For in‑sheet analytics on small/medium tables, QUERY is faster to ship.
Implementation guide: build a live sales dashboard in 30 minutes
- Prep data: Create a Clean_Sales tab with TRIM/VALUE and standardized headers.
- KPIs: Add a KPIs tab; write a
QUERYthat rolls up revenue last 30/90 days. - Leaderboards:
QUERYby rep and by product; addlabelandformat. - Charts: Insert bar/line charts from the KPIs ranges.
- Filters: Add dropdown cells (date range, region) and reference them in your QUERY strings. Slicers only filter charts and pivot tables built directly on Clean_Sales, not formula output.
- Ship: Protect formula ranges, share view‑only with stakeholders.
Common QUERY errors and how to fix them
- #N/A “Query completed with an empty output”: no row matched. Check the spelling and case of text in single quotes (
'Closed Won'is not'closed won'), and that numbers are not stored as text. - #REF! “Array result was not expanded because it would overwrite data”: something already sits in the cells the result needs. Clear the cells below and to the right of the formula, or move the formula to an empty area.
- #VALUE! from query string: Check quotes and escaped characters. Use
"for the outer string and single quotes inside. The error message names the part it could not parse. - Type mismatches: Ensure date columns are true dates, numbers are numeric. Clean in a prep tab.
- Wrong columns after insertions: The letters inside the query string are plain text, so they do not shift when you insert or move columns. Re-check the query after any structural change to the source range.
- NO_COLUMN error: The identifier does not exist in
data. Use letters that fall inside a plain range, andCol1-style names for arrays and IMPORTRANGE. - Headers off by one: If output includes header labels or misses a row, adjust the
headersargument (0 or 1).
Frequently asked questions
Can QUERY use column names instead of letters?
No. QUERY identifies columns by letter (A, B) for a normal range, or by position (Col1, Col2) for arrays and IMPORTRANGE, never by header text. Write a short note such as A=Date, C=Rep, G=Amount next to the formula so others can read it.
How do I use a cell value inside a QUERY?
Join the cell into the query string with &: "select C, sum(G) where B = '" & H1 & "' group by C". Keep the single quotes around text values; leave them out for numbers. For a date in a cell, use date '" & TEXT(H1, "yyyy-mm-dd") & "'.
What’s the difference between QUERY and FILTER?
FILTER only returns the rows that match your conditions, using normal formula syntax. QUERY can also pick and reorder columns, sort, group, total, pivot and rename headers in one step. Use FILTER for quick row filtering and QUERY when you need a summary or a report-style table.
Can QUERY pull data from several tabs or files?
Yes. Stack tabs with an array literal such as {Tab1!A1:G; Tab2!A2:G} and refer to the columns as Col1, Col2. For other spreadsheets, wrap IMPORTRANGE inside QUERY; the IMPORTRANGE master sheet guide shows how to combine several files.
Can QUERY join two tables?
No. QUERY works on a single table, and stacking ranges with an array literal is a union, not a join. To bring a matching value in from a second table, add a lookup column next to the QUERY result; see XLOOKUP vs VLOOKUP in Google Sheets. For heavier joins, consider BigQuery or Apps Script.
Next steps
Copy the sample table above into a blank tab, paste the two worked formulas and check that you get the same results. Then swap in your own range and column letters. If your source columns hold messy text such as mixed phone formats or embedded IDs, clean them first with REGEXEXTRACT patterns for data cleaning and point QUERY at the clean columns.
Official docs and trusted sources
- Google Sheets function reference: support.google.com/docs
- QUERY language (Visualization API): developers.google.com
- Spreadsheet tips (Help Center): support.google.com/docs

