Someone asked a human this. So we asked the AI.

How to Use the QUERY Function in Google Sheets

Someone wanted a spreadsheet to answer questions in plain-ish English, and yes, there is a function for that.

The answer

QUERY runs a short SQL-like command on a range and returns only the rows and columns you ask for. The syntax is =QUERY(data, "query", headers).

Clauses and their order

Clauses must appear in this order, and you can leave out any you don't need: select, where, group by, pivot, order by, limit, offset, label, format, options.

Writing conditions

Column letters vs Col1, Col2

With a normal range, use the sheet's own column letters, not letters counted from the start of the range. For C1:E10, the columns are C, D and E, so "select C" works and "select A" gives a NO_COLUMN error.

When the data is an IMPORTRANGE result or an array like {A1:B10; D1:E10}, the sheet letters no longer apply. Use Col1, Col2, Col3, counted from the first column of that data.

Another sheet or spreadsheet

Using a cell value in the query

Join it with &. Numbers: "select A where C > " & E1. Text needs quotes inside the string: "select A where B = '" & E1 & "'".

Example

Data in A1:C6:

Name  Region  Sales
Ann   East    500
Ben   West    300
Cara  East    700
Dan   West    200
Eve   East    100

East rows with sales over 200, biggest first:

=QUERY(A1:C6, "select A, C where B = 'East' and C > 200 order by C desc", 1)

Result:

Name  Sales
Cara  700
Ann   500

Total per region, largest first:

=QUERY(A1:C6, "select B, sum(C) group by B order by sum(C) desc", 1)

Result: East 1300, West 500, with headings Region and sum Sales.

Pivot version:

=QUERY(A1:C6, "select sum(C) pivot B", 1)

Result: one row with East 1300 and West 500 as column headings.

Where people go wrong

The prompt you could have used

I have sales data in Google Sheets A1:C100 (Name, Region, Sales). Write a QUERY formula that returns Name and Sales for the East region where Sales is over 200, sorted by Sales descending, and explain each part.

Watch the AI answer it →

Updated 16 November 2026. Written by an AI and fact-checked by a second one before it went up; anything they weren't sure of was left out.

Someone asked you something like this? Send them a link →

More questions nobody needed to ask

More Google Sheets questions · All questions · Just Fucking Prompt It