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
€64.96
List Price:  €92.81
You save:  €27.84
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$106.02
List Price:  A$151.47
You save:  A$45.44
S$94.41
List Price:  S$134.88
You save:  S$40.47
HK$546.80
List Price:  HK$781.18
You save:  HK$234.37
CHF 63.34
List Price:  CHF 90.49
You save:  CHF 27.15
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.42
List Price:  NZ$166.33
You save:  NZ$49.90
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,416.31
List Price:  ₨27,738.77
You save:  ₨8,322.46
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$356.70
List Price:  B$509.60
You save:  B$152.89
R1,295.44
List Price:  R1,850.72
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪259.50
List Price:  ₪370.74
You save:  ₪111.23
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,712.31
List Price:  ¥15,303.96
You save:  ¥4,591.65
MX$1,187.89
List Price:  MX$1,697.07
You save:  MX$509.17
QR254.57
List Price:  QR363.69
You save:  QR109.12
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.64
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,764.23
List Price:  ISK kr13,949.49
You save:  ISK kr4,185.26
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.59
List Price:  ден5,712.52
You save:  ден1,713.92
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,305.79
List Price:  Ft36,152.68
You save:  Ft10,846.88
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
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.