ShareYourGroupShareYourGroupGoogle Sheets

Only in Sheets

QUERY function in Google Sheets

QUERY selects, filters, sorts and summarises a range using a short query written in words, much like SQL.

QUERY is the most powerful function in Google Sheets, and Excel has nothing quite like it. One formula can pick columns, keep only some rows, sort them, and work out totals for each group. It replaces a combination of FILTER, SORT, SUMIF and a pivot table.

Syntax

=QUERY(data, query, [headers])
ArgumentWhat it is
dataThe range to work on, such as A1:F13. Include the heading row.
queryThe instruction, as text in double quotation marks. Columns are named by their letters.
headersHow many rows at the top are headings. Usually 1. If left out, Sheets guesses.

Examples

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

How it works

You hand QUERY a range and an instruction written as text. The instruction uses a small language of its own, the Google Visualization API Query Language, which reads like SQL. Columns are called by their letters: A, B, C.

The result spills into the cells below and to the right of the formula, with the headings on top.

The parts of a query

They must appear in this order. All are optional except that you need at least one.

ClauseDoesExample
selectwhich columns to returnselect B, F
wherewhich rows to keepwhere F > 2000
group byone row per value, with totalsgroup by B
order bysortingorder by F desc
limitonly the first rowslimit 3
labelrename a headinglabel sum(F) 'Total'

select * returns every column.

Conditions in where

Totals: sum, count, avg, max, min

With group by, every column in the select must either be grouped or sit inside one of the totalling functions. select B, sum(F) group by B gives one row per salesperson. The heading of a total column reads like "sum Amount". Use label to give it a better name.

Using a value from a cell

The query is text, so a cell's value has to be joined into it. With a region typed in H1:

=QUERY(A1:F13, "select B, F where C = '"&H1&"'", 1)

Note the single quotation marks either side of the join. For a number, leave them out: "where F > "&H1.

Dates

A date in a query must be written as date '2026-03-01': the word date, then year-month-day in single quotation marks. To use a date from a cell, format it with TEXT: "where A >= date '"&TEXT(H1,"yyyy-mm-dd")&"'".

Each column must hold one kind of data

QUERY decides the type of each column from most of its values, and treats the minority as empty. A column that is mostly numbers with a few text entries loses the text entries. This is the usual reason for values going missing from a QUERY result. Keep each column to one type.

Common mistakes

In Excel

Excel has no QUERY function. The nearest tools there are FILTER and SORT for rows, PivotTables for totals, and Power Query for bigger jobs. A sheet that uses QUERY shows only its last values after being saved as an Excel file.

Related functions

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.