ShareYourGroupShareYourGroupGoogle Sheets

Guides

The QUERY language, clause by clause

The QUERY function takes an instruction written in a small language of its own. This guide goes through that language one clause at a time, using one sample table. If you have used SQL, most of it will look familiar. If you have not, each clause is a plain English word that does what it says.

Sample sheet: Twelve sales made by four salespeople in three regions.
ABCDEF
1DateSalespersonRegionProductUnitsAmount
205-Jan-2026AshaNorthNotebook201200
312-Jan-2026RaviSouthPen50500
420-Jan-2026MeenaNorthBag43000
502-Feb-2026AshaEastPen30300
614-Feb-2026KabirSouthNotebook15900
728-Feb-2026RaviNorthBag21500
803-Mar-2026MeenaEastNotebook402400
915-Mar-2026AshaNorthBag32250
1022-Mar-2026KabirSouthPen60600
1101-Apr-2026RaviEastNotebook251500
1218-Apr-2026MeenaNorthPen80800
1306-May-2026AshaSouthBag53750

The data is A1:F13. In a query, columns are named by their letters: A is the date, B the salesperson, C the region, D the product, E the units and F the amount. The final argument, 1, tells QUERY that the first row is a heading.

The order of the clauses

Clauses must be written in this order. Leave out any you do not need.

select · where · group by · pivot · order by · limit · offset · label · format

select: which columns

select * returns all columns and is the same as leaving select out. A column can also be calculated, as in select B, F*2. Sheets then writes a heading of its own for it, which you will want to replace with label.

where: which rows

Comparison words for text: contains, starts with, ends with, matches (a regular expression for the whole value) and like (with % for any run of characters). Use is null and is not null for empty cells, and not to reverse a condition. Text comparisons are case sensitive. Wrap the column in lower() to ignore case: where lower(C) = 'east'.

group by: totals for each value

The totalling functions are sum, count, avg, max and min. Every column in the select must be either in the group by or inside one of them.

pivot: values across the top

pivot turns the values of a column into column headings. This makes a cross table.

order by, limit and offset

asc is the default. List several columns for several sort levels: order by C, F desc.

label and format

format sets how a column is shown: format F '#,##0.00', A 'dd-mmm-yyyy'. To drop the heading row from a result altogether, set every label to empty or wrap the query in QUERY(..., 0) over data without headings.

Dates: year, month and friends

Scalar functions work on date columns: year(A), month(A), day(A), dayOfWeek(A), quarter(A). One trap: month() counts from 0, so January is 0 and December is 11. Add 1 to get the usual number: select month(A)+1, sum(F) group by month(A)+1.

Using cell values in a query

The query is text, so join cell values into it with &.

Build long queries in a spare cell first, as ordinary text, and look at the result. A missing space or quotation mark is easy to see there.

Data without column letters

When the data comes from IMPORTRANGE, from curly brackets, or from another function, it has no letters. Name the columns Col1, Col2, Col3, counting from the left: =QUERY(IMPORTRANGE(url,"Sales!A:F"), "select Col2, sum(Col6) group by Col2", 1).

Curly brackets stack ranges for QUERY: {Jan!A2:F; Feb!A2:F} puts two tabs one above the other.

Things that go wrong

Message or symptomLikely cause
#VALUE!, unable to parse query stringa spelling slip, a clause out of order, or double quotation marks inside the query
#VALUE!, no columna letter outside the data range, or letters used where Col1 is needed
values missing from a columnthe column mixes numbers and text; the minority type is treated as empty
headings joined into one row of textthe headers argument was left out and guessed wrongly; give it as 1
nothing returneda text condition with the wrong case, or stray spaces in the data

The results on this page were worked out by us from the sample sheet; they were not produced by a script running Google Sheets. If Sheets shows you something different, please tell us.

This is an independent help site. It is not connected with or approved by Google. Google Sheets is a trademark of Google LLC, named here only to say which program a formula is for. The people and figures in the sample sheets are made up.