Database

Google Sheets QUERY Function — Syntax, Examples & Tips

Learn how to use the QUERY function in Google Sheets with SQL-like syntax. Includes SELECT, WHERE, GROUP BY, ORDER BY, and LABEL examples.

Syntax
=QUERY(data, query, [headers])

What QUERY Does

QUERY lets you run SQL-like queries on your spreadsheet data. You can filter rows, select specific columns, group and aggregate values, sort, and even pivot — all in a single formula. It is arguably the most powerful function in Google Sheets for data analysis.

Syntax

=QUERY(data, query, [headers])
ParameterDescription
dataThe range of cells to query.
queryA string written in Google Visualization API Query Language (similar to SQL).
headersThe number of header rows in the data. Usually 1. If omitted, Google Sheets guesses.

Basic Examples

Example 1: Select Specific Columns

From a sales table (A1:E), pull only the name and revenue columns:

=QUERY(A1:E, "SELECT B, D", 1)

Result: A two-column table with Name (col B) and Revenue (col D).

Example 2: Filter Rows with WHERE

Show only orders over $500:

=QUERY(A1:E, "SELECT * WHERE D > 500", 1)

Example 3: Sort Results

List all customers sorted by signup date (column C), newest first:

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

Advanced Examples

GROUP BY with Aggregation

Sum total revenue by region:

=QUERY(A1:D, "SELECT A, SUM(D) WHERE A IS NOT NULL GROUP BY A LABEL SUM(D) 'Total Revenue'", 1)
RegionTotal Revenue
East45200
West38700
North29100

The LABEL clause renames the aggregated column in the output.

Multiple Conditions + Formatting

Find all deals closed in Q1 2026 with a value above $10,000:

=QUERY(A1:F, "SELECT B, C, E WHERE E > 10000 AND C >= date '2026-01-01' AND C <= date '2026-03-31' ORDER BY E DESC", 1)

Dates in QUERY must use the date 'YYYY-MM-DD' format — this is a common source of confusion.

Common Mistakes

  • Column letters vs. data columns. In QUERY, columns are referenced by their position in the data range, not the sheet. If your data starts at column B, the first column in your query is still Col1 (or B if you use the range B:F). This mismatch often causes wrong results.
  • Date formatting. Dates must be written as date '2026-01-15' inside the query string, not as regular text or cell references. To use a cell reference for a date, concatenate it: "WHERE C > date '"&TEXT(G1,"yyyy-mm-dd")&"'".
  • Mixed data types in a column. If a column has both text and numbers, QUERY returns results for whichever type is more common and ignores the rest — without warning. Clean your data first.

SheetAI Tip

Don't want to memorize this syntax? With SheetAI, just type what you need in plain English — like "show total revenue by region, sorted highest to lowest" — and the formula is generated for you. Install SheetAI to try it free.

Skip the formula. Describe what you need.

SheetAI lets you type what you want in plain English and generates the perfect formula — no syntax to memorize.

Try SheetAI Free