How to sort Query using ORDER BY in Google Sheets

Reading Time: 4 minutes

In this guide, we’ll learn how to sort Google Sheets queries using the QUERY function’s ORDER BY in Google Sheets.

Sections of the article:

How to sort data using ORDER BY in Google Sheets

The Google Sheets ORDER BY clause in the QUERY function sorts the dataset in ascending or descending order based on specified columns. This helps us to analyse and interpret data quickly and effectively.

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.
Data set to query using order by clause in Google Sheets
Create a dataset and different sheet for querying

Step 2:- Enter the Google Sheets QUERY command

  • Go to the second sheet named Query.
  • Select a cell and type in the name of the query function, which is =QUERY.
  • The parameters to be included will be displayed in the suggested function as shown below.
Using QUERY command of Google Sheets
Start querying using the QUERY command

Step 3: Enter the dataset in the QUERY command

We’ll use the data in the first sheet as the data to query and sort using the ORDER BY clause.

Add the range of the data as the “data” parameter in the QUERY function.

  • Go to the first sheet, ie, the Dataset sheet.
  • Select the dataset you want to query by pressing Ctrl + A (for Windows ) or Cmd + A (for Mac) as shown below:
Selecting dataset for querying in Google Sheets
Select the dataset of interest to query

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

The second parameter in the QUERY command is query. Here’s where we’ll insert the query ORDER BY clause in order to sort the data in a particular order.

  • Type the query you want. For this example, the query is Select *Where B >120 to retrieve all values greater than 120 from column B.
  • Append in the query ORDER BY to sort the query–in ascending or descending order. Use ASC for ascending and DESC for descending order; if not specified, the data are sorted in ascending order. Here I have sorted them by A column, in ascending order.
Dataset sorted in ascending order using the Google Sheets query order by
Datasets sorted in ascending order of column A using Google Sheets query order by

Let us learn some more use cases of the Google Sheets QUERY ORDER BY with examples.

Example 1: ORDER BY one column in descending order

To sort the data by using Google Sheets QUERY ORDER BY clause in descending order of C, and where values in B is greater than 120, we’ll use the following QUERY formula.

=QUERY(Dataset! A:I, "Select * where B > 120 ORDER BY C DESC")

Here the dataset is ordered by column C in descending order as shown below:

Sorting by descending order
ORDER BY column in descending order using Google Sheets Query function

Example 2: ORDER BY one column in ascending order

To sort the data by using ORDER BY in Google Sheets, we can use the following QUERY formula.

=QUERY(Dataset! A:I, "Select * where B > 120 ORDER BY C ASC")
  • Here the dataset is ordered by column C in ascending order as shown below:
Sorting by ascending order
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

  • We can use the Google Sheets query ORDER BY command to sort the query in various ways.
  • We can sort the query by multiple columns, one in descending order and the other in ascending order by typing this command.
=QUERY(Dataset! A:I, "Select * where B > 120 ORDER BY C DESC, A ASC")
  • Here the data are sorted by multiple columns, one in descending, and another in ascending, as shown below.
Sorting by descending and ascending order
Query sorted by C in descending order, followed by A in ascending order | ORDER BY in Google Sheets

See Also

How To Scrape Websites Using Google Sheets

How to use the QUERY function with IMPORTRANGE in Google Sheets

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

X
Hire expert data analysts on-demand. Get 30 Days Free Trial