# Tutorial - QUERY: “The most powerful function” in Google Sheets (part 2)

**URL:** <https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849>\
**Category:** Ask for Help\
**Created:** [June 14, 2020, 9:41am UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849 "2020-06-14T09:41:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [June 14, 2020, 9:41am UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849/1 "2020-06-14T09:41:48Z")

</div>

If you have not read part 1, below is the direct link so you can have a read at what QUERY is, and how it can help your case, whether it be building Glide apps or make your normal day work easier.

> [@Tutorial - QUERY: "The most powerful function" in Google Sheets (part 1)](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-1/10432):
>
> This topic was inspired by our last community meetup, in which we talked about some formulas in Google Sheets and some people want to know more about QUERY, which was branded [“The most powerful function in Google Sheets”](https://www.benlcollins.com/spreadsheets/google-sheets-query-sql/) by famous GSheets teacher Ben Collins. So, what is QUERY, and why is it so powerful. Here’s a writeup on it, which hopefully would help many of you here in this community. Trust me, it would change so much how many of you “clean” your data in the future from raw data, in a very…

> **6/Dynamically sort your data using ORDER BY**
>
> An ORDER BY claused can be used to dynamically sort your data after being queried, and you can change the column that is used to sort with just a letter.
> 
> By default, the ORDER BY sorts ascendingly, and it can grab the data type in the column automatically. Below is an example where I sorted the data on the left by column B, ascendingly, by alphabetical order.
> 
> ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/a/ad8073363ac6b548210af0b914d6f5a7f80fa697.png)
> 
> And here’s how it goes when I change the sorting column to C.
> 
> ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/9/9dd1b8db615652e2cbddf5c71fb0d454733f7c31.png)
> 
> So how do you sort descendingly? The answer is you add a DESC at the end of the ORDER BY (and for ascending it’s ASC).
> 
> ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/1/106c4681a230bc3284dcb32222d6712c53491479.png)

> **7/Top 5, Top 10, Bottom 5, Bottom 10? We can do it all with LIMIT**
>
> This would be useful in many cases where you want to highlight the top performing employee in your company, the ones who earn most, the worst performing SKUs in your marketplace, etc.
> 
> We still using ORDER BY here, just add a LIMIT clause at the end to make the “Top”/“Bottom” thing work.
> 
> For example, here are the people in the top 5 for highest salary in my example.
> 
> ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/c/c8f50f15df2876841bb82ccfefcf951164100e2e.png)
> 
> And here are the top 2 only for Amazon.
> 
> ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/c/c6fc627e352e49e1f8eeeb5eeacf3338a940661f.png)
> 
> And the bottom 7 overally.
> 
> ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/4/4b1bcc2f19397fb713317c39b46e1115882c125b.png)

> **8/GROUP BY - side by side with aggregated functions**
>
> In the 1st part, I have written about the aggregated function, and to make it work in a pivot style, we need the GROUP BY clause.
> 
> The rule of thumb here is: “every column in the SELECT clause (i.e. before the GROUP BY) must either be aggregated (e.g. counted, min, max) or appear after the GROUP BY clause” - as Ben Collins wrote.
> 
> I have extended my example to include the year of birth and month of birth for examples of this part.
> 
> Let’s say I want to know the average salary by year of birth, sorted ascendingly, the formula would be.
> 
> ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/e/eef719374586c644097682aeca1d16036e950c66.png)
> 
> Or if we want to see the max salary by company, sorted descendingly, here’s how it goes.
> 
> ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/9/9ff6788bbc1f89f5f696db5c96fd64ed665abe33.png)
> 
> To wrap this up, I show a table counting the number of people that was born in each month, by company.
> 
> ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/5/52705ab6e2040f5d54c36925ee343ced074e1e62.png)

> **9/Resources to learn further**
>
> There are a lot more of things to learn, that you can read via this article - about data manipulation inside QUERY.
> 
> > **[Query Language Reference (Version 0.7)  |  Charts  | ...](https://developers.google.com/chart/interactive/docs/querylanguage#data-manipulation-functions)**
> >
> > Learn how to use this language and discover detailed documentation for its classes, functions, and element.
> 
> Also, these videos from Ben Collins might help.
> 
> [![](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/e/5/e5b112549d7e47d4f0357a40b34ba63bdea21323.jpeg "Google Sheets Query Function - Part I") ](https://www.youtube.com/watch?v=iWi3nL5MPK4)
> 
> [![](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/b/beba803078e281c7066045d84df98553791f6a6b.jpeg "Google Sheets Query Function - Part II") ](https://www.youtube.com/watch?v=DY_TpplozAk)
> 
> Hope the 2 tutorials about QUERY would help you a lot in your specific use cases!

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [June 14, 2020, 9:46am UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849/2 "2020-06-14T09:46:51Z")

</div>

@Krivo, @Erni_D, @osxzxso here’s the 2nd part, hope it helps.

---

<div class="post-metadata">

**Author:** ![Krivo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/krivo/32/5363_2.png) [@Krivo](https://community.glideapps.com/u/Krivo)\
**Post date:** [June 14, 2020, 9:47am UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849/3 "2020-06-14T09:47:39Z")

</div>

@ThinhDinh thanks a lot for sharing. 👍👍

---

<div class="post-metadata">

**Author:** ![Roberto\_Salim](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/roberto_salim/32/6861_2.png) [@Roberto\_Salim](https://community.glideapps.com/u/Roberto_Salim)\
**Post date:** [June 14, 2020, 11:15am UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849/4 "2020-06-14T11:15:54Z")

</div>

This will help me a lot, I started studying this function yesterday.

thank you 👍

---

<div class="post-metadata">

**Author:** ![Wiz.Wazeer](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/wiz.wazeer/32/72423_2.png) [@Wiz.Wazeer](https://community.glideapps.com/u/Wiz.Wazeer)\
**Post date:** [June 14, 2020, 12:40pm UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849/5 "2020-06-14T12:40:40Z")

</div>

Can’t thank you enough for your contribution to these topics.

---

<div class="post-metadata">

**Author:** ![Wiz.Wazeer](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/wiz.wazeer/32/72423_2.png) [@Wiz.Wazeer](https://community.glideapps.com/u/Wiz.Wazeer)\
**Post date:** [June 14, 2020, 12:47pm UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849/6 "2020-06-14T12:47:01Z")

</div>

You and Jeff are just simply amazing ppl when it comes to going out of your way to help others, and sharing your knowledge. If i can be of any help, do let me know. 😊

---

<div class="post-metadata">

**Author:** ![osxzxso](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/osxzxso/32/7356_2.png) [@osxzxso](https://community.glideapps.com/u/osxzxso)\
**Post date:** [June 24, 2020, 3:16am UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849/7 "2020-06-24T03:16:22Z")

</div>

@ThinhDinh Sorry for the late reply. My summer term recently started so my schedule has been very busy lately. Just looked over part 2 and, wow, perfect breakdown. There’s so much helpful information in there. 🙏

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [June 24, 2020, 3:18am UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849/8 "2020-06-24T03:18:56Z")

</div>

Have fun with the study and stay safe bro 😄

---

<div class="post-metadata">

**Author:** ![XceL](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/xcel/32/91079_2.png) [@XceL](https://community.glideapps.com/u/XceL)\
**Post date:** [July 23, 2020, 10:30am UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849/9 "2020-07-23T10:30:41Z")

</div>

@ThinhDinh saving the day again 🙂  
Thanks for this, I was overcomplicating my query with pivot and was stuck trying to filter the column name when I can simply use multiple group bys

your last example showed me the way

Thanks for the amazing sharing

Cheers

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [July 23, 2020, 10:58am UTC](https://community.glideapps.com/t/tutorial-query-the-most-powerful-function-in-google-sheets-part-2/10849/10 "2020-07-23T10:58:28Z")

</div>

My pleasure to help. Let me know if I can help you in the future.
