The QUERY function in Google Sheets is a powerful tool that lets you run SQL-like queries on your data. The clause is the part of the QUERY function that specifies what to do with the data—such as SELECT, WHERE, ORDER BY, GROUP BY, PIVOT, LIMIT, OFFSET, and LABEL—and it must be written in a specific order. This article explains each clause, how to combine them, and common errors you might encounter.
What Is the QUERY Function and Its Clauses?
The QUERY function has the syntax =QUERY(data, query, [headers]). The query argument is a text string that contains the clauses. The clauses are keywords that tell Google Sheets what to do with the data. The order of clauses is strict: SELECT, WHERE, GROUP BY, PIVOT, ORDER BY, LIMIT, OFFSET, LABEL, FORMAT, and OPTIONS. You can omit any clause, but you cannot reorder them. For example, ORDER BY must come after WHERE, not before.
How the QUERY Clause Works Under the Hood
When you enter a QUERY formula, Google Sheets parses the query string and translates it into a series of operations on your data range. It treats the first row as headers (unless you specify otherwise) and the rest as records. The SELECT clause chooses which columns to return, WHERE filters rows, GROUP BY aggregates rows, and so on. The result is a new table that appears where you placed the formula. The clause syntax is case-insensitive (e.g., select works as well as SELECT), but column references use letters (A, B, C) or column names if you use the Col1 notation.
Writing the SELECT Clause
The SELECT clause is the only mandatory one. It determines which columns to display. You can reference columns by letter (A, B, C) or by using Col1, Col2, etc. For example, =QUERY(A1:C10, "SELECT A, C") returns columns A and C. You can also use aggregation functions like SUM, AVG, COUNT, MAX, MIN in the SELECT clause. For instance, =QUERY(A1:C10, "SELECT A, SUM(B) GROUP BY A") groups by column A and sums column B for each group.
Filtering with WHERE and Sorting with ORDER BY
The WHERE clause filters rows based on conditions. For example, =QUERY(A1:C10, "SELECT * WHERE B > 100") returns all columns where column B is greater than 100. You can use comparison operators (>, <, =, <>, >=, <=) and logical operators (AND, OR, NOT). Text conditions require single quotes, e.g., WHERE A = 'John'. The ORDER BY clause sorts the result. For example, =QUERY(A1:C10, "SELECT A, B ORDER BY B DESC") sorts by column B descending. You can sort by multiple columns: ORDER BY A ASC, B DESC.
Grouping and Pivoting Data
The GROUP BY clause aggregates rows that share a value in a specified column. It is used with aggregation functions in SELECT. For example, =QUERY(A1:C10, "SELECT A, COUNT(B) GROUP BY A") counts the number of rows for each unique value in column A. The PIVOT clause is a special form of grouping that turns unique values in a column into new columns. For example, =QUERY(A1:C10, "SELECT B, SUM(C) PIVOT A") creates a summary table with column B as rows and unique values of column A as columns, with sums of column C in the cells.
Limiting, Offsetting, and Labeling
The LIMIT clause restricts the number of rows returned. For example, =QUERY(A1:C10, "SELECT * LIMIT 5") returns the first 5 rows. The OFFSET clause skips a number of rows before returning results. For example, =QUERY(A1:C10, "SELECT * LIMIT 5 OFFSET 2") skips the first 2 rows and returns the next 5. The LABEL clause renames the column headers in the result. For example, =QUERY(A1:C10, "SELECT A, B LABEL A 'Name', B 'Score'") changes the headers to ‘Name’ and ‘Score’.
Common QUERY Clause Errors and Fixes
One common error is #VALUE! when the query string is malformed. This often happens when you forget to close a quote or use double quotes instead of single quotes for text. Another is #REF! when the data range is invalid or the query references a column that doesn’t exist. Also, remember that the WHERE clause cannot use aggregation functions; you need to use HAVING for that, but Google Sheets QUERY does not support HAVING directly—you can achieve similar results with a subquery or by using FILTER in a separate step. Another common issue is that the ORDER BY clause must come after GROUP BY and PIVOT, and you cannot use WHERE with aggregated values.
Comparing QUERY with Other Google Sheets Functions
QUERY is powerful, but it has a learning curve. For simple filtering, you might use FILTER or SORT instead. For example, =FILTER(A1:C10, B1:B10>100) does the same as =QUERY(A1:C10, "SELECT * WHERE B>100") but is easier to read. For grouping, you could use SUMIF or COUNTIF, but QUERY is more flexible for multiple aggregations. However, QUERY can be slower on large datasets because it processes the entire range. Also, QUERY does not support all SQL features, such as JOIN or UNION; you would need to use other functions or scripts for those.
Frequently Asked Questions
Can I use QUERY with data from another sheet? Yes, you can reference another sheet by using the sheet name in the data range, e.g., =QUERY(Sheet2!A1:C10, "SELECT *").
How do I use a cell value in the query string? You can concatenate the cell reference with the query string using the ampersand (&). For example, =QUERY(A1:C10, "SELECT * WHERE B > "&D1) uses the value in D1 as the threshold.
Why does my QUERY return #N/A when there are no matches? By default, QUERY returns an empty table if no rows match. If you want to show a custom message, you can wrap it in IFERROR.
Can I use QUERY to combine multiple sheets? Not directly. You can use curly braces to combine ranges, e.g., =QUERY({Sheet1!A1:C10; Sheet2!A1:C10}, "SELECT *"), but the columns must match.
Is there a way to use column names instead of letters? Yes, if you use the Col1 notation, but it doesn’t use the header names. To use header names, you would need to restructure your data or use a different function like FILTER.
Does QUERY work in Excel? No, QUERY is a Google Sheets function. Excel has a similar feature called Power Query, but it is not a formula and works differently.
Related
- Shortcut for Deleting in Excel: Top 5 Ways
- Google Sheets Divide Function: Syntax & Examples
- Random Number Generation in C++: A Practical Guide
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Create a Word Template with Fillable FieldsTo create a fillable Word template, add content controls from the Developer tab, set their properties, then save…
- Excel Cell Formatting: How to Test ItIf A2 is formatted as 0.00%, enter =CELL("format",A2)="P2" in another cell to test its number format; Excel returns…
- How to Make a Poster Presentation in PowerPoint: StepsTo make a poster presentation in PowerPoint, create a blank slide, set it to the required printed dimensions,…
- Excel Row Limit: How Many Rows Can a Worksheet Hold?In Microsoft Excel for Microsoft 365, a worksheet can hold a maximum of 1,048,576 rows. That is the…
- How to Close an Excel File With a Keyboard ShortcutIn Excel for Microsoft 365 on Windows, press Ctrl+W to close the active workbook. If you have unsaved…
- How to Select All on a Laptop: 5 Fast WaysThe quickest way to select all on a laptop is to press Ctrl+A (Windows) or Command+A (Mac). This…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.