Google Sheets QUERY: How It Works and How to Use It

How-to basics · Updated November 3, 2026

Google Sheets QUERY: How It Works and How to Use It

In Google Sheets, QUERY uses a SQL-like instruction inside a formula to select, filter, sort, group, and summarize data. A basic formula is =QUERY(A1:D, "select A, B where D > 10", 1): it examines columns A through D, returns columns A and B for rows where D is greater than 10, and treats the first row as a header.

What the QUERY function does

QUERY is useful when you want a live view of a table that includes only certain rows or columns, or when you need a summary grouped by a category. Rather than manually sorting or filtering the source, you describe what you want in a query string, and Sheets returns the matching results in a separate output area. When the source data changes, the result can update automatically.

The function is built around Google Visualization API Query Language. Its syntax resembles SQL, but it is a Google Sheets feature with its own rules; it is not a full SQL database engine. The function’s general form is:

=QUERY(data, query, [headers])

  • data is the cell range or array to examine.
  • query is text containing instructions such as select or where.
  • headers is an optional number specifying how many header rows are at the top of the data.

QUERY returns an array of results that spills into neighboring cells. Leave those cells empty so the output can expand. If existing content blocks the result, Sheets may show an error instead of replacing that content.

Choosing the data range and header count

Choose a range that includes the rows and columns the query needs. For example, if a table has headings in row 1 and records below, A1:D includes all four columns from the header onward. A bounded range such as A1:D100 can be easier to inspect, while an open-ended range can include future rows without changing the formula. Avoid including unrelated notes or totals in the same range: they may be treated as ordinary records.

The third argument tells QUERY how many leading rows are headers. Use 1 for one header row, 2 for two, or 0 if there are none. If you omit it, Sheets attempts to infer the number. Inference can be wrong, especially when the top of the range is unusual, so specifying the count is generally clearer. The header argument describes rows in the input range, not how many heading rows you want in the output.

For a regular range, refer to columns by their sheet letters, such as A, B, and C. If the data is supplied as an array rather than a normal sheet range, QUERY may refer to its columns as Col1, Col2, and so on. Keep the range and its column references aligned: a query that asks for column D cannot work if the selected data contains only columns A through C.

How the query language works

The query argument is a text string, usually written in double quotes in the formula. Inside it, clauses specify which records and fields to return. A common order is select, where, group by, order by, and limit, although you only need the clauses relevant to your task.

  • select chooses columns, for example select A, C. Use select * to request all columns.
  • where filters records, for example where C >= 10 or where A = 'West'.
  • order by sorts the returned rows. Add asc for ascending or desc for descending order.
  • limit restricts the number of returned rows, while offset skips rows before returning results.
  • group by combines records that share a value, typically so an aggregate such as sum(C) can calculate a total for each group.

Text values in the query language are enclosed in single quotes, while the whole query is usually enclosed in double quotes as a formula string. Comparisons can use operators such as =, <>, >, >=, <, and <=. You can combine conditions with and or or. For a date condition, the query language uses a date literal such as date '2025-01-01', not a date written as an ordinary quoted text value.

Examples: filtering, sorting, and summarizing

Suppose A1:D contains a header row with Region, Rep, Units, and Date, followed by sales records. With one header row, this formula returns the region, representative, and units for West records with at least 10 units, sorted from largest unit count to smallest:

=QUERY(A1:D, "select A, B, C where A = 'West' and C >= 10 order by C desc", 1)

To total units by region, use a grouping and an aggregate. With the same A1:D layout and one header row, this formula returns one row per region and its summed units:

=QUERY(A1:D, "select A, sum(C) where A is not null group by A label sum(C) 'Total units'", 1)

The label clause changes the displayed heading for the calculated column; it does not change the source data. The where A is not null condition helps keep blank region rows out of the summary. For date filtering, if dates are in column D, a query condition can look like where D >= date '2025-01-01'. The dates in the sheet should be real date values, not text that merely looks like a date.

You can also make a criterion depend on a cell. If F1 contains the text West and A1:D has the same layout, this formula inserts that value into the query string:

=QUERY(A1:D, "select A, B, C where A = '"&F1&"'", 1)

This is handy for a dropdown-driven report. However, if the inserted value contains an apostrophe or other special character, the assembled query may need additional handling. For simple dashboards, a fixed criterion is less error-prone; for flexible ones, test the formula with the actual values users may enter.

Data types, headings, and output behavior

QUERY interprets columns as containing values of particular types, such as text, numbers, booleans, or dates. If one column mixes types, the majority type determines how QUERY treats the column, and minority-type values may be treated as null. For instance, numbers stored as text may not behave like numeric values in a comparison or sum. Standardize the source column when possible: use consistent dates, numbers, and text rather than mixing formats.

Column headings in the output are not always identical to source headings. When you select a source column, Sheets may use its header; expressions such as sum(C) can produce an automatically generated heading. Use label to give calculated results a useful name. If you select a column that contains blanks, those blanks can also affect what appears in the output, so consider filtering them out where appropriate.

The result is a formula-generated array, not a new editable copy of the records. You can format the output cells, but you cannot edit an individual returned value as if it were an independent source record; change the original data or the query instead. QUERY does not modify, delete, or rearrange the underlying table.

Common errors and how to fix them

A QUERY error often comes from a mismatch between the range and the query, a misspelled clause, or a value written in the wrong type or quoting style. Check these points first:

  • Wrong column reference: Confirm that each referenced column exists in the selected range. If the input is an array, check whether you need Col1 notation.
  • Incorrect header count: Set the third argument to the actual number of header rows, rather than relying on inference.
  • String quoting problem: Use single quotes around text values in the query, and make sure the outer formula string is properly closed.
  • Unexpected missing values: Look for mixed data types, numbers stored as text, blank cells, or dates stored as text.
  • Blocked output: Clear cells in the area where the results need to spill.
  • Unexpected order: Add an explicit order by clause. Do not assume the output will retain a particular order unless the query requests it.

Also check the clause order and spelling. QUERY is not forgiving of SQL syntax that its language does not support, and copying a query from a database tutorial may not work unchanged. If the formula parses but returns no rows, temporarily remove conditions one at a time to identify which condition excludes the records.

QUERY compared with filters and other tools

QUERY is a strong choice when you want a compact, repeatable report that selects columns, applies several conditions, sorts, and groups data in one formula. A standard filter is more interactive: it lets a person inspect or hide rows directly in the source view. Filter views are useful when different people need different saved views without building separate result tables.

For a simple condition, functions such as FILTER may be easier to read because the filtering condition is expressed as a spreadsheet range condition. QUERY can be more convenient when the same result also needs selected columns, sorting, aggregation, or a row limit. Pivot tables are another good option for interactive summaries; they provide a point-and-click way to group and aggregate data rather than encoding the setup in a query string.

Google Sheets QUERY is not the same feature as Microsoft Excel’s Power Query. Excel does not provide this Google Sheets QUERY function. In Excel, formulas such as FILTER or tools such as Power Query can address related tasks, but their syntax and behavior differ. If a workbook must work in both products, plan for separate implementations rather than assuming a QUERY formula will transfer to Excel.

Frequently Asked Questions

Is QUERY in Google Sheets the same as SQL?

No. It uses a SQL-like query language, but it is a Google Sheets feature with a defined set of clauses and functions. SQL statements, database features, and syntax cannot all be used directly in QUERY.

Why does QUERY return blank or missing values?

Check whether the rows meet every where condition, whether the referenced column is correct, and whether its values have consistent types. In a mixed-type column, values that do not match the majority type may be treated as null.

How do I return all columns with QUERY?

Use select * in the query string, for example =QUERY(A1:D, "select *", 1) for a range with one header row. This returns all columns included in the specified data range.

Can QUERY use a cell as a filter?

Yes. Build the query text by joining a cell reference into the string, with the cell value placed between the single quotes required for text criteria. Test values containing apostrophes or unusual characters, because they can affect the query text.

Does QUERY change the original spreadsheet data?

No. It returns a calculated result in the cells containing the formula and its spill area. The source range remains unchanged; edit the source or formula if you want different results.

Why do I get a parse error?

Look for a typo, a missing quote, an unsupported clause, or a clause in the wrong order. Also verify that text values use single quotes inside the query string and that the selected columns belong to the data range.

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.