Category: FUNCTION | Updated: 2026-11-20

How to Use the QUERY Function in Google Sheets | Extract and Aggregate Data with SQL-Like Queries

How to Use the QUERY Function

The QUERY function is one of Google Sheets’ most powerful functions, allowing you to filter, sort, group, and aggregate data in a single formula using the Google Visualization API Query Language (SQL-like statements).

Syntax

=QUERY(data, query, [headers])
  • data: The cell range of the table you want to query and aggregate (required).
  • query: A query string enclosed in double quotes " (required). The main clauses are written in the following order:
    • SELECT: Columns to display or aggregation functions (SUM, COUNT, AVG, etc.)
    • WHERE: Filtering conditions
    • GROUP BY: Columns to group by
    • ORDER BY: Columns to sort by (ASC for ascending / DESC for descending)
  • headers: The number of header rows at the top of data (typically 1, or 0 if there are no headers) (optional).

Table Example

Here is an example of calculating total sales (SUM(C)) per sales representative (Column A) from the "Sales Data" in Columns A–C, including only rows where Status is "Confirmed".

Row Column A (Rep) Column B (Status) Column C (Sales) Column D Column E (Rep) Column F (Total Sales)
1 Rep Status Sales Rep sum Sales
2 Sato Confirmed 50,000 =QUERY(A1:C5, "SELECT A, SUM(C) WHERE B = 'Confirmed' GROUP BY A", 1) (Auto-spilled)
3 Suzuki Confirmed 80,000 Suzuki 80,000
4 Sato Confirmed 30,000
5 Tanaka Pipeline 40,000
  • Formula in cell E2: =QUERY(A1:C5, "SELECT A, SUM(C) WHERE B = 'Confirmed' GROUP BY A", 1)
  • Result: Confirmed sales totals are automatically aggregated and displayed for Sato (80,000) and Suzuki (80,000).

Common Errors and Notes

  • Query syntax errors (#VALUE! error):
    • Column letters (A, B, etc.) must always be uppercase (lowercase a causes an error).
    • When specifying text strings inside the query, use single quotes ' (e.g., B = 'Confirmed'), since the outer query is wrapped in double quotes ".
    • Writing clauses out of order (SELECT → WHERE → GROUP BY → ORDER BY) causes an error.
  • Missing values when numbers and text are mixed in the same column: QUERY automatically infers a single data type per column; if a column contains both numbers and text, the minority data type is ignored and treated as blank. Keep data types consistent within each column.

Frequently Asked Questions

Q. What are the rules for referencing columns in a QUERY formula?

A. When data is a cell range (such as A1:D50), reference columns using uppercase letters like "A, B, C". When data is the array output of another function, reference columns using "Col1, Col2", etc.

Q. How do I filter by date in a QUERY function?

A. Prefix the date literal with "date" and enclose the YYYY-MM-DD format in single quotes, such as "WHERE A >= date '2026-11-01'".