How to use QUERY in Google Sheets: syntax and examples

Last update: 30/10/2026

  • QUERY allows you to filter, sort, and summarize data without modifying the original range.
  • Headers and data types influence the result, especially when values ​​are missing.
  • Combined arrays use positional identifiers, which must be reviewed if the structure changes.
  • It is advisable to diagnose errors before hiding them, so as not to confuse an incorrect formula with a query that has no matches.
How to use QUERY in Google Sheets

La Google Sheets QUERY function It allows you to select columns, filter records, sort results, and calculate summaries using a formula. This is useful when you need to generate a report that relies on data from another table without modifying the original content.

Your query uses the Google Visualization language, with a syntax similar to SQL. To get started, you don't need to know all of that language: you can build a simple query and add conditions or groupings as needed.

QUERY syntax in Google Sheets and first example

QUERY syntax in Google Sheets

The function structure has three arguments:

=QUERY(datos; consulta; encabezados)
  • data: the range or matrix you want to query.
  • consultation: the instructions in quotation marks or a reference to a cell that contains them.
  • headings: The number of initial rows in the range that are headings. Use 1 if there is a heading row and 0 if there isn't. If you omit it or enter -1, Sheets tries to guess.

In the following examples we will use a table with this structure:

A: Date B: Client C: State D: Amount E: Channel
01/10/2026 Ann Filled 100 Web
02/10/2026 Luis Earring 80 Store
03/10/2026 Ann Filled 50 Store
04/10/2026 Marten Filled 200 Web
05/10/2026 Luis Canceled 40 Web

The headers are in row 1 and the five records occupy rows 2 to 6. To return the entire table, write the formula in an empty area, for example G1:

=QUERY(A1:E6;"select *";1)

The examples use semicolon between argumentsIf your file uses commas, adjust those separators. The commas in the query text, as in select B, DThey must be preserved.

Exclusive content - Click Here  How to add fonts

Select columns, filter and sort results

Magnifying glass over graphics and documents next to a laptop computer.

The clause select This allows you to choose the columns. To display only the customer and the amount:

=QUERY(A1:E6;"select B, D";1)

With where You can limit records to completed orders:

=QUERY(A1:E6;"select B, D where C = 'Completado'";1)

The example returns both of Ana's operations and Marta's. Adding order by You can sort them by descending amount:

=QUERY(A1:E6;"select B, D where C = 'Completado' order by D desc";1)

You can also combine conditions:

=QUERY(A1:E6;"select B, D where C = 'Completado' and D >= 100 order by D desc";1)

This last query returns Marta with 200 and Ana with 100. Numbers are compared without quotes; text values ​​are written between single quotes within the query.

Group amounts with GROUP BY and change the headers

To obtain the total amount per customer, combine group by with sum():

=QUERY(A1:E6;"select B, sum(D) group by B label sum(D) 'Importe total'";1)

The result shows 150 for Ana, 120 for Luis, and 200 for Marta. In this case, all states are included because we haven't added a filter.

If you only want to add completed orders:

=QUERY(A1:E6;"select B, sum(D) where C = 'Completado' group by B order by sum(D) desc label sum(D) 'Ventas completadas'";1)

You can use other aggregations, such as avg(), count(), min() y max()When you group, the selected columns that are not aggregated must be part of group by.

Remember that count(D) counts non-null values ​​of DIt does not sum the amounts. Choose an appropriate column if you need to count transactions.

Use PIVOT, LIMIT, and OFFSET

The clause pivot Transforms the values ​​of a field into columns. For example, you can distribute each customer's completed sales between Web and Store:

=QUERY(A1:E6;"select B, sum(D) where C = 'Completado' group by B pivot E";1)

To get only the three highest amounts, use limit after ordering:

=QUERY(A1:E6;"select B, D order by D desc limit 3";1)

With offset You can omit rows from the result. This query skips the first two records in the sort and returns the next two:

=QUERY(A1:E6;"select B, D order by D desc limit 2 offset 2";1)

The clauses must respect their order: select, where, group by, pivot, order by, limit, offset, label y format.

Filter dates and work with text

If column A contains dates recognized by Sheets, you can select the October transactions as follows:

=QUERY(A1:E6;"select * where A >= date '2026-10-01' and A < date '2026-11-01'";1)

The date field within the query uses the format year-month-dayEven if the cells display day/month/year. If the source contains text that looks like a date, you'll need to correct it first.

Exclusive content - Click Here  What is Bubok and how does it work?

To work with parts of dates there are year(), month() y day()Please note that month() starts at 0 for January and ends at 11 for DecemberWhen grouping several years, also use the year to avoid mixing equivalent months.

In text filters you can use contains, starts with, ends with o matchesComparisons are case-sensitive; you can normalize the text with lower() o upper().

Combine data from multiple sheets using Col1 and Col2

Two professionals reviewing tables and graphs on their computers.

To stack records from two tabs of the same file, you can construct an array with curly braces. Let's assume both have the same five columns and one row of headers:

=QUERY({Ventas1!A1:E100;Ventas2!A2:E100};"select Col2, Col4 where Col2 is not null";1)

The first reference includes the headers; the second starts in row 2 to avoid repetition. The ranges must have the same number of columns and a compatible structure.

In this matrix, Col1 represents the first column and Col2 the secondThese are positions within the queried set, not field names. If you rearrange the column contents, check the query to ensure it continues to select the correct information.

Exclusive content - Click Here  How to Set a Video as Your Wallpaper

The semicolon within the curly braces stacks the ranges vertically. This operation does not relate records by an identifier and is not equivalent to performing a JOIN between tables.

Common QUERY errors and how to find them

  • Missing values: A column with mixed types may lose the values ​​of the minority type, which QUERY interprets as null.
  • Error parsing the query: Check straight quotes, identifiers, functions, and clause order.
  • No new records appear: Verify that they are included in the queried range. A reference up to row 100 does not include row 101.
  • The matrix cannot be expanded: Leave the cells where the result should appear free and check if there are any merged cells.
  • A row of data is interpreted as a title: check the header argument.

Do not wrap the formula in IF.ERROR or IFERROR While you're looking for the fault: you could be hiding a syntax problem and mistaking it for a lack of matches.

Once the query is working, if you want to specifically address an error #N/A, you can use SI.ND:

=SI.ND(QUERY(A1:E6;"select B, D where C = 'Devuelto'";1);"Sin coincidencias")

If the file keeps the function names in English, use IFNAThis function handles errors of that type; it does not, by itself, demonstrate what their cause was.

To work safely, start with a simple selection and add each condition separately. QUERY recalculates its result when the data it depends on changes, but It does not modify the origin or automatically expand a closed range..