Skip to main content
SheetCraft
📐 Array Function · advanced

QUERY Function in Google Sheets

Runs a SQL-like query against a range. Google Sheets' most powerful function for filtering, sorting, aggregating, and reshaping data. The single most underused tool in the entire Sheets toolkit.

Syntax

QUERY(data, query, [headers])

Returns: A new range containing the query result.

Excel equivalent: No direct equivalent — Excel users typically use Power Query or Pivot Tables instead. Google Sheets QUERY is a unique strength.

Parameters

NameRequiredDescription
dataRequiredThe range to query.
queryRequiredA SQL-like string. Supports SELECT, WHERE, GROUP BY, ORDER BY, LIMIT, LABEL, and more. Columns referenced by letter (Col A, Col B...) or position (Col1, Col2...) depending on source.
headersOptionalNumber of header rows in data. Default is -1 (auto-detect). Pass 1 if the first row is a header, 0 if not.

Examples

Filter rows by a condition

=QUERY(A1:E1000, "SELECT A, C, E WHERE B > 1000 ORDER BY E DESC", 1)

Returns columns A, C, E from rows where column B exceeds 1000, sorted by E descending. The 1 says "row 1 is a header".

Group by category with totals

=QUERY(A1:D1000, "SELECT A, SUM(D) WHERE C = 'Paid' GROUP BY A LABEL SUM(D) 'Total Paid'", 1)

Sum of column D grouped by column A, only for rows where C is 'Paid'. LABEL renames the output header.

Date filter and limit

=QUERY(Orders!A1:F, "SELECT A, B, F WHERE A >= date '2026-01-01' AND A < date '2026-04-01' ORDER BY A DESC LIMIT 50", 1)

Q1 2026 orders, most recent first, capped at 50 rows. Dates use the special date 'YYYY-MM-DD' syntax inside the query string.

When to use an alternative

  • FILTERYou only need to filter rows, no grouping or aggregation.
  • SUMIFS / COUNTIFSYou only need a single aggregate, not a multi-row result.
  • Pivot TableYou want interactive exploration without writing the query.

Common errors and how to fix them

  • #VALUE! "Unable to parse query string"

    Cause: Syntax error in the query — usually quote mismatch or missing space.

    Fix: Double-check string literals use single quotes inside the query, not double. e.g. WHERE C = 'Paid' not WHERE C = "Paid".

  • #NUM!

    Cause: Mixed types in a column QUERY is trying to filter (e.g., numbers and text in the same column).

    Fix: Coerce types explicitly with TO_TEXT() or TO_NUMBER() inside the query, or clean the source data.

  • Returns header twice

    Cause: headers argument set wrong or LABEL clause defined when not needed.

    Fix: Pass 1 explicitly for headers, and remove LABEL clauses for non-aggregated columns.

Related functions

Frequently Asked Questions

Is QUERY actually SQL?

It uses the Google Visualization API Query Language, which is SQL-like but not full SQL. Supports SELECT, WHERE, GROUP BY, ORDER BY, LIMIT, OFFSET, LABEL, FORMAT, PIVOT, OPTIONS. Doesn't support JOIN, subqueries, or window functions. For those, do multiple steps with helper ranges.

Why use QUERY instead of a Pivot Table?

Pivot Tables are interactive but static — they don't update when source data changes structure, and they live as a separate UI object. QUERY is a formula — it updates live, can be referenced by other formulas, and lives in the spreadsheet's calc graph. Use Pivot Tables for one-off exploration, QUERY for production dashboards.

How do I filter QUERY by a cell value?

Concatenate the cell into the query string: =QUERY(A:E, "SELECT * WHERE B = '" & D1 & "'", 1). Note the embedded single quotes for string values. For numbers, omit the quotes: "...WHERE B = " & D1.

Source: Google Sheets official function reference.