What is the Correct Clause Order in Google Sheets Query?

Published on

This tutorial is about language clauses in the Google Sheets Query function.

They are Select, Where, Group By, Pivot, Order By, Limit, Offset, Label, and Format. I have presented these clauses as per their correct clause Order in Google Sheets Query.

I have excluded the clauses “From,” “Skipping,” and “Options.” These clauses are either eliminated or not in regular use.

Please note that I am not talking about Google Sheets Query order of execution but their order (precedence of occurrence) in the formula.

What is the correct clause order in Google Sheets Query? are they as per the above-given order?

Yes! The above is the correct order to use language clauses in Google Sheets Query.

But as you may know, we don’t require to use all of them in a formula.

Let’s learn how to correctly use the language clauses (keywords or keyword combinations) as per their order in a Google Sheets Query formula.

How to Use Correct Clause Order in a Google Sheets Query Formula

Query Syntax: QUERY(data, query, [headers])

Here is our sample data (Statement of Accounts aka SOA).

Data to Test Correct Clause Order in Google Sheets Query

We can manipulate this data using Query formulas that involve different keywords.

I am purely concentrating on how to write a Query formula with the correct clause or keyword order.

The first clause to use in Query is SELECT.

1. SELECT: Keyword for Selecting Columns

The following formula with Query SELECT clause returns the ‘Customer’ and ‘Balance’ columns from the above data (SOA).

=query(A1:F,"Select A,F")

As mentioned above, SELECT is the first clause in Query in order. Then the WHERE clause follows.

2. WHERE: Keyword to Specify Conditions to Filter

The WHERE clause comes second in the order. The SELECT clause is optional with the WHERE clause.

Problem: Return the rows in columns A and F where the customer name in column A is “Customer 1.”

=query(A1:F,"Select A,F Where A='Customer 1'")

If you do not want to select any particular columns in the formula, you can omit the SELECT clause use.

See the following example, which filters ‘Customer 1’ as above but returns all the columns in the range.

=query(A1:F,"Where A='Customer 1'")

If you aren’t familiar with values used for comparisons or assignments, please check this: Examples of the Use of Literals in Query in Google Sheets.

3. GROUP BY: Keyword Combination for Grouping Rows

GROUP BY is the third clause to use in the correct clause order in Google Sheets Query.

The WHERE clause is optional with the GROUP BY clause.

Use only the columns specified in the SELECT clause in the GROUP BY clause.

=query(A1:F,"Select A, Sum(F) Where A='Customer 1' group by A")

The above formula would return the below result.

Customersum Balance
Customer 112964

As far as I know, the SELECT clause should be present when using GROUP BY in the formula.

But you have the freedom not to specify the column/columns.

The following Query formula would only return the ‘sum Balance’ column above.

=query(A1:F,"Select Sum(F) Where A='Customer 1' group by A")

You can skip using the WHERE clause with the GROUP BY clause.

=query(A1:F,"Select A, Sum(F) group by A")

It will return the output as per the table below.

Customersum Balance
Customer 112964
Customer 268628

Why does the blank row appear at the top of the Query formula result?

It’s because we have blank rows in our data range A1:F. Either use A1:F10 or use the WHERE clause within Query.

=query(A1:F,"Select A, Sum(F) Where A<>'' group by A")
=query(A1:F,"Select A, Sum(F) Where A<>'' group by A Order by A Desc")

4. Pivot: Keyword to Pivot Data

The PIVOT is the fourth clause in the correct clause order to use in Google Sheets Query.

Here also, the WHERE keyword is not a must with PIVOT as it’s essentially for filtering data but recommended to filter out blanks when your data is open (A1:F instead of A1:F10).

=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot C")

See one more example.

=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot month(C)")
The Correct Order of Pivot Clause in Google Sheets Query

5. ORDER BY: Keyword Combination for Sorting

ORDER BY is the fifth keyword in the correct clause order in Google Sheets Query.

All the preceding clauses (keywords or keyword combinations) are optional with ORDER BY.

Below find a few Query formula examples of using the ORDER BY clause without other preceding keywords or keyword combinations.

Query Function ORDER BY without Other Preceding Clauses:

=query(A1:F,"Order by A")
=query(A1:F,"Order by A,C")
=query(A1:F,"Order by A Asc")
=query(A1:F,"Order by A Desc")

Query Function ORDER BY with Other Preceding Clauses:

=query(A1:F,"Select A,E order by A Asc")
=query(A1:F,"Select A,E Where A is not null order by A Asc")
=query(A1:F,"Select A, Sum(F) Where A<>'' group by A Order by A Desc")
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot month(C) Order By A Desc")

6. LIMIT: Keyword to Limit or Constrain the Number of Rows

Find below the examples for the correct clause order of the LIMIT clause in Google Sheets Query.

The LIMIT clause is independent and doesn’t require other clauses.

Here are example formulas, with or without using other clauses with LIMIT.

Query Function LIMIT without Other Preceding Clauses:

Limit the number of rows:

=query(A1:F,"Limit 5")

Query Function LIMIT with Other Preceding Clauses:

Query to limit the number of rows and columns.

=query(A1:F,"Select A,E Limit 5")
=query(A1:F,"Select A,E Where A<>'' Limit 5")
=query(A1:F,"Select A, Sum(F) Where A<>'' group by A Limit 1")
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot C Limit 1")
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot month(C) Order By A Desc Limit 1")

7. OFFSET: Keyword to Offset Rows

Does the Query OFFSET cLause require the other clauses to work?

Nope! The OFFSET clause will work stand-alone.

=query(A1:F,"Offset 1")

When we use other clauses with the OFFSET keyword, we must follow the correct clause order.

If we follow a different clause order, Google Sheets Query will return the #VALUE error.

It applies to all clauses!

The Correct Clause Order When Using the OFFSET Clause in Google Sheets Query:

=query(A1:F,"Select A,E Offset 2")
=query(A1:F,"Select A,E Where A<>'' Offset 2")
=query(A1:F,"Select A, Sum(F) Where A<>'' group by A Offset 1")
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot C Offset 1")
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot month(C) Order By A Desc Offset 1")
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot month(C) Order By A Desc Limit 1 Offset 1")

8. Label: Keyword to Customize Field Labels

The LABEL clause in Query doesn’t need the support of other clauses to work.

I hope the below Query formula example itself is self-explanting.

=query(A1:F,"label A 'Name', B 'Inv'")
Format Labels in Query

What about using the LABEL clause with other clauses preceding?

=query(A1:F,"Select A,E Label E 'Amt Cr'")
=query(A1:F,"Select A,E Where A<>'' Label E 'Amt Cr'")
=query(A1:F,"Select A, Sum(F) Where A<>'' group by A Label Sum(F) 'Total Balance'")
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot C Label A'Name'")
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot month(C) Order By A Desc Label A 'Name'")
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot month(C) Order By A Desc Limit 1 Label A'Name'")
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Pivot month(C) Order By A Desc Limit 1 Offset 1 Label A 'Name'")

9. Format: Keyword to Specify Format Patterns

To learn this clause, please read my post – How to Format Date, Time, and Number in Google Sheets Query.

We can use the FORMAT clause in Google Sheets Query with or without other clauses.

=query(A1:F,"format C 'dd-mmm'")

FORMAT is the last clause in the Query clause order. You can close the ‘query’ syntax with it, followed by the header as below.

Query Function Syntax:
QUERY(data, query, [headers])
=query(A1:F,"Select A, Sum(D) Where A<>'' group by A Order By A Desc Limit 1 Offset 1 Label A 'Name' Format Sum(D) '$##.00'",1)

I have skipped the PIVOT clause in the above Query formula as formatting a Pivot header needs a workaround.

Do you know how to format Google Sheets Query Pivot labels?

If not, you can read this guide – How to Format Query Pivot Header Row in Google Sheets.

You may bookmark this webpage to check the correct clause order in Google Sheets Query as and when required.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

More like this

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.