How to sort Query using ORDER BY in Google Sheets

How to sort Query using ORDER BY in Google Sheets
Reading Time: 3 minutes

In this guide, you can learn how to sort Google Sheets queries using the ORDER BY function in Google Sheets.

How to sort data using ORDER BY in Google Sheets

The ORDER BY clause sorts the dataset in ascending or descending order based on specified columns. This helps you analyse and interpret data in a more meaningful way.

4 Steps to SORT data using ORDER BY in Google Sheets

Step 1:- Prepare and select data

  • Create a dataset of interest and name the sheet containing the dataset.
  • Create another sheet so as to query the dataset.
Creating dataset for querying
Figure 1: Create a dataset and different sheet for querying

Step 2:- Start Querying using Google Sheets QUERY command

  • Go to sheet Query.
  • Select a cell and type “ =QUERY ( in the cell as shown below.
  • The parameters to be mentioned in the command are mentioned below :
Using QUERY command of Google Sheets
Figure 2: Start querying using the QUERY command

Step 3:- Enter the dataset in the QUERY command

  • Go to the Dataset sheet.
  • Select the dataset you want to query by selecting the cell A1 and pressing Ctrl + A (for Windows ) or Cmd + A (for Mac) as shown below :
Selecting dataset for querying in Google Sheets
Figure 3:- Select the dataset of interest for quering

Step 4:- Sort the query by using the ORDER BY clause

  • The second parameter in the QUERY command is a query.
  • Type the query you want to after selecting the dataset.
  • End the query with the ORDER BY command to sort the query according to the requirement. Here I have sorted them by A column, in ascending order.
Dataset sorted in ascending order
Figure 4: Dataset sorted by Pregnancies count in ascending order

Example 1:- ORDER BY One Column in Descending Order

  • You can sort the query by a column in descending order by typing this command :
  • Here the dataset is ordered by column C in descending order as shown below :
Sorting by descending order
Figure 5: ORDER BY column in descending order using Google Sheets Query function

Example 2:- ORDER BY One Column in Ascending Order

  • You can sort the query by a column in ascending order by typing this command:
  • Here the dataset is ordered by column C in ascending order as shown below:
Sorting by ascending order
Figure 6: ORDER BY column in ascending order using Google Sheets Query function | ORDER BY in Google Sheets

Example 3:- To sort Multiple Columns using Google Sheets ORDER BY Query

  • You can use the ORDER BY command to sort the query in various ways.
  • You can sort the query by multiple columns, one in descending order and the other in ascending order by typing this command :
  • Here I have sorted them by multiple columns, one by descending, other by ascending, as shown below :
Sorting by descending and ascending order
Figure 7: Query sorted by C in descending order, followed by A in ascending order | ORDER BY in Google Sheets

See Also

Most Commonly Used Google Script Functions for 2022: Learn about widely used advanced script functions.

Query Function in Google Sheet- All Variations: Using the Google Sheet Query function to run SQL queries.