Google Sheets QUERY: Syntax, Examples, and Tips Explained

How-to basics · Updated November 3, 2026

Google Sheets QUERY: Syntax, Examples, and Tips Explained

In Google Sheets, QUERY is a function that retrieves, filters, sorts, groups, and summarizes rows from a range using a query-language string. For example, =QUERY(A1:D5, "select B, C, D where B = 'North'", 1) returns columns B, C, and D for rows whose region is North, assuming row 1 contains headers.

What QUERY means in Google Sheets

QUERY is both the name of the spreadsheet function and a reference to the query language written inside its second argument. That language is based on the Google Visualization API Query Language. It resembles SQL, the language used to request data from many databases, but it is a smaller, different dialect. SQL examples found online do not necessarily work in Sheets.

The function reads a supplied data range and displays matching results in the cell containing the formula and the cells below or beside it. The result is calculated from the source data, so changes to relevant source cells can update the displayed results. QUERY does not change, delete, or rearrange the original rows; it creates a separate result table.

This makes it useful when ordinary sorting or filtering is not enough—for example, when a report should show only selected columns, combine repeated categories into totals, or update automatically as source data changes. It works on data already available in the spreadsheet; it is not, by itself, a connection to an external database.

How the QUERY function works

The function has three arguments: the data to inspect, a query written as text, and an optional number telling Sheets how many header rows the data contains. The query text describes what to return and which conditions to apply. In many cases, the query is enclosed in double quotes as a text string, while text values inside it use single quotes.

For a practical example, imagine a table in A1:D5 with headers Date, Region, Product, and Sales. The four data rows contain North/Pen/12, South/Notebook/30, North/Notebook/25, and South/Pen/10. With that layout, the formula =QUERY(A1:D5, "select B, sum(D) group by B order by sum(D) desc", 1) returns two rows: South with 40, followed by North with 37. The final argument, 1, tells Sheets the first row is a header.

In the query, A through D refer to columns in the range. If the data argument is a constructed array rather than a plain range, identifiers such as Col1 and Col2 are commonly used instead. If the reference is not clear, check whether the formula uses a direct range or combines ranges into an array.

Basic syntax and headers

The general form is =QUERY(data, query, [headers]). The square brackets in that description mean the third argument is optional; do not type the brackets into the formula. The data argument is a cell range or array, the query argument is text containing the instructions, and headers is the number of header rows at the top of the data.

Use 1 when a range has one header row, 0 when it has none, or another appropriate number when it has multiple header rows. You can omit the argument or use -1 to let Sheets try to detect the header count, but automatic detection can be wrong, especially with short ranges or data whose first values resemble headings. Setting the count explicitly makes a formula easier to interpret and troubleshoot.

For a simple selection, assume A1:C6 has headers in row 1, with a person in column A, a department in B, and a score in C. The formula =QUERY(A1:C6, "select A, C where B = 'Sales'", 1) returns the names and scores for Sales rows. The query string is text, so its condition uses the exact column and value spelling relevant to the data.

Common clauses and what they do

Clauses describe the parts of a request. The order matters: a typical query selects columns, filters rows, groups or pivots when needed, sorts the output, and may then limit how many rows are returned. Some clauses only make sense with particular others; for example, a selected non-aggregated column in a grouped summary generally needs to appear in GROUP BY.

  • SELECT chooses which columns or aggregate results to return.
  • WHERE keeps rows that meet a condition, such as where D > 20.
  • ORDER BY sorts results; add desc for descending order or use asc for ascending order.
  • GROUP BY combines rows that share a value so an aggregate such as sum(D) can calculate a total for each group.
  • PIVOT turns distinct values in a column into output columns, which can create a compact cross-tab report.
  • LIMIT caps the number of returned rows, while OFFSET skips a number of rows.
  • LABEL changes an output column heading, and FORMAT can specify display formats for query results.

For instance, a query can filter to one year, group by region, total sales, and sort the totals from highest to lowest. A label can make the aggregate heading more readable. QUERY supports aggregate operations including sum, average, count, minimum, and maximum, but its language has specific syntax; check Google’s function documentation when building a complex expression rather than assuming every SQL function is available.

Useful QUERY examples

Filtering is often the easiest place to start. With the Date, Region, Product, and Sales columns described above, this formula returns North sales rows in descending sales order: =QUERY(A1:D5, "select B, C, D where B = 'North' order by D desc", 1). It returns Notebook, 25, followed by Pen, 12. The source data stays unchanged, while the output is a separate table.

To select rows on or after a date, assume column A contains actual date values, not dates stored as plain text. With the same header layout, =QUERY(A1:D5, "select A, C, D where A >= date '2026-01-01'", 1) returns the date, product, and sales for qualifying rows. The date literal uses the form date 'yyyy-mm-dd'. If dates are text or mixed with other types, convert or clean the source data before relying on date comparisons.

For totals by region, the earlier sum(D) and group by B example is a basic summary. To rename the total column, assume the same A1:D5 layout and use =QUERY(A1:D5, "select B, sum(D) group by B label sum(D) 'Total sales'", 1). This changes the displayed heading, not the source column name.

Data types, blanks, and mixed columns

QUERY works best when each column contains one consistent kind of value: dates in a date column, numbers in a numeric column, and text in a text column. If a column mixes types, Sheets determines a predominant type for that column; values of other types may be treated as null in the query. That can make apparently present values disappear from conditions, selections, or calculations.

Blank cells are also relevant. A condition such as where C is not null can exclude rows with an empty value in column C. Be precise about what counts as a blank in the source: a truly empty cell and a formula that returns an empty string may not behave identically in every operation. If the result is unexpected, inspect the source values and their underlying types, not just how they look on screen.

Text comparisons need quoted text values inside the query string, while numbers are generally written without quotes. For example, a text condition looks like where B = 'North', whereas a numeric condition looks like where D > 20. When a text value itself contains an apostrophe, the query string needs careful quoting or a different method of building the condition.

Common errors and how to fix them

A QUERY error or an unexpectedly empty result usually points to a mismatch between the data, the query syntax, and the header setting. Check the smallest parts first rather than rewriting the entire formula.

  • Confirm the data range includes the intended columns and rows, including the header row if the header argument says it does.
  • Check column references: A, B, and C refer to columns in a regular range, while constructed arrays may need identifiers such as Col1.
  • Match the header count to the actual range. If a header row is treated as data, it can affect filtering and aggregation.
  • Check quotation marks and parentheses. The formula needs double quotes around the query text, and text values within that text are usually enclosed in single quotes.
  • Verify whether numbers and dates are genuine numeric or date values rather than text that only looks like a number or date.
  • If a formula reports a parse error, check your spreadsheet locale. Some locales use semicolons between function arguments instead of commas; the query-language text itself still follows QUERY syntax.

Another frequent surprise is that the formula cannot expand because cells in its output area are occupied. Clear the cells where the result needs to appear, or move the formula to an open area. Also check the wording and capitalization of category values: a condition must correspond to the data value you intend to match.

QUERY compared with filters and other tools

Use a filter when you want to inspect or manually narrow a table in place. Use QUERY when you want a formula-driven output with chosen columns, multiple conditions, grouping, or a repeatable summary. QUERY is particularly handy for dashboards and reports because one formula can produce a changing result table as source rows are added or edited.

A pivot table is often a better choice when you want an interactive summary that readers can rearrange without editing a query string. The FILTER function is more direct for returning rows that meet conditions, while SORT can sort a range. These tools do not have identical capabilities: QUERY can combine selection, filtering, grouping, and ordering in a single expression, but its query-language rules can be less intuitive to learn.

There is no separate fee for using the QUERY function in Google Sheets. Its practical limits are more about learning the syntax, keeping source data consistent, and producing a result that fits the available sheet space. If the data is maintained in another file, it must first be made available in the spreadsheet—for example, through an appropriate import or connection—and access permissions may apply.

Frequently Asked Questions

Is QUERY in Google Sheets the same as SQL?
No. It has SQL-like clauses such as SELECT, WHERE, and ORDER BY, but it uses the Google Visualization API Query Language and supports a different, more limited set of syntax and operations. SQL statements cannot automatically be pasted into QUERY unchanged.

How do I use QUERY to select rows by a text value?
Put a condition in a WHERE clause and enclose the text value in single quotes inside the query string. For example, with Region in column B, where B = 'North' selects rows whose region is North. Set the headers argument to match the range.

What does the last number in QUERY mean?
It specifies how many header rows are at the top of the data argument. Use 1 for one header row and 0 for no header rows. Supplying the number explicitly can prevent Sheets from guessing incorrectly.

Why does QUERY return blank or missing values?
Common causes include a condition that matches no rows, a wrong column reference, an incorrect header count, or mixed data types in a column. Check the source values and try a simpler SELECT query first, then add conditions or grouping one at a time.

Can QUERY combine data from different sheets?
It can query a range on another tab in the same spreadsheet by using a sheet-qualified range, such as Sales!A1:D100. Data in a separate spreadsheet must first be brought into the current file or otherwise made available there; QUERY alone does not fetch it.

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.