In this article:

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

>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!


Automate repetitive tasks with Lido

Save hours on repetitive and tedious work. Lido is a new spreadsheet that connects your spreadsheets, forms, PDFs, and email inbox.

Trigger
Action
Select trigger
When a cell value in Google Sheets changes
Check Google Sheets for today's date
When a new row is added to Google Sheets
On new Google Form submission
Send me a daily reminder
On new Typeform submission
When a cell value in database changes
Check database for today's date
When a new row is added in database
When a new HubSpot customer is created
Select action
ADDCALENDAREVENT
Creates a one- or multiple-day calendar event with optional attendees
ADDHUBSPOT
Adds an object to Hubspot
ADDSLACKCHANNEL
Create a Slack channel, and optionally add a topic or members
CALLURL
Makes any HTTP request
CREATEGOOGLEDOC
Replaces [@column_name] values in a Google Doc with the corresponding table row's values
CREATEPDF
Replaces [@column_name] values in a Google Doc with the corresponding table row's values, then export it as a PDF
FETCH
Makes any HTTP request and returns the response
INSERTROWS
Inserts given array below defined values in given worksheet
SENDGMAIL
Sends an email using your Google account
SENDOUTLOOK
Sends an email using your Microsoft Outlook account
SENDSLACK
Sends a Slack message
SENDSMS
Sends an SMS message using your Twilio account
UPDATECELL
Updates cells with given values
UPDATEHUBSPOT
Updates a property of a Hubspot object
Trigger is required
Action is required
Get started