Google Sheets QUERY Function: SQL-Style Data Analysis in Your Spreadsheet
Master the QUERY function in Google Sheets. Learn the Google Visualization Query Language syntax to filter, sort, group, and aggregate spreadsheet data without leaving your cells.
The QUERY function is arguably Google Sheets' most powerful built-in function. It accepts a range of data and a query string written in the Google Visualization Query Language (GQL) — a SQL dialect — and returns a dynamically filtered, sorted, or aggregated result set, all within a single formula.
Basic Syntax
- data — The cell range to query (e.g.,
A1:F200or a named range). - query — A GQL query string enclosed in double quotes, or a cell reference to a string.
- headers — Optional. Number of header rows in
data(usually1).
Your First QUERY: Selecting Columns
Think of SELECT as choosing which columns to return. Columns are referenced by letter (A, B, C…) matching their position in the data range.
Return only the Name and Revenue columns from a dataset:
To return all columns in order:
Filtering Rows with WHERE
The WHERE clause filters rows, equivalent to a spreadsheet filter.
Return all rows where Region is "North":
Important: String values in GQL must use single quotes, not double quotes (which enclose the entire query string).
Combining Conditions
Use AND and OR to combine multiple conditions:
Using Cell References as Filter Values
To make the filter dynamic, concatenate a cell value into the query string. Assume the region filter is in cell H1:
For numeric comparisons:
Sorting with ORDER BY
Sort results by revenue descending, then by name ascending:
DESC = descending (largest first), ASC = ascending (smallest first, the default).
Limiting Results with LIMIT and OFFSET
Return only the top 10 rows:
Skip the first 5 rows and return the next 10 (useful for pagination):
Aggregating Data with GROUP BY
Summarize total revenue per region:
Rename the aggregated column header with LABEL:
Available Aggregate Functions
| Function | Description |
|---|---|
SUM(col) | Total of numeric column |
COUNT(col) | Number of non-empty cells |
AVG(col) | Average value |
MAX(col) | Maximum value |
MIN(col) | Minimum value |
Filtering with Date Columns
Dates in GQL use the date 'YYYY-MM-DD' literal syntax:
To use a dynamic date from cell J1 (which contains a date value):
The TEXT function formats the date serial number into the string format GQL requires.
QUERY Across Multiple Sheets with IMPORTRANGE
Combine QUERY with IMPORTRANGE to query data from another Google Sheets file:
Note: When using IMPORTRANGE as the data source, use
Col1,Col2... notation instead ofA,B... since the columns are renumbered.
Common Errors and Solutions
| Error | Cause | Fix |
|---|---|---|
#N/A | No rows match the WHERE clause | Add IFERROR() or check condition syntax |
#VALUE! | Wrong data type in WHERE | Ensure string values use single quotes |
Unable to parse query string | Syntax error in GQL | Check for mismatched quotes or unsupported keywords |
| Headers repeating in results | headers argument set to 0 with header row present | Set headers to 1 |