The Google Sheets QUERY function format is =QUERY(data, query, [headers]), where data is the range to search, query is a string written in Google Visualization API Query Language (similar to SQL), and [headers] is an optional number indicating how many header rows are in the data. For example, =QUERY(A1:D100, "SELECT A, B WHERE C > 100", 1) returns columns A and B from rows where column C exceeds 100.
What Is the QUERY Function?
QUERY is a built-in Google Sheets function that lets you filter, sort, group, and aggregate data using a simplified SQL-like syntax. It works entirely in the browser and recalculates automatically when the source data changes. Unlike Excel’s older FILTER or database functions, QUERY combines many operations into one formula, making it a favorite for dashboards and data cleaning tasks. Excel does not have a direct QUERY equivalent; the closest are Power Query (a separate tool) or dynamic array functions like FILTER and SORT.
QUERY Syntax and Arguments
The function takes three arguments:
- data – the range to query, such as
A1:F500or a named range likeSalesData. It can include another sheet:'Sheet2'!A1:C50. - query – a string in double quotes containing the query clause. For example,
"SELECT A, B WHERE C = 'Yes'". You can also reference a cell containing the query text using&concatenation. - [headers] – optional. Enter
1if the first row contains column labels,0if there are no headers, or a negative number to skip that many rows. If omitted, Google Sheets guesses based on the data.
Column references in the query use letters (A, B, C) or column numbers (Col1, Col2) when the data range starts at A. If your range starts at B, then B becomes Col1, C becomes Col2, and so on.
Core Query Clauses
The query string can include several clauses, written in order after SELECT:
- SELECT – chooses which columns to return. Use
*for all columns, or list letters:SELECT A, C. - WHERE – filters rows. Use comparison operators (
>,<,=,<>) and logical operators (AND,OR,NOT). Text must be enclosed in single quotes:WHERE B = 'Active'. - ORDER BY – sorts results. Add
ASCorDESC:ORDER BY C DESC. - LIMIT – restricts the number of rows returned:
LIMIT 10. - OFFSET – skips a number of rows:
OFFSET 5. - LABEL – renames column headers in the output:
LABEL A 'Name', B 'Total'. - FORMAT – formats numbers and dates in the result:
FORMAT C '0.00'. - GROUP BY – groups rows for aggregation, used with aggregate functions.
- PIVOT – creates a pivot table-like summary by turning unique values in a column into new columns.
Using Aggregate Functions
QUERY supports aggregate functions: SUM(), COUNT(), AVG(), MIN(), MAX(). These require GROUP BY for any non-aggregated columns. For example, with sales data in A (region) and B (amount), the formula =QUERY(A1:B50, "SELECT A, SUM(B) GROUP BY A", 1) returns total sales per region. You can also use COUNT(A) to count non-empty cells, and AVG(B) for averages. Note that COUNT counts numbers only; use COUNTA to count text values.
Common Errors and How to Fix Them
QUERY errors are often cryptic. Here are the most frequent:
- #VALUE! – Usually means the query string has a syntax error, such as missing commas or unquoted text. Check that text values in WHERE are in single quotes.
- #REF! – The data range is invalid or the sheet name is misspelled. Ensure the range exists and is correct.
- #N/A – Often appears when the query returns no rows, or when a column reference is out of bounds. Verify column letters match your range.
- Cannot parse query – The query string is malformed. Check for extra spaces, missing keywords, or incorrect use of quotes. For example,
WHERE A = 'Yes'is correct, butWHERE A = "Yes"(double quotes) fails. - Column A is not a number – Occurs when you use a numeric function like SUM on a text column. Ensure the column contains numbers, or use
VALUE()in the data first.
To debug, start with a simple query like SELECT * and add clauses one by one.
Practical Examples
Here are three common scenarios:
- Filter and sort: With data in A1:C100 (Name, Date, Amount), use
=QUERY(A1:C100, "SELECT A, C WHERE B >= DATE '2024-01-01' ORDER BY C DESC", 1)to list names and amounts for entries after Jan 1, 2024, sorted by amount descending. - Combine text and criteria: If you have a dropdown in cell E1, use
=QUERY(A1:C100, "SELECT A, B WHERE C = '"&E1&"'", 1)to filter by the selected value. - Group by month: With dates in A and amounts in B,
=QUERY(A1:B100, "SELECT YEAR(A), MONTH(A), SUM(B) GROUP BY YEAR(A), MONTH(A) LABEL YEAR(A) 'Year', MONTH(A) 'Month'", 1)gives monthly totals.
Frequently Asked Questions
Can I use QUERY with another sheet? Yes, reference the sheet by name in the data argument, e.g., =QUERY(Sheet2!A1:B50, "SELECT *", 1). If the sheet name has spaces, wrap it in single quotes: 'My Sheet'!A1:B50.
Why does my QUERY return only the header? This usually means the WHERE clause filtered out all rows, or the range includes empty rows. Check that your criteria match the data type (numbers vs. text). Also ensure the header count is correct.
Can I use cell references inside the query string? Yes, concatenate with &. For example, to use a value in cell G1 as a filter: "WHERE A = '"&G1&"'". For numbers, no quotes needed: "WHERE B > "&G1.
What is the difference between QUERY and FILTER? FILTER is simpler and faster for basic row filtering, but QUERY can sort, aggregate, and pivot in one formula. Use FILTER for simple conditions, QUERY for complex summaries.
Does QUERY work in Excel? No, QUERY is a Google Sheets-specific function. In Excel, use FILTER, SORT, or Power Query for similar tasks.
How do I handle dates in QUERY? Use the DATE keyword with single quotes: WHERE B >= DATE '2024-01-01'. For datetime, use DATETIME '2024-01-01 00:00:00'. Always format dates in YYYY-MM-DD.
Alternatives and Limitations
When QUERY gets too complex, consider these alternatives:
- Pivot tables – Built-in pivot tables are easier for interactive summaries and don’t require formulas.
- FILTER + SORT – For simple filtering and sorting, these functions are more readable and less error-prone.
- Apps Script – For advanced data manipulation or when you need to write results to a sheet, a custom script gives full control.
QUERY has a few limitations: it cannot reference cells outside the data range, it doesn’t support JOIN operations, and it may be slower on very large datasets (over 100k rows). Also, the query string cannot exceed 50,000 characters, and the output is limited to 50,000 rows (though this is rarely an issue).
Related
- Google Sheets QUERY: Syntax, Examples & Fixes
- Generate Random QR Code: How It Works & Free Tools
- Computer Data Entry Operator: Job, Skills, Pay
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Add Footnotes at the End of a Page in Microsoft WordTo add a footnote at the bottom of a page in Microsoft Word, place the cursor after the…
- Excel Shortcut Keys for Symbols on Windows and MacIn Excel for Microsoft 365 on Windows, you can enter many symbols with an Alt code on the…
- Solar Battery Runtime Calculator: How to Use OneA solar battery runtime calculator estimates how long a battery can power your appliances by dividing usable battery…
- How to Remove Formatting Marks From a Word DocumentTo hide formatting marks in Word, select Home > Paragraph > Show/Hide ¶, or press Ctrl+Shift+8 on Windows…
- Delete a Blank Page in Word: A Simple Step-by-Step GuideTo delete a blank page in Word, show formatting marks with Ctrl+Shift+8 on Windows or Command+8 on Mac,…
- Excel Dependent Drop-Down Lists Based on Cell ValuesYes. In Microsoft Excel for Microsoft 365 on Windows, you can make a data validation list depend on…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.