Google Sheets QUERY: Syntax, Examples & Fixes

How-to basics · Updated November 23, 2026

The Google Sheets QUERY function is a powerful tool that lets you run SQL-like queries on your data directly within a spreadsheet. In short, it uses the syntax =QUERY(data, query, [headers]) to filter, sort, group, and pivot data using a simple query language. This article explains how it works, provides concrete examples, and covers common problems and fixes.

What Is the QUERY Function?

QUERY is a built-in Google Sheets function that interprets a text string as a query in a language similar to SQL (Structured Query Language). It takes a range of cells as its data source, then applies the query text to that range, returning a new table of results. Unlike filters or pivot tables, QUERY can combine multiple operations—like filtering, sorting, aggregating, and labeling—in a single formula. It is especially useful for summarizing large datasets without creating intermediate helper columns.

QUERY Syntax and Arguments

The basic syntax is:

=QUERY(data, query, [headers])
  • data – the range of cells to query (e.g., A1:D100).
  • query – a text string in double quotes that contains the query commands (e.g., "select * where B > 10").
  • headers – optional; the number of header rows in the data. If omitted, Google Sheets guesses based on the data. Use 1 if the first row contains column labels.

For example, with data in A1:C20 (where A has names, B has sales, C has region), to get all rows where sales exceed 100, you would write:

=QUERY(A1:C20, "select * where B > 100", 1)

This returns rows where column B is greater than 100, preserving the header row.

Core Query Clauses: SELECT, WHERE, ORDER BY, GROUP BY

The query language supports several clauses that you can combine:

  • SELECT – chooses which columns to return. Use column letters (A, B, C) or * for all columns.
  • WHERE – filters rows based on conditions. Supports comparison operators (>, <, =, <>) and logical operators and and or.
  • ORDER BY – sorts the result by one or more columns, with asc or desc.
  • GROUP BY – groups rows by a column and aggregates values using functions like sum(), avg(), count(), max(), min().
  • LABEL – renames the output column headers.
  • LIMIT and OFFSET – limit the number of rows returned and skip a number of rows.

Example: To get total sales by region, sorted descending:

=QUERY(A1:C20, "select C, sum(B) group by C order by sum(B) desc label sum(B) 'Total Sales'", 1)

This returns two columns: Region and Total Sales, sorted from highest to lowest.

Practical Examples of QUERY in Action

Here are a few common scenarios with real formulas:

  1. Filter with multiple conditions: Get rows where region is “West” and sales > 500.
    =QUERY(A1:C20, "select * where C = 'West' and B > 500", 1)
  2. Sort by a column: Get all data sorted by sales ascending.
    =QUERY(A1:C20, "select * order by B asc", 1)
  3. Count entries per category: Count how many rows per region.
    =QUERY(A1:C20, "select C, count(A) group by C", 1)
  4. Average with label: Average sales per region, renaming the average column.
    =QUERY(A1:C20, "select C, avg(B) group by C label avg(B) 'Average Sales'", 1)

Note that text values in WHERE clauses must be enclosed in single quotes ('West'), while numbers need no quotes.

Common Errors and How to Fix Them

QUERY often throws errors that confuse users. Here are the most frequent ones:

  • #VALUE! – “Unable to parse query string” – This usually means a syntax error in the query text. Check for missing quotes, stray commas, or misspelled keywords.
  • #REF! – “Column X does not exist” – You referenced a column letter that is outside the data range. If your data has 3 columns, you can only use A, B, C.
  • #N/A – “No data” – The query returned no rows. Check your WHERE condition; perhaps the values don’t match exactly (case sensitivity or extra spaces).
  • #DIV/0! – “Division by zero” – Occurs when using aggregate functions like avg() on an empty set. Use IFERROR to handle it.
  • Mismatched data types – If you compare a text column to a number, you’ll get parse errors. Ensure column types are consistent.

A quick fix for many errors is to use the IFERROR wrapper: =IFERROR(QUERY(...), "No results").

QUERY vs. FILTER vs. Pivot Tables

QUERY is not the only way to summarize data. Here’s how it compares:

  • FILTER – Simpler for basic row filtering. Syntax: =FILTER(range, condition1, [condition2]). It cannot sort or aggregate directly.
  • Pivot tables – Great for interactive analysis and grouping. They allow drag-and-drop, but they are not formula-driven and don’t update dynamically unless refreshed.
  • QUERY – Combines filtering, sorting, grouping, and aggregation in one formula. It updates automatically when data changes, and it can be nested inside other functions.

For example, to filter rows where sales > 100, you could use =FILTER(A1:C20, B1:B20>100). But to also sum sales by region, QUERY is the better choice.

Performance and Best Practices

QUERY can slow down large spreadsheets if used excessively. To keep things fast:

  • Reference only the necessary rows and columns, not entire columns (e.g., A1:C1000 instead of A:C).
  • Avoid using QUERY in array formulas over huge ranges.
  • Use LIMIT to reduce output size when you only need a few rows.
  • If you need to reuse the same query often, consider creating a named range for the data.

Also, QUERY is case-sensitive for text comparisons. If you need case-insensitive matching, use lower() or upper() functions inside the query, e.g., where lower(C) = 'west'.

Frequently Asked Questions

Can I use QUERY with data from another sheet?

Yes. Reference another sheet by name, e.g., =QUERY(Sheet2!A1:C20, "select *", 1). If the sheet name contains spaces, enclose it in single quotes: 'My Sheet'!A1:C20.

How do I use a cell value in the WHERE clause?

You can concatenate the cell reference into the query string. For example, if cell E1 contains the region name, use: =QUERY(A1:C20, "select * where C = '"&E1&"'", 1). Note the single quotes around the value.

Why does my QUERY return the header row twice?

This happens when the headers argument is set incorrectly. If your data has a header row, use 1 as the third argument. If you omit it, Google Sheets may guess wrong. Alternatively, the query itself might select the header row as data—ensure your WHERE clause doesn’t accidentally include it.

Can I use QUERY to combine text columns?

Yes, you can concatenate columns in the SELECT clause using the & operator. For example, =QUERY(A1:B20, "select A & ' - ' & B", 1) returns a single column with combined text.

Does QUERY work in Excel?

No. QUERY is unique to Google Sheets. In Excel, you would use functions like FILTER, SORT, or Power Query (Get & Transform) to achieve similar results.

Alternatives to QUERY for Complex Tasks

If QUERY becomes too complex, consider these alternatives:

  • Google Sheets Pivot Tables – For interactive reporting without formulas.
  • FILTER + SORT + UNIQUE – For simpler operations that don’t need aggregation.
  • Apps Script – For custom data processing beyond what formulas can do.

Each has its strengths, but QUERY remains the most flexible formula-based approach for data manipulation.

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.