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
Point QUERY at a range including headers, for example QUERY(Data!A1:F, "select ...", 1).
-
2
Write select Col1, sum(Col3) with column letters as Col1, Col2 matching the range width.
-
3
Add where Col4 = 'Active' and order by Col3 desc for filtered sorted output.
-
4
Use label sum(Col3) 'Total' to rename output headers for dashboards.
-
5
Wrap in ARRAYFORMULA only when combining QUERY output with other arrays; QUERY already spills.
-
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.
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
Search Every Function in Google Sheets
Related terms
filters in Google Sheets
Filtering hides rows that do not match criteria without deleting data. The filter toolbar on a header row is the fastest UI path. FILTER and QUERY formulas build dynamic views that update when source data changes, which suits dashboards and reporting tabs.
Read guide →sorting data in Sheets
Sorting reorders rows by one or more columns. The Data > Sort range menu sorts selection in place. The SORT function returns a sorted copy that updates when inputs change. Always decide whether related columns must move together as a single row unit.
Read guide →Google Sheets formula syntax
Every formula starts with = followed by a function name and arguments in parentheses. Ranges use A1 notation, text sits in quotes, and operators like + and & combine values. Sheets recalculates when inputs change, so syntax errors show as #NAME? or #ERROR! in the cell.
Read guide →dashboards
A dashboard is a single view that answers "how are we doing?" without hunting across tabs. In Google Sheets, it is usually a summary tab with big numbers, charts, and conditional formatting fed by QUERY, pivot tables, or IMPORTRANGE from raw data you keep elsewhere.
Read guide →Done reading about SQL-style queries (QUERY)?
Membership unlocks 600+ 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.