Google Sheets Query Format: Syntax & Examples

How-to basics · Updated November 23, 2026

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:F500 or a named range like SalesData. 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 1 if the first row contains column labels, 0 if 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 ASC or DESC: 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, but WHERE 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:

  1. 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.
  2. 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.
  3. 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

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.