Grouping Data using SQL GROUP BY Clause

Microsoft SQL Server 2017 for Everyone Group By and Aggregate Functions
11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.35
List Price:  €96.21
You save:  €28.86
£55.83
List Price:  £79.77
You save:  £23.93
CA$100.76
List Price:  CA$143.95
You save:  CA$43.19
A$112.23
List Price:  A$160.33
You save:  A$48.10
S$95.14
List Price:  S$135.93
You save:  S$40.78
HK$543.62
List Price:  HK$776.63
You save:  HK$233.01
CHF 63.04
List Price:  CHF 90.06
You save:  CHF 27.02
NOK kr797.48
List Price:  NOK kr1,139.30
You save:  NOK kr341.82
DKK kr502.49
List Price:  DKK kr717.87
You save:  DKK kr215.38
NZ$124.10
List Price:  NZ$177.29
You save:  NZ$53.19
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,361.55
List Price:  ৳11,945.58
You save:  ৳3,584.03
₹5,962.79
List Price:  ₹8,518.63
You save:  ₹2,555.84
RM314.04
List Price:  RM448.65
You save:  RM134.61
₦108,335.42
List Price:  ₦154,771.52
You save:  ₦46,436.10
₨19,495.71
List Price:  ₨27,852.21
You save:  ₨8,356.49
฿2,390.81
List Price:  ฿3,415.60
You save:  ฿1,024.78
₺2,467.44
List Price:  ₺3,525.07
You save:  ₺1,057.62
B$434.14
List Price:  B$620.23
You save:  B$186.09
R1,296.68
List Price:  R1,852.48
You save:  R555.80
Лв131.68
List Price:  Лв188.12
You save:  Лв56.44
₩102,198.93
List Price:  ₩146,004.73
You save:  ₩43,805.79
₪256.56
List Price:  ₪366.53
You save:  ₪109.97
₱4,090.81
List Price:  ₱5,844.26
You save:  ₱1,753.45
¥10,994.99
List Price:  ¥15,707.80
You save:  ¥4,712.81
MX$1,408.98
List Price:  MX$2,012.91
You save:  MX$603.93
QR255.09
List Price:  QR364.43
You save:  QR109.34
P967.82
List Price:  P1,382.66
You save:  P414.84
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,562.49
List Price:  E£5,089.49
You save:  E£1,527
ብር8,907.19
List Price:  ብር12,725.10
You save:  ብር3,817.91
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$69,323.69
List Price:  CLP$99,038.09
You save:  CLP$29,714.40
CN¥510.80
List Price:  CN¥729.75
You save:  CN¥218.94
RD$4,245.36
List Price:  RD$6,065.06
You save:  RD$1,819.70
DA9,465.07
List Price:  DA13,522.10
You save:  DA4,057.03
FJ$162.39
List Price:  FJ$232
You save:  FJ$69.60
Q539.11
List Price:  Q770.19
You save:  Q231.08
GY$14,638.77
List Price:  GY$20,913.42
You save:  GY$6,274.65
ISK kr9,772.70
List Price:  ISK kr13,961.60
You save:  ISK kr4,188.90
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,291.33
List Price:  L1,844.84
You save:  L553.50
ден4,143.97
List Price:  ден5,920.21
You save:  ден1,776.24
MOP$559.97
List Price:  MOP$800
You save:  MOP$240.02
N$1,289.45
List Price:  N$1,842.15
You save:  N$552.70
C$2,574.80
List Price:  C$3,678.44
You save:  C$1,103.64
रु9,534.71
List Price:  रु13,621.60
You save:  रु4,086.89
S/260.99
List Price:  S/372.86
You save:  S/111.86
K283.85
List Price:  K405.52
You save:  K121.66
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,936.45
List Price:  ZK2,766.47
You save:  ZK830.02
L335.12
List Price:  L478.77
You save:  L143.64
Kč1,694.03
List Price:  Kč2,420.15
You save:  Kč726.12
Ft27,771.44
List Price:  Ft39,675.18
You save:  Ft11,903.74
SEK kr774.38
List Price:  SEK kr1,106.30
You save:  SEK kr331.92
ARS$71,792.58
List Price:  ARS$102,565.23
You save:  ARS$30,772.64
Bs484.55
List Price:  Bs692.24
You save:  Bs207.69
COP$307,165.41
List Price:  COP$438,826.54
You save:  COP$131,661.12
₡35,530.54
List Price:  ₡50,760.09
You save:  ₡15,229.55
L1,777.33
List Price:  L2,539.15
You save:  L761.82
₲546,250
List Price:  ₲780,390.60
You save:  ₲234,140.59
$U3,115.91
List Price:  $U4,451.49
You save:  $U1,335.58
zł287.82
List Price:  zł411.19
You save:  zł123.37
Already have an account? Log In

Transcript

Hey guys, in this video we will be looking at SQL group by statements. Now the SQL group by statement is an excellent way to literally group the data to see more distinct data. And we will look at how it is written the different variations and considerations that one must make in using this statement. And in the rest of this section, we will actually be looking at aggregate functions that rely on this statements. Now, I did start off by saying that the SQL group by statements can help us to identify distinct data. And I just want to inject a bit of disambiguation there with the Select distinct statement.

So select distinct actually says that whatever data I'm about to select, I will look for are only unique rows. That's what the Select distinct does. So if I did a select distinct on enrollments, then what it would do is if I had a rule that say, row 12, with the same data across the columns that I'm selecting, then it would only bring back one rule. So let's take for instance, and here's a practical example. At least in this enrollment stable. In real world, there should be no tools that have the same ID, teacher ID, student ID, and course ID at least taking it out of the picture.

No teacher, student and course should be repeating in your tables. So there's that but then, in this situation, I do have a lecturer or a teacher teach in the course and I have two instances of that appearing here. So I have 141, and four. So I quickly wrote up two statements wanted to select the teacher ID and the course ID from enrollments and If you want to get fancy, of course, you can just enjoy everything so you get about more details. But just for the expediency, I'm just going to use this quick example. So select these two columns from the enrollments table.

And here's all of enrollments. So when I run this query, it should bring back 17 rows only with the teacher ID and the course ID, no one will look at them together, we see that we have a few repetitions here we have two and two here, two and two. Here, we have one and four, one and four, etc, etc. So the Select distinct is actually going to say, okay, since I'm seeing these two, the two and two, twice, then I only need to bring it back one time. And that's the purpose of select this thing. So I'm just going to run that line.

And then you can compare the data output. So here we see that we're now eliminating our don't from AWS, it's 17. So we're done from 17 rules. To just nine. So the select this thing actually eliminated all of those repeating rules. So if you're in a situation where you have repeating rules updatable, you really only need one rule represented in your results and then the distinct keyword right before the columns, and right after the word select that is your keyword to eliminate those repetitions.

That is, however not why we're here why we're here is to group. Now grouping actually does something a bit differently than the Select distinct grouping actually will bring back only one record that matches all of the requirements. And it brings about this one record by actually doing like a bat grown mathematics kind of thing to actually keep track of the number of rows that were there, allowing us to then layer certain mathematical functions on top. So if you need My flag for instance count or the average or the maximum or the minimum those kind of things, then you need to use a group by to actually bring back that one clump of the same data as opposed to the distinct the distinct will just omit group by actually clumps. So we'll just do some examples of this. And as we go through this section with the difference, I forget functions then you will see exactly what I mean by the group by actually helps with the mathematical function.

Now we'll just modify this query that's already here selecting everything from our enrollments and add a group by and the thing with a group by is that it is the last statement. So in other words, if you have an inner join, it comes before the group by if you have a where it comes before the group by so we're going to use a we're in this situation. So let's just see where and lets us run. So quarters against teacher ID two. So we want to do some things where teacher IDs equals two. And I'll just execute and sifters.

So this is all the data relating to teacher ID two. Now, the first thing to note with a group by is that well, the second thing to note with a group by is that every column that is being referenced needs to be included in the group by and this is a blessing or a curse, because the more columns that you put in, and the more variations of data is the less grouping that can actually occur. So let's take for instance, iD iD is different in every row in this result set. So if I included the ID in my group by I literally would see no difference. Alright, so the same way you would write the Select and and specify the columns of the same way you would, group by column, comma, column, etc, etc. And if I execute that, This query, then you'll see that there is literally no variation in the data set that comes back.

That's because because each role is unique, then there is nothing to group because it can't group distinctly different data or roles. So when you're running a group by have to start with a process of elimination, what data is not absolutely necessary to my grouping? So let's say for instance, ID, I really don't need a group ID. All right. And so my group by starts with teacher ID, and the school is up. Now that I've taken it out of the picture, it depends on what kind of data we want to get back once again.

So let's say our scenario is that we want to see how many are all of the courses being taught by this teacher, which means that I just want to see the teacher ID, and the course the teacher ID and the course teacher ID and the course and you can see that these start Repeating, because he's already here for of course, too. He's already here, for course, one, etc. So obviously, the variation of the values with students ID across the courses would skew that result. So I can just eliminate students idea now just coming to talk quickly, because I'm going to use it at a later date. And then I execute. And there you go.

So the group by is now eliminating all of the additional rows, and just bringing back the group's teacher ID with course, one course 243. And remember, that we use group by because we want to use some maths and we'll get into the math and the aggregate functions a bit later. But for now, just appreciate how the group by works. So we see here, obviously, if the grades were different than there would be variations there also. So it would be distinctly different data again, so Right now we don't need grid actually didn't remember to remove that and can execute. And there we go.

So the group bys are slumping these two together. What if we wanted to see all of the students that this teacher is teaching regardless of the course. So then students ID would become our source of contention. In addition to course ID so we can just comment, of course ID, and then we run it. And so we see teacher to come back with only one record of the students. And of course, we can go ahead and join it on our table if we wish.

All right. So I wrote up my inner join statements, and I'm just showing you this error here. And this is why I would have alluded to the use of aliases before because if two tables have the same column name, then there's is going to be some amount of ambiguity as to which one is which. So that's why we put on our aliases. And we use those to distinctly identify which table it is that we're referring to. Now, having added both tables, Inner Joins to this query, we need to add the columns.

Now, notice I'm getting this, this arrow saying that certain things are invalid because they're not a part of the GROUP BY clause. So remember that any column that I put in another space, so what I did was include the student first name and the student last name, and call it student name. And I included the teachers first name, and last name, call the teacher's name. And both of these or any column that I'm referencing in my select has to be referenced in my group. By, so I will have to also add s dot, first name, dot last name, and repeat the same for the teacher columns. And after I've completed adding my additional columns to my group by, so it really doesn't matter the fact that I'm concatenating remembering but the full name, it just matters that I am making a reference to them in my select.

And if I'm selecting them, then they must be a part of the group by so when when I write this entire query and this entire condition, then I must ensure that my group by is a part contains all the columns that are part of my query. And so below you'll see that the teacher ID and the student ID are going back and the student name is going back as well as the teacher name. And then really, and truly, I don't need to teach Your ID and the student ID. So process of elimination cleaning up my report. And here my output is just the student name and the teacher names and the back end I'm grouping by, and I could probably even take them out of the GROUP BY clause. And there you go.

So it's just a process of elimination really, and truly, so sometimes you're building from the ground up, you have a requirement for report, you don't know where the data is coming from you feel it's all to group by and then when you see a sensible result, then you can refine it so that you get the desired output. All right. So as we go along, we'll see how group by helps us with our aggregate functions.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.