Google Sheets QUERY Function: A Beginner's Guide with Examples

Google Sheets QUERY function beginner's guide cover
Image: Spreadsheet Tutorials & Productivity (original graphic)

The QUERY function in Google Sheets lets you filter, sort, group, and total a table with a single formula. Instead of chaining FILTER, SORT, and SUMIFS together, you write a short instruction in a SQL-like language and Sheets returns the result as a new table that updates whenever the source data changes.

This guide walks through the syntax and six practical examples on one small sales table, so you can copy each formula and see exactly what it returns.

Quick answer: Use =QUERY(A1:E13, "select B, sum(E) group by B", 1). The first argument is your data, the second is the query text (columns are referred to by their letters), and the third is the number of header rows.

The sample data

All examples below use this sales log in A1:E13. Row 1 holds the headers.

DateRegionRepProductRevenue
10/2/2026EastMayaLaptop$1,200
10/3/2026WestJonahMonitor$340
10/5/2026NorthPriyaLaptop$1,150
10/8/2026EastMayaKeyboard$85
10/9/2026WestLeoLaptop$1,300
10/12/2026NorthPriyaMonitor$360
10/15/2026EastSamMonitor$330
10/17/2026WestJonahKeyboard$90
10/20/2026NorthAnaLaptop$1,250
10/23/2026EastSamLaptop$1,180
10/27/2026WestLeoMonitor$350
10/30/2026NorthAnaKeyboard$95
Google Sheets QUERY formula grouping revenue by region, sorted from highest to lowest total
A single QUERY formula in G1 builds a revenue summary by region. Image: Spreadsheet Tutorials & Productivity (original screenshot)

QUERY syntax

=QUERY(data, query, [headers])
  • data – the range to search, such as A1:E13. Include the header row.
  • query – the instruction, written as text inside double quotes.
  • headers (optional) – how many rows at the top are headers. If you leave it out, Sheets guesses, so it is safer to type 1.

Inside the query, columns are referred to by their letters (A, B, C...), not by their header names. The clauses you will use most are select, where, group by, order by, limit, and label, and they must appear in that order.

Example 1: Filter rows with WHERE

Return the date, rep, and revenue for every West region sale:

=QUERY(A1:E13, "select A, C, E where B = 'West'", 1)

Result: four rows – Jonah $340 (10/3), Leo $1,300 (10/9), Jonah $90 (10/17), and Leo $350 (10/27), plus the header row. Note the single quotes around 'West': the whole query is already inside double quotes, so text values inside it use single quotes.

Example 2: Sort and keep the top results

Show the three biggest sales over $1,000:

=QUERY(A1:E13, "select C, D, E where E > 1000 order by E desc limit 3", 1)

Five sales are above $1,000. After sorting from highest to lowest and keeping three, the result is Leo (Laptop, $1,300), Ana (Laptop, $1,250), and Maya (Laptop, $1,200). Numbers are written without quotes.

Example 3: Filter by date

Dates need the date keyword followed by the date in yyyy-mm-dd format:

=QUERY(A1:E13, "select A, B, E where A >= date '2026-10-15'", 1)

This returns the six sales from October 15 onward. Typing A >= '10/15/2026' instead does not work, because that compares a date column to a piece of text.

Example 4: Group and total (the screenshot above)

=QUERY(A1:E13, "select B, sum(E) group by B order by sum(E) desc label sum(E) 'Total'", 1)

Result:

RegionTotal
North$2,855
East$2,795
West$2,080

Every column in select that is not inside an aggregate function (here, B) must also appear in group by. Without the label clause, the header would read “sum Revenue”. Other aggregates work the same way: count(), avg(), min(), and max().

Example 5: Count items per group

=QUERY(A1:E13, "select D, count(E) group by D label count(E) 'Orders'", 1)

This counts orders per product: 5 laptops, 4 monitors, and 3 keyboards. Add order by count(E) desc before the label clause if you want the most common product first.

Example 6: Use a cell value in the query

To let users pick the region from a cell (say, K1 contains West), build the query text with &:

=QUERY(A1:E13, "select A, C, E where B = '" & K1 & "'", 1)

Look closely at the quotes: a single quote, then the closing double quote, then the cell reference, then a double quote followed by a single quote. With West in K1, this returns the same four rows as Example 1. For a number cell, leave out the single quotes: "where E > " & K2.

How it works

QUERY treats your range as a small database table. Each column letter is a field, and the query text tells Sheets which fields to return (select), which rows to keep (where), how to combine rows (group by with an aggregate), and how to arrange the output (order by, limit, label). The result spills into the cells below and to the right of the formula, so leave that area empty.

The totals above match what you would get from SUMIFS for each region, but QUERY builds the list of regions, the totals, and the sort order in one step.

Common mistakes

  • Using header names instead of letters. select Revenue fails; use select E.
  • Double quotes around text values. Write where B = 'West', not where B = "West".
  • Clauses in the wrong order. order by must come after group by, and label comes after limit.
  • Selecting a non-grouped column. If you group by B, you cannot also select C unless it is inside an aggregate such as count(C).
  • Mixed data types in one column. QUERY decides each column's type from the majority of its values. If a Revenue column holds mostly numbers and a few text entries, those text entries are treated as empty, so totals can come out lower than expected.
  • Not leaving room for the result. If cells in the output area already contain data, the formula returns a #REF! error instead of overwriting them.

FAQ

Does Excel have a QUERY function?

No. QUERY is a Google Sheets function. In Excel, you can get similar results with FILTER and SORT (available in Excel 365 and Excel 2021), with SUMIFS for totals, or with a PivotTable.

When should I use Col1 instead of A?

Use Col1, Col2, and so on when the data is not a plain range, for example an array in curly braces such as {A1:E13} or the output of IMPORTRANGE. For a normal range, use column letters.

Is the query text case-sensitive?

The keywords are not: select and SELECT both work. Text comparisons are, so 'west' does not match West.

How do I return every column?

Use an asterisk: "select * where B = 'East'". You can also leave out select entirely and start with where.

Summary

QUERY takes a range, a short instruction, and a header count, and returns a filtered, sorted, or grouped table that stays in sync with your data. Start with select and where, add group by with sum() or count() for summaries, and remember the three rules that cause most errors: column letters, single quotes for text, and the date 'yyyy-mm-dd' format for dates.

For more tutorials, browse the Google Sheets articles, or see Math & Conditional Sums to learn how SUMIFS gets the same totals.

Related Tutorials

Comments