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:
- Steps to sort data using query ORDER BY
- Sort a column in descending order
- Sort a column in ascending order
- Sort multiple columns
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.
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.
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:
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.
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:
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:
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.
Query Function in Google Sheet- All Variations: Using the Google Sheet Query function to run SQL queries.