What are SQL-style queries in Google Sheets?

The QUERY function runs a SQL-like statement against a range in your sheet. You SELECT columns, filter with WHERE, sort with ORDER BY, and aggregate with GROUP BY without building many helper columns. It is the closest native option to a small in-sheet database query.

When to use it

Use QUERY when you filter and summarize rectangular data often: top products by revenue, active rows from a staging tab, or pivot-like rollups that would need messy FILTER stacks.

When to skip it

Skip QUERY for one-line lookups (use XLOOKUP or INDEX/MATCH), heavy joins across many keys, or when teammates cannot read SQL-ish strings in formulas.

How it works

  1. 1

    Point QUERY at a range including headers, for example QUERY(Data!A1:F, "select ...", 1).

  2. 2

    Write select Col1, sum(Col3) with column letters as Col1, Col2 matching the range width.

  3. 3

    Add where Col4 = 'Active' and order by Col3 desc for filtered sorted output.

  4. 4

    Use label sum(Col3) 'Total' to rename output headers for dashboards.

  5. 5

    Wrap in ARRAYFORMULA only when combining QUERY output with other arrays; QUERY already spills.

  6. 6

    Test the query in the query generator or a scratch cell before embedding in a published dashboard.

Examples in Google Sheets

Top ten customers

=QUERY(Sales!A1:D, "select A, sum(D) group by A order by sum(D) desc limit 10 label sum(D) 'Revenue'", 1)

Filter open tasks

=QUERY(Tasks!A1:E, "select A, B, C where E <> 'Done' order by D asc", 1) lists incomplete work.

Monthly rollup

Helper column with TEXT date as month, then QUERY groups spend by month for a chart source tab.

Build this without starting from a blank cell

Use a Better Sheets tool for SQL-style queries (QUERY), then watch a walkthrough when you want the full pattern.

Better Sheets resources

Common mistakes

  • Mismatching Col numbers after inserting a column in the source range.
  • Using double quotes inside the query string without escaping, which breaks parsing.
  • Forgetting the third argument 1 when row 1 is headers, mislabeling columns in output.
  • Expecting JOIN across two tables; QUERY handles one range unless you UNION prepared stacks.
  • Aggregating text columns without any aggregation function in SELECT.

Frequently asked questions

Is QUERY real SQL?
It is SQL-like, not full SQL. Supported clauses are limited but powerful for one-range analytics.
Can QUERY use dates?
Yes. Compare date columns with date literals or wrap with date functions inside the query string.
QUERY vs pivot table?
Pivot tables are interactive UI. QUERY is formula-driven and updates when source data changes.
Why parse error?
Check quotes, clause spelling, and that aggregate columns appear in GROUP BY when required.
Can I query another file?
IMPORTRANGE first, then QUERY the imported range in the same formula chain.
Col1 vs A in QUERY?
Inside the query string, refer to columns as Col1, Col2 by position in the range, not A1 letters.
Limit rows returned?
Add limit 50 at the end of the query string for top-N style results.
QUERY with headers in row 2?
Start the range at row 2 and pass 0 as the third argument, or move headers to row 1 for simplicity.

Related Tutorials

Watch how SQL-style queries (QUERY) works

Browse more tutorials
Search Every Function in Google Sheets

Search Every Function in Google Sheets

Built a useful tool to filter Google Sheet formulas based on keywords.

Related glossary terms

Done reading about SQL-style queries (QUERY)?

Membership unlocks 636+ tutorials, unlimited generators, and every template. Practical lessons. Zero fluff.

Need this once

Jump to a free tool or a single tutorial for this topic.

Learning Sheets for real

Unlock the full library, generators, and templates with membership.