Google Sheets QUERY: How to Write and Use the Function

How-to basics · Updated November 3, 2026

Google Sheets QUERY: How to Write and Use the Function

To write a query in Google Sheets, enter the QUERY function in an empty cell, give it a data range, and put a query-language instruction in quotation marks. For example, with a table in A1:D, =QUERY(A1:D,"select A, B, D where D > 100",1) returns rows whose value in column D is greater than 100. QUERY can select, filter, sort, group, and summarize data without changing the source table.

The examples below are for Google Sheets in a web browser. QUERY is a Google Sheets function and does not work as written in Excel. Before starting, make sure your table has consistent columns and that you know whether its first row contains headers.

What You’ll Need

  • A Google Sheets spreadsheet with data arranged in columns. A small, clean table is easiest to learn with.
  • Column headings in the first row, if you want to identify fields by their column letters and make results easier to read.
  • An empty cell or clear area where the results can expand. The query returns a table, not just one value.

Step-by-Step

  1. Check and organize the source data. Put one field in each column, use one header row, and avoid blank rows within the table. For example, use A1:D5 for Date, Region, Item, and Sales. Keep dates as dates and sales as numbers; values that look alike but are stored as different types can produce confusing results. The sample rows might be West, Notebook, 120; East, Pen, 40; West, Folder, 85; and East, Notebook, 150, with a date in column A for each row.
  2. Choose a destination cell. Click a blank cell outside the source range, such as F1. QUERY spills its results into adjacent cells, so clear enough room to the right and below. It is fine to put the result on another sheet, but the range reference must point to the source sheet, for example Data!A1:D when the source tab is named Data.
  3. Enter the basic QUERY structure. In F1, enter =QUERY(A1:D,"select A, B, C, D",1). This selects all four fields and includes the first row as a header. The arguments are the data range, the query-language text, and the number of header rows. In this example the result includes the column headings and all four sample records. If the range starts on a different row, adjust the range, but still set the final argument to the number of header rows included in that range.
  4. Choose only the columns you need. Replace the list after select with the column letters you want returned. With the same A1:D data layout, =QUERY(A1:D,"select A, C, D",1) returns Date, Item, and Sales, leaving Region out of the result. This is useful when a wide source table contains fields that are not relevant to a report. For a normal sheet range, use the sheet column letters, such as A or D; for some constructed arrays, QUERY uses identifiers such as Col1 and Col2 instead.
  5. Filter rows with WHERE. Add a condition after the selected fields. With headers in row 1 and sales in D, =QUERY(A1:D,"select A, B, C, D where D > 100",1) returns the two sample sales of 120 and 150. Use a single quote around text values inside the query string: with Region in B, =QUERY(A1:D,"select A, B, C, D where B = 'West'",1) returns the West records. Use operators such as =, >, <, >=, and <> for comparisons.
  6. Sort the results. Add order by and a column after the filter, if there is one. With the same source, =QUERY(A1:D,"select A, B, C, D order by D desc",1) sorts Sales from largest to smallest; use asc for ascending order. You can sort by more than one field by listing additional columns, such as order by B asc, D desc. This changes the order of the query output, not the original data.
  7. Summarize by category when useful. To add sales by region, use =QUERY(A1:D,"select B, sum(D) where B is not null group by B label sum(D) 'Total sales'",1). With the sample values, the result is East: 190 and West: 205; those totals are 40 + 150 and 120 + 85. The nonblank condition excludes empty region cells, group by makes one row per region, and label changes the displayed heading. A selected field that is not summarized generally needs to be included in the grouping.
  8. Set the header argument and review the output. Use 1 when the first row of the data range contains one header row, and 0 when the range has no header row. You can also omit the argument and let Sheets guess, but an explicit value is more predictable. Press Enter, then check that the output headings and rows match your intent. If Sheets displays a parse error, inspect the quotation marks, commas, and parentheses first.

QUERY’s query language supports more than the clauses shown here, but build a query one part at a time: first select fields, then add a filter, then sorting or grouping. That makes it easier to see which change caused an unexpected result.

Common Mistakes

  • Using the wrong header count. The last argument describes header rows at the top of the selected range, not the number of columns. If the range includes one heading row, use 1; if it starts with data, use 0.
  • Putting spreadsheet syntax inside the query text. The query language has its own words and operators. For instance, write where D > 100 inside the query string; do not put a cell formula such as =D1>100 there.
  • Mixing data types in a column. If a column contains mostly numbers but also text such as “unknown,” QUERY may treat the minority type as blank. Clean or separate inconsistent values before relying on a filter or calculation.
  • Forgetting to quote text or dates properly. Text conditions need single quotes inside the double-quoted query string. Date comparisons use a date literal in the query language, not an ordinary unquoted date-looking phrase.
  • Blocking the expanding result. A value or merged cell in the output area can stop the query from displaying. Keep its spill area clear and avoid putting the formula inside the source range it reads.
  • Expecting Excel to run the same function. Google Sheets QUERY is not an Excel function. If you open the file in Excel, use Excel tools or formulas instead; do not assume the QUERY formula will translate.

Tips

  • Use a bounded range such as A1:D500 when the table is small and fixed. Use a whole-column range such as A:D when new rows will be added regularly, while keeping unrelated content out of those columns.
  • Write the query text in a readable order: select, optional where, optional group by, then optional order by. QUERY keywords are not case-sensitive, but consistent capitalization can make a long instruction easier to scan.
  • For text comparisons, check for leading or trailing spaces in the source. A cell containing “West ” may not match the intended text “West”. Use Sheets data cleanup tools or trim the source values when needed.
  • For dates, make sure the cells are actual dates. The query language uses a date literal like date '2026-01-01'. With dates in A and headings in row 1, an example is =QUERY(A1:D,"select A, B, D where A >= date '2026-01-01'",1). This includes records on or after January 1, 2026.
  • In Sheets locales that use semicolons as formula argument separators, you may need semicolons between the function arguments instead of commas. The commas and punctuation inside the quoted query-language text are not all function separators, so change only the formula-level separators appropriate to your locale.
  • Keep a copy of the source data before doing major cleanup. QUERY creates a result view and does not edit the source table, which makes it useful for reports and summaries.

Troubleshooting

“Unable to parse query string” or a formula parse error: Check that the whole query instruction is inside double quotes, text values are inside single quotes, and each parenthesis is closed. Confirm that clauses appear in a sensible order. Also check whether your spreadsheet locale expects semicolons between function arguments.

The result is blank or missing expected rows: Verify the column letter and condition, then inspect the source cells for extra spaces, text stored in a number column, or dates stored as text. Try a simple selection first, then reintroduce the filter. For dates, test a known row and confirm that its cell is formatted and stored as a date value.

The output has unexpected headings or an extra row: Check where the selected range begins and whether the header argument matches it. If the range begins at A1 and row 1 contains headings, use 1. If it begins at A2 and contains only records, use 0.

“Array result was not expanded” or only part of the output appears: Clear cells in the expected output area, including hidden content, and unmerge cells that overlap it. If the formula is on another tab, confirm that the source range reference points to the correct sheet and that the tab name is spelled correctly.

A summary is missing categories or totals: Check that the category and measure columns contain consistent values. For grouped totals, make sure the measure is numeric and every non-aggregated field in the selected list is also included in group by.

Frequently Asked Questions

What does the QUERY function do in Google Sheets?
It returns a table based on a range and instructions written in Google Visualization API Query Language. You can select columns, filter rows, sort results, and perform supported summaries such as sums and counts.

What is the basic QUERY formula?
The pattern is =QUERY(data, query, headers). For example, the data argument could be A1:D, the query could select columns or add conditions, and headers could be 1 for a single header row. The formula uses commas in the standard US-style locale.

Can I use QUERY to search for text?
Yes. Add a text condition after where and put the text in single quotes inside the query string, such as where B = 'West'. Check spacing and spelling if the filter returns no matches.

Can QUERY sort and filter at the same time?
Yes. Put where before order by, for example select A, D where D > 100 order by D desc. Filtering determines which records remain; sorting determines their displayed order.

Why does QUERY show some values as blank?
A column with mixed types can be interpreted as the type that appears most often, leaving minority-type entries blank in QUERY results. Make the column consistent, or separate text and numeric values into different fields.

Does the QUERY function work in Microsoft Excel?
No. QUERY is specific to Google Sheets. Excel has different tools and functions for querying or filtering data, so a Sheets QUERY formula generally needs to be rewritten when moving a workbook to Excel.

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.