Skip to content
getgriddy.ai/blog/google-sheets-query-function
Google Sheets

Google Sheets QUERY Function: SQL for Your Spreadsheet

The QUERY function lets you filter, sort, group, and aggregate data using SQL-like syntax — without pivot tables or complex formulas. Here's how it works with practical examples.

·6 min read

QUERY is one of the most powerful functions in Google Sheets. It applies a SQL-like query language to your data — letting you filter, sort, group, and aggregate in ways that would require a combination of FILTER, SORT, and SUMIFS in Excel.

The syntax

fx
=QUERY(data, query, [headers])
  • data — the range containing your data (include headers)
  • query — a SQL-like string in quotes
  • [headers] — number of header rows in your data (usually 1)

SELECT — choose which columns to show

Show only columns A, B, and D:

fx
=QUERY(A1:E100, "SELECT A, B, D", 1)

Show all columns:

fx
=QUERY(A1:E100, "SELECT *", 1)

NOTE

QUERY references columns by letter (A, B, C...) based on their position in your data range, not their actual sheet column. If your data starts at column C on the sheet, it's still called "A" inside the query.

WHERE — filter rows

Filter rows where column B equals "West":

fx

TIP

Text values in QUERY WHERE clauses use single quotes: WHERE B = 'West'. This is different from the double quotes used in regular Sheets formulas.

Filter with a number comparison:

fx
=QUERY(A1:D100, "SELECT A, C WHERE C > 1000", 1)

Combine conditions with AND or OR:

fx

ORDER BY — sort results

Sort by column C descending:

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

GROUP BY and aggregate functions

QUERY supports SUM, COUNT, AVG, MAX, MIN with GROUP BY for aggregation:

Total revenue by region (column A = Region, column C = Revenue):

fx
=QUERY(A1:C100, "SELECT A, SUM(C) GROUP BY A ORDER BY SUM(C) DESC LABEL SUM(C) \"Total Revenue\"", 1)

The LABEL clause renames the output column header.

Using cell references in the query

You cannot put cell references directly inside the query string, but you can concatenate them:

fx

Where E2 contains the region you want to filter by. Change E2 and the results update automatically.

TIP

For dates in WHERE clauses, use date 'YYYY-MM-DD' format: WHERE D > date '2026-01-01'

QUERY vs FILTER

FILTERQUERY
Simple row filtering✓ Easier✓ Works
Column selection✗ Returns all✓ SELECT specific
Aggregation (SUM, COUNT)✗ Need separate formulas✓ Built in
Sorting output✗ Need SORT wrapper✓ ORDER BY
Learning curveLowMedium

The Griddy way

QUERY syntax trips up almost everyone — the quote escaping, the column letter references, and the date format syntax all have subtle rules. Just describe what you want:

"From the transactions sheet, show me all rows from Q1 where the category is either Marketing or Sales, sorted by amount descending, with a total row at the bottom"

Griddy writes the QUERY with correct syntax and handles the concatenated cell references if you want dynamic filtering.

Skip the manual work

Describe it. Griddy does it.

Instead of writing this formula yourself, just tell Griddy what you need in plain English. Works in Excel and Google Sheets.