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