Hey guys, in this video we'll be looking at the sum aggregate function. Our scenario today is to bring back the total number of credits being done by each student. And so we'll have to get a bit creative here because in order to get this data we need to see the students information will see the course information because our courses table has the number of credits. And of course, all of that is combined within the enrollments table. So we'll need data from three different touch points in order to form or aggregate output. And so for me, Ground Zero is our enrollments table.
So I select star from enrollments and then I start adding on what I need. So I know I want students details. So I'm just going to energize the students table. Alright, and having done that inner join on the students table, and I'm going to also Have the inner join the courses table. All right now I have a full party on my hands. So we have data coming in from all three tables, enrollment students and courses.
And no, we can start filtering on what is most important for us to have a sense of the output. So we want to see the total number of credits that each student is doing. So, of course, we will know we need the credit counts, so see that number of credits. And we'll also probably want the student name since we already energized so I'll just go ahead and fill in our column for the full name. Alright, so I just amended my query to only bring back the number of credits and the student's full name and of course, we can just add pretty titles to those. Now the next thing I want to do of course, is to group so I will just group by and what we want to buy is any column that We have included in our select.
So I have to group by first name, after group by last name. And I'll just copy and paste and I'm in a split in the comma separation. And I have to group by the number of credits. Now you see that this, this has reduced because maybe our team is taking more than one three credit courses. And so it's shown to them together in proper and true group by fashion. But the fact is that all work is not complete, because what we want to do is find a total number of credits per student.
And so we'll have to employ our aggregate function called sum, which actually says that for all of those that you're grouping, give me the total so I can just grab the number of credits, inside sum. And if you were kicking and screaming when I included the number of credits down here in the group by then you were right. So we We're about to do the aggregates on number of credits, I don't need to include number of credits in the group by actually I shouldn't include it in the Roubaix. And so this query will actually give me how many credits Judy's doing, how many credits petroi is doing, and etc, etc. And all of this is based on the enrollments table because the enrollments table is keeping track of which classes each one is doing. So the group bys are seeing that each student and grouping them and I am summing up the additional data, which in this case is the number of credits and then bringing back that total.
And that is essentially how the sum aggregate function works. I hope you have fun with it and experiment with it. Bring up another scenario, the script file is already included with this video. And based on the data that you have or how you design your database, feel free to experiment and use it