Google Sheets QUERY Function: Master Data Retrieval
Introduction
The QUERY function is Google Sheets’ built‑in, SQL‑like engine for pulling, filtering, and reshaping data without leaving the spreadsheet. By writing a text string that resembles a simple SELECT statement, you can return exactly the rows and columns you need, apply conditions, sort, group, and calculate aggregates—all in a single formula.
=QUERY(data, query, [headers])
- data – the range to query (e.g.,
A1:E500). - query – a string containing the SQL‑style command.
- headers – optional; set to
1if the first row contains column headers, otherwise0.
Because QUERY works on a range, you can reference raw tables, named ranges, or the output of another function. A single QUERY often replaces dozens of FILTER, SUMIF, VLOOKUP, and array formulas, and it updates automatically as the source data changes—ideal for dashboards, reporting, and ad‑hoc analysis.
Basic Syntax and Parameters
| Argument | Description | Typical values |
|---|---|---|
| data | The range to search (sheet, named range, or array). | A1:D100 |
| query | A string written in Google Visualization API Query Language. | "select A, sum(C) where B = 'East' group by A" |
| headers (optional) | Number of header rows at the top of data. If omitted, Sheets guesses. | 0, 1, 2 … |
Key points
- Quotes – The whole query must be in double quotes; text literals inside use single quotes.
- Case‑insensitivity – Keywords (
SELECT,WHERE,GROUP BY, …) can be written in any case. - Header handling – When you set the third argument to
1, you can refer to column names instead of letters (e.g.,select Sales where Region='East').
Simple example
| A | B | C |
|---|---|---|
| Product | Region | Qty |
| Apple | East | 10 |
| Banana | West | 5 |
| Apple | East | 7 |
=QUERY(A1:C4, "select A, sum(C) where B = 'East' group by A", 1)
Result
| Product | sum |
|---|---|
| Apple | 17 |
Filtering Data
QUERY filters rows with a WHERE clause, just like SQL.
| A | B | C |
|---|---|---|
| Name | Dept | Salary |
| Alice | Sales | 72000 |
| Bob | Marketing | 65000 |
| Carol | Sales | 58000 |
| Dave | IT | 77000 |
| Eve | Marketing | 62000 |
Example 1 – Sales staff earning > $60 k
=QUERY(A1:C6,
"select A, C where B = 'Sales' and C > 60000",
1)
| Name | Salary |
|---|---|
| Alice | 72000 |
Example 2 – All non‑Marketing staff, sorted by salary descending
=QUERY(A1:C6,
"select A, B, C where B <> 'Marketing' order by C desc",
1)
| Name | Dept | Salary |
|---|---|---|
| Dave | IT | 77000 |
| Alice | Sales | 72000 |
| Carol | Sales | 58000 |
Tips
- Use
0for the third argument when your range has no header row. - Dates must be wrapped as
date 'yyyy-mm-dd'. - Column letters are relative to the range you pass (e.g., in
B2:E,Arefers to column B on the sheet).
Sorting and Grouping
You can sort, limit, and aggregate in the same statement.
| Goal | Sample data | Formula | Result |
|---|---|---|---|
| Sort by quantity (ascending) | Item QtyApple 10 Banana 5 Cherry 12 |
=QUERY(A1:B4,"SELECT A,B ORDER BY B",1) |
Banana 5 Apple 10 Cherry 12 |
| Top 3 regions by Jan sales | Region Sales MonthEast 5000 Jan West 7200 Jan East 6100 Feb West 4300 Feb |
=QUERY(A1:C5,"SELECT A,SUM(B) WHERE C='Jan' GROUP BY A ORDER BY SUM(B) DESC LIMIT 3",1) |
West 7200 East 5000 |
| Sum amounts per category | Category AmountFood 120 Travel 80 Food 200 |
=QUERY(A1:B4,"SELECT A, SUM(B) GROUP BY A",1) |
Food 320 Travel 80 |
Multiple Conditions and Simulated Joins
Multiple AND/OR conditions
| A (Region) | B (Product) | C (Units) |
|---|---|---|
| East | Apple | 120 |
| West | Banana | 85 |
| East | Banana | 45 |
| West | Apple | 150 |
Goal: Return rows where the region is East and the product is Apple, or where units > 100.
=QUERY(A1:C5,
"SELECT A,B,C
WHERE (A = 'East' AND B = 'Apple')
OR C > 100",
1)
| Region | Product | Units |
|---|---|---|
| East | Apple | 120 |
| West | Apple | 150 |
Simulating a JOIN
| Sales (A‑C) | Customers (E‑F) | |
|---|---|---|
| OrderID | CustomerID | Amount |
| 101 | C01 | 250 |
| 102 | C02 | 400 |
| 103 | C01 | 150 |
Combine the two tables with an array literal, then query:
=QUERY({A2:C4, E2:F4},
"select Col4, Col1, Col3
where Col4 = Col2
label Col4 'Customer', Col1 'OrderID', Col3 'Amount'",
0)
| Customer | OrderID | Amount |
|---|---|---|
| Acme | 101 | 250 |
| Beta | 102 | 400 |
| Acme | 103 | 150 |
(Col1‑Col4 refer to the columns of the combined array.)
Practical Business & Personal Examples
| # | Scenario | Sample Table | Query Formula | Result |
|---|---|---|---|---|
| 1 | Sales dashboard – total sales by region | Region Product AmountNorth Widget 1200 South Gizmo 850 North Gadget 430 East Widget 670 |
=QUERY(A1:C5,"select A, sum(C) where C>0 group by A label sum(C) 'Total Sales'",1) |
Region Total SalesEast 670 North 1630 South 850 |
| 2 | Personal budget – expenses > $100 | Date Category Cost2024‑01‑02 Groceries 45 2024‑01‑05 Utilities 120 2024‑01‑09 Dining 78 2024‑01‑12 Car Repair 250 |
=QUERY(A1:C5,"select A, B, C where C > 100",1) |
Date Category Cost2024‑01‑05 Utilities 120 2024‑01‑12 Car Repair 250 |
Common Pitfalls & Troubleshooting
| Problem | Why it Happens | Quick Fix |
|---|---|---|
| Header row mis‑identified | QUERY assumes the first row is data unless you tell it otherwise. |
Add the third argument (1 for one header row). |
| Column letters off by one | Letters are relative to the supplied range, not the sheet’s absolute columns. | Double‑check the range you pass; use A for the first column inside that range. |
| Data‑type mismatch | Comparing text to numbers (e.g., where C = "10") forces a failed conversion. |
Use the proper literal type: where C = 10 for numbers, where C = '10' for text. |
| Locale‑dependent decimal separator | Some locales use commas for decimals, breaking numeric literals inside the query string. | Use a US locale for the sheet or write numbers with periods (3.14). |
| Reserved words as column names | Names like date, order, or group clash with SQL keywords. |
Wrap the identifier in backticks: select `date`, B where `order` > 5. |
| Array‑formula errors | When combining ranges with {} the column references become Col1, Col2, … |
Refer to those generated column names (Col1, Col2, …) in the query string. |
FAQ
What is the QUERY function in Google Sheets?
QUERY lets you treat a range as a mini‑database and retrieve rows or aggregates using a SQL‑style language. It returns a dynamic array, so results update automatically when the source data changes.
How do I filter data using QUERY?
Place a WHERE clause inside the query string, e.g., =QUERY(A2:C, "select A, C where B = 'Sales' and C > 60000", 1). Use single quotes for text literals and logical operators (AND, OR, <>, etc.) as needed.
Can I combine QUERY with other functions?
Yes. QUERY can consume the output of other functions (e.g., FILTER, IMPORTRANGE, or an array literal) and can be wrapped by functions like ARRAYFORMULA, INDEX, or SORT for additional processing.
What are the limits of the QUERY function?
QUERY works on up to 5 million cells per spreadsheet and supports most SQL‑like clauses (SELECT, WHERE, GROUP BY, ORDER BY, LIMIT, LABEL). It does not support true JOIN syntax; you must concatenate ranges with {} to simulate joins. Very large datasets may become slower than native formulas.
Where can I find more advanced QUERY tutorials?
The official Google Sheets Help Center has a comprehensive QUERY guide. Community sites such as the Google Docs Editors Help forum, Ben Collins’ blog, and the Stack Overflow tag “google-sheets-query” contain deeper examples and real‑world use cases.
All formulas shown are valid in Google Sheets. In Excel 365 you can achieve similar results with FILTER, SORT, UNIQUE, and XLOOKUP, but the exact syntax differs.