Skip to playerSkip to main contentSkip to footer
  • 6/12/2025
Learn the fundamentals of SQL GROUP BY with our visual guide! This video breaks down the concept using a unique infographic of colored / coloured stick figures, showing how data is grouped and aggregated. We also demonstrate practical examples using a salary dataset, grouping by gender and age group in both a spreadsheet and a MySQL database. See the SQL commands in action and try them yourself using the link to mycompiler.io provided below. Perfect for beginners and intermediate learners looking to grasp this essential SQL concept. Don't forget to subscribe for more SQL tutorials!
---
Code from the video: https://www.mycompiler.io/view/JYu3GflAdsd
Run SQL code online free: https://www.mycompiler.io/new/sql
---
💻 Get started with cloud or VPS hosting with DigitalOcean and receive a $200 credit for 60 days through our link 👇
https://watsontechworld.com/digitalocean
---
Our website: https://watsontechworld.com
---
00:00 Intro and overview of video
01:02 SQL GROUP BY in a nutshell with an example
01:58 Infographic for SQL GROUP BY with example spreadsheet
06:36 Spreadsheet approximating GROUP BY p1
07:57 SQL GROUP BY commands in MySQL p1
08:45 Spreadsheet approximating GROUP BY p2
10:25 SQL GROUP BY commands in MySQL p2
11:22 GROUP BY and discussing aggregate functions in SQL
11:46 2 examples of SQL GROUP BY commands
12:13 Important note about GROUP BY syntax
12:35 Infographic review of GROUP BY with MySQL
12:48 SQL GROUP BY commands in MySQL p3
13:05 Review of SQL SELECT
13:17 SQL GROUP BY commands run online interactively
14:29 Review and conclusion
---
#SQL #MySQL #GROUPBY #SQLTutorial #SQLGROUPBY #DataAnalysis #database #LearnSQL

Category

🤖
Tech
Transcript
00:00Hi, everyone. In this video, I'm going to teach you and go over the SQL group by statement.
00:09And the SQL group by statement is very useful and is an important statement in SQL.
00:16And very briefly, I'm going to show you an image I made.
00:21If I had seen an image like this when I first learned about group by, I think it would have made it much easier.
00:27And then I'm going to show you an example spreadsheet and showing you doing something that's roughly equivalent to group by.
00:37And then I'm going to show you actually doing group by in a database.
00:41And I'll show you how you can play around with the group by statement by yourself in this link that I'll show you.
00:54So I hope this video will be useful for you and a good overview of group by.
01:00OK, so in a nutshell, group by is a way to take rows and put them into groups or buckets.
01:10By a common thing such as their column and then usually do things like summary statistics on the groups that you made.
01:18And then this is the basic form of a group by statement.
01:30So you're going to select some grouping columns, some aggregate functions on some columns from some table group by some grouping columns.
01:42OK, so this is an example.
01:44Let's say group by gender and the average yearly salary from salary and group by gender.
01:54So let's come back to this.
01:57I'm going to let's go over this image first because I think this image will really help at least it has for me.
02:04And this is a simple image, but it took me a while to make this.
02:09OK, so let's say you have original data.
02:12For example, I'm going to show you this.
02:15We have name column, career column, yearly salary, gender, age and age group.
02:20And notice that we have different people.
02:23We have males, we have females, M for male, F for female.
02:27We have different age groups, people in their 30s and their 20s, in their 40s and so on.
02:34And we can think of each one of these people as being part of some group.
02:42For example, males, Brad is a male, Luke is a male and so on.
02:48And if we look at this, we can think of each person could be thought of as being part of some group, whether it's just depends.
02:58Maybe this person's occupation is the same as this person's occupation or maybe their age is the same as this person's age, age and so on.
03:07Maybe where they're from is the same, you know, their nationality or whatever.
03:13There's so many different ways to classify people or things.
03:17But let's just say, for example, we want to talk about age or age group.
03:21Maybe this person is in their 30s.
03:24This maybe red is people in their 20s.
03:27Maybe purple is people in their 40s.
03:30Maybe green is people in their 50s and so on.
03:34You just need to pick the group, whatever group that is.
03:38And the color here is just, you can think of it as basically just any category of thing to group by.
03:46So notice that we have, again, all these different people.
03:50And they all have their types of information.
03:55But, for example, we can say that there's two, here there's two genders, male and female.
04:05We can put them into groups or we can put age group into groups.
04:09And this is what this is showing here.
04:12This is showing putting them into groups.
04:15So I took this yellow person, this yellow, yellow, put it there, blue, blue, blue, put that into a group.
04:21So this is a blue group, a green group, a red group, a purple group, and so on.
04:27And the colors for me is to represent basically any category you want to sort by.
04:32This could have been gender, for example.
04:36Anyway, the point is that we have, I happen to have five groups.
04:41It doesn't have to be five groups.
04:43It could be two groups.
04:44For example, maybe we just have this is male, this is female, and, you know, we just had less data than this.
04:53Or maybe this is career, maybe this is doctor, and maybe this is lawyer, and maybe this is teacher, engineer, and so on.
05:03Or, again, there's so many different ways you can classify things into groups.
05:10I just happened, this is a salary spreadsheet, but you can apply the group by idea to basically anything.
05:20So the first thing is you have the original data, and then what you do is you group by.
05:25So you sort, you can sort them, for example, get all the yellows together, or you can have a filter.
05:31Many different ways to do this, but the basic idea is you just make the groups.
05:35And then the next step is to do aggregate functions.
05:39So, for example, let's say we wanted female.
05:44We can take all the females and then get their average salary.
05:50Or we could have done the minimum salary, or this means get the number of those rows in that group and more.
06:00So again, I think that this visualization can help, I hope.
06:05So the main idea for group by is you have the original data, and then you decide on something to group by.
06:11For example, color.
06:13But here the color doesn't have to mean literally just color.
06:16It could mean their occupation, or their income, maybe their income category, or their, again, their nationality, or maybe their first language, or many other things.
06:30And then we do aggregate functions.
06:33And then the final thing is that we have the group by thing.
06:37So let's go through the spreadsheet.
06:39Again, we have this data.
06:41And Brad is a doctor, has $150,000 per year, is a male, is age 35, happens to be in the 39 group, or 30s group, and so on.
06:54Shero is a software engineer, makes this much, and so on.
06:58And Shero is in a different age group, and is a different gender.
07:02So let's say, for example, we want to group by the gender, and get the average salary.
07:08So the first thing we can do is we can, for example, do a, we can take this same data, and all I did here is I just sorted it by gender.
07:19Or, yeah, so you can, for example, here you can do data, sort range, and then you could have done the, by gender, and then I did from reverse.
07:32So all this did is just basically put it into, and then I added, I think, three spaces, three blank rows.
07:40And here all I did is, okay, so here are all the females, and what I did is I took the average of all their values, and similarly, I did the average of all the males.
07:55And this is actually how I did it in a real database.
08:02So the command is select gender, comma, the average, this, this, in SQL, average is AVG.
08:12If you type A-V-E-R-E-G-E, you'll actually get an error.
08:17So you can only do average this way in SQL.
08:20So I did the average of yearly salary, from the salary table, group by gender.
08:25And notice here, we have 91203.75, and that's what that is there.
08:33And then the female average is 256666.6667, and we get the same value there.
08:43So, okay, and let's go, let's do another one here.
08:46Let's do, we'll group by the age group.
08:50So for example, we have 20s, 30s, 40s, 50s, 60s, and 70s.
08:59So we have five distinct groups.
09:02And what we can do here is, again, I, here all I did is I did a, I sort by the age group.
09:11Like here, you see we have the distinct groups.
09:13And then I just added three, three blank lines.
09:18And what I did here is I did average yearly salary.
09:22I did the average function in here.
09:27And the average yearly salary for people in their 20s is this.
09:32And this person is a bit of an outlier.
09:35That's why this is so high.
09:38For people in their 30s, the average salary is 120,000.
09:43And I don't know if these people are outliers, if that's really representative of people in their 30s.
09:48But for this limited amount of data, the average was just this.
09:54So this plus this plus this plus this plus this over five.
09:57And that's what that average function does.
10:00And similarly, this is people in their 30s, or in their 40s, sorry.
10:04People in their 40s.
10:05And that's the average salary.
10:08And there was only one person in the 50s, so it's going to be the same number.
10:17And similarly, there's only one person here, so it's the same number.
10:21And I just wanted to show you now in SQL doing the same thing.
10:27What we did is we did select, and then we did the age group.
10:36So select age group, and average yearly salary from salary group by age group.
10:46And notice here, this is people in their 30s.
10:49And this is not sorted by this.
10:52You know, this is slightly out of order, but that's okay.
10:55Let's look at this up.
10:56This people in their 30s have an average of 120,000.
10:59Is that right?
11:00People in their 30s average 120,000.
11:04That's right.
11:05Okay, people in their 20s.
11:06They have 398157.
11:09Okay, fantastic.
11:11And let's test one more.
11:12People in their 40s should be 8840.
11:19And we have 88400.
11:21So that's correct.
11:22Correct.
11:23So that's the basic idea of group by.
11:26And again, we did an example.
11:30That's an example.
11:31And we have aggregate functions.
11:33And that includes average, AVG, min is minimum, max is maximum, and count star.
11:40This tells you how many rows there are for a given thing.
11:45And again, this is an example.
11:47Let's say you wanted to group by the gender and get the average yearly salary for that group.
11:56And this is the general form of how you would do it.
11:59Select gender, average yearly salary, comma, average yearly salary from salary, group by gender.
12:05And similarly, this is how you can do it for age group.
12:09And this is something you might get.
12:12And this is an important note that if you're using group by, all the columns after the select, this one here, must also either be in the group age group, but must also be after group by or have an aggregate function like this.
12:30Otherwise, you'll get an error, most likely.
12:35And again, just as a reminder, this is the basic idea.
12:39You have the original data, put them into groups, put some aggregate, calculate some aggregate functions, most likely.
12:46And then you'll end up with something like this.
12:50You see here we had, this was the average yearly salary by age group.
12:56And this is average yearly salary by gender.
13:01And group by really is very useful, but it can take some time to get used to.
13:07Okay, so this is a review of SQL select.
13:11And if you want more in depth about SQL select, you can go watch my video here.
13:17And I wanted to show you here, again, I mentioned that you can play around with this.
13:22So what I did here is, this is making a table and this is inserting the same data as I had in the original data.
13:32And here, this is select star from salary, that's showing all the, this is showing the same data.
13:41Well, we have Brad as a doctor, Sana, Luke, and so on.
13:48And I'll put a link to this so you can actually literally copy this.
13:52And what you can then do is go to this link and also put this in the description.
13:58And you can override this code and you can run it yourself.
14:02And you can see I have like that.
14:05Now I'm going to uncomment.
14:07I'm going to comment this out and I'm just going to show you a one group by.
14:12So this is the group by gender.
14:14Get the average yearly salary.
14:16And that's what we had before.
14:18And for example, now let's do a group by age group and get the average yearly salary.
14:25And we notice we have these different age groups and their average yearly salary.
14:29Okay, so again, the group by, it's a little bit complicated initially, but I really hope that this image can help you.
14:36So again, you have, you can think of each person as unique, but they're also in categories.
14:42And you just pick the category or thing that you want to group by, make the different groups and then perform aggregate functions.
14:49Okay, I hope you enjoyed this video.
14:50If you liked it, please subscribe to or follow my channel and see you in the next one.

Recommended