In this article:

How to Arrange the Order of SQL Query Results in Google Sheets

May 8, 2024
>Click here to Import a MYSQL Database to a Lido Spreadsheet

In this tutorial, we will apply the order by clause to change the way the query results are listed in Google Sheets. The order by clause allows you some flexibility by letting you specify which column will take precedence in rearranging the query results.

Arrange using queried data

To see how order by works, let us have a simple example of rearranging the list of products by the price. Our source sheet is below:

a product list. Includes the item name, the price per item, the cost per item, and the item code.
Source sheet with the item name, the price per item, the cost per item, and the item code.


If we want to query that list while rearranging it by increasing price per item, we will make the following query (without quotes):

"select A, B, C order by B"

With this, we'll have to place it in Google Sheets' QUERY function with the format:

=query([range],"[SQL query]'")

And the result will look like this:

a queried product list. Includes the item name, the price per item, and the cost per item, but listed according to increasing price per item
Our new queried product list with increasing price per item


Arrange using aggregated data

The order by clause becomes more powerful when you combine it with the aggregation functions. We already have the following query results that we got by aggregating the total profit from each of the products offered:

a list of total profit from each product from a specific week.
A list of total profit from each product from a specific week.

We want to rearrange it from the smallest profit to largest profit. We will add the order by clause to the original query (without quotes):

"select B, sum(H) group by B order by sum(H)"

With this, we'll have to place it in Google Sheets' QUERY function with the format:

=query([range],"[SQL query]'")

And the result will look like this:

 a list of total profit from each product from a specific week, arranged by increasing total profit
A list of total profit from each product from a specific week, arranged by increasing total profit


Arrange in descending order

By default, order by arranges the query results in ascending order. How can we arrange the results in descending order?

Simply add desc keyword after the column specified in the order by clause.

With our previous example, we will then have the following query (without quotes):

"select B, sum(H) group by B order by sum(H) desc"

With this, we'll have to place it in Google Sheets' QUERY function with the format:

=query([range],"[SQL query]'")

And the result will look like this:

the same list of total profit from each product from a specific week, arranged by decreasing total profit
The same list of total profit from each product from a specific week, arranged by decreasing total profit


A bit of a hassle?

This is a fairly straightforward tutorial, but you'll likely need to use order by in combination with other SQL queries to perform more complex data analysis.

What if there's a way you can do the same thing in a few clicks, alongside sophisticated analysis of metrics to make your decision making easier?

If that’s what you fancy, consider trying Lido. With a few clicks, you can now access all the relevant metrics without going through the hassle of accessing the SQL databases of your eCommerce platforms and then coding the formulas to process them. Let our platform do it all for you!


Get Google Sheets productivity and automation tips delivered straight to your inbox
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
We'll email you 1-3 times a week — and never share your information.

Work less, automate more!

Use Lido to connect your spreadsheets to email, Slack, calendars, and more to automate data transfers and eliminate manual copying and pasting. View all use cases ->