QUERY Function
QUERY lets you run SQL-like commands on your spreadsheet data. Filter, sort, aggregate, pivot — all in one formula. It's the Swiss Army knife of Google Sheets data manipulation.
Syntax
Parameters
| Parameter | Description | Required |
|---|---|---|
| data | The range of cells to query. Include header row if you want to reference columns by name. | Required |
| query | A query string using Google Visualization API Query Language (similar to SQL). E.g., 'SELECT A, B WHERE C > 100 ORDER BY B DESC'. | Required |
| headers | Number of header rows at the top of data. -1 = auto-detect, 0 = no headers. Default is -1. | Optional |
Basic Example
Select all rows where the region is 'East' and sort by sales descending
=QUERY(A1:E50, "SELECT A, B, D WHERE C = 'East' ORDER BY D DESC", 1)SELECT A, B, D picks 3 columns. WHERE C='East' filters by region. ORDER BY D DESC sorts by sales. 1 header row.
Advanced Examples
Example 1: QUERY with aggregation (GROUP BY)
ReportingSum sales by region using GROUP BY
=QUERY(A1:E50, "SELECT C, SUM(D) GROUP BY C ORDER BY SUM(D) DESC", 1)Example 2: QUERY with multiple conditions
Complex filteringFilter for East region sales above $5000 in Q1
=QUERY(A1:F50, "SELECT A, B, D WHERE C='East' AND D>5000 AND F='Q1'", 1)Example 3: QUERY with IMPORTRANGE for cross-sheet analysis
Multi-sheet dataQuery data from another spreadsheet entirely
=QUERY(IMPORTRANGE("sheet_url", "Sheet1!A1:E100"), "SELECT Col1, Col4 WHERE Col3='East'", 1)How QUERY Works
QUERY treats your spreadsheet range like a database table. The query string uses a SQL-like language: SELECT chooses columns, WHERE filters rows, GROUP BY aggregates, ORDER BY sorts, LIMIT restricts row count. Column references use letters (A, B, C) when querying a direct range, or Col1, Col2, Col3 when querying computed arrays like IMPORTRANGE results. The function evaluates the query and returns a dynamic result set that spills into adjacent cells.
Important Notes & Limitations
QUERY is exclusive to Google Sheets — there is no equivalent in Excel.
QUERY cannot reference columns by letter (A, B) when the data comes from IMPORTRANGE — use Col1, Col2 instead.
QUERY has limited date handling — dates must be formatted carefully in the query string.
Complex queries may be slow on very large datasets (10K+ rows).
QUERY doesn't support JOIN operations between different ranges.
Common Errors & Fixes
#VALUE! (Col not found)Referencing column by letter (A, B) when data comes from IMPORTRANGE or computed arraysFix: Use Col1, Col2, Col3 notation for computed array sources. Letters only work with direct range references.
Empty resultWHERE condition doesn't match any rows, or date format is wrongFix: Check that your conditions actually match data. For dates, use date 'YYYY-MM-DD' format in the query.
#N/A (query parse error)Syntax error in the query stringFix: Verify SQL syntax: quotes around text values, proper SELECT/WHERE/ORDER BY structure, no typos.
Download Practice File
QUERY
queryPractice QUERY with Real Data
Download a sample CSV file with pre-populated data and practice exercises for the QUERY function. Works in both Excel and Google Sheets.
File format: CSV (comma-separated values) - opens in Excel, Google Sheets, and all spreadsheet apps
Related Tutorials
Google Sheets ARRAYFORMULA
Google Sheets ARRAYFORMULA applies one formula to an entire range at once, so you never copy down again. Learn how to combine it with IF, FILTER, and QUERY.
Master the QUERY Function in Google Sheets
Learn how to use Google Sheets' powerful QUERY function to filter, sort, and analyze data like a database.