Google Sheets QUERY Function: SQL Power Without SQL

by Fahim Mahmud Chisti

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.

  1. Click an empty cell with free space below and to the right for the results.
  2. Enter, for example, =QUERY(Sales!A1:G, "select C, sum(G) where E = 'Closed Won' group by C", 1).
  3. Refer to columns by letter (A, C) for a normal range, or Col1, Col2 for arrays and IMPORTRANGE.
  4. Wrap text values in single quotes and write dates as date 'yyyy-mm-dd'.
  5. Press Enter: the result table fills the cells below and to the right and updates when the source changes.
In this guide
  1. What the QUERY function does
  2. QUERY syntax and column letters
  3. Worked example: QUERY on sample sales data
  4. Core patterns you’ll use daily (filter, sort, limit)
  5. Aggregation, GROUP BY, and LABEL
  6. PIVOT tables via QUERY
  7. Text search, wildcards, and matches
  8. Dates and numbers that don’t misbehave
  9. Multi‑sheet analytics with array literals
  10. Formatting, labels, and user‑friendly output
  11. Practical applications and templates
  12. Performance and maintenance tips
  13. QUERY vs FILTER vs PIVOT vs SQL
  14. Implementation guide: build a live sales dashboard in 30 minutes
  15. Common QUERY errors and how to fix them
  16. Frequently asked questions
  17. Next steps
  18. Official docs and trusted sources
Google Sheets QUERY function: SQL-style analytics inside spreadsheets
QUERY gives you SQL‑style analytics directly in Sheets—fast, readable, and maintenance‑friendly.

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. Use 1 if your first row has headers; otherwise 0.
Column references:
  • With a plain range such as Sales!A1:G, refer to columns by their sheet letters: A, C, G.
  • When data is 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:

ABCDEFG
1DateRegionRepProductStatusUnitsAmount
22026-01-05EastAnaProClosed Won21200
32026-01-09WestBenBasicOpen1800
42026-01-14EastAnaBasicClosed Won1600
52026-02-02WestCaraProClosed Won31500
62026-02-11EastBenProClosed Won2900
72026-02-20WestBenBasicLost1400

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:

RegionRepAmount
WestCara1500
EastAna1200
EastBen900

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:

RepRevenue
Ana1800
Cara1500
Ben900

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)
Tip: Use single quotes for string literals, double quotes for the whole query.

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 the month() 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)
Note: 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)
This creates columns per pivot key (e.g., regions or product lines) with sums by row group.

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' (always yyyy-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
)
Only the first range includes the header row; the others start at row 2 so their headers are not stacked in as data. The data is now an array, so the query uses 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

Use label 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:Z to 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

  1. Prep data: Create a Clean_Sales tab with TRIM/VALUE and standardized headers.
  2. KPIs: Add a KPIs tab; write a QUERY that rolls up revenue last 30/90 days.
  3. Leaderboards: QUERY by rep and by product; add label and format.
  4. Charts: Insert bar/line charts from the KPIs ranges.
  5. 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.
  6. 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, and Col1-style names for arrays and IMPORTRANGE.
  • Headers off by one: If output includes header labels or misses a row, adjust the headers argument (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

all_in_one_marketing_tool