SQL SUM Aggregate Function

5 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.63
List Price:  €92.33
You save:  €27.70
£53.93
List Price:  £77.05
You save:  £23.11
CA$96.81
List Price:  CA$138.30
You save:  CA$41.49
A$104.70
List Price:  A$149.58
You save:  A$44.88
S$92.07
List Price:  S$131.53
You save:  S$39.46
HK$544.08
List Price:  HK$777.30
You save:  HK$233.21
CHF 60.58
List Price:  CHF 86.55
You save:  CHF 25.96
NOK kr764.18
List Price:  NOK kr1,091.73
You save:  NOK kr327.55
DKK kr482.01
List Price:  DKK kr688.62
You save:  DKK kr206.60
NZ$115.61
List Price:  NZ$165.17
You save:  NZ$49.55
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,388.04
List Price:  ৳11,983.43
You save:  ৳3,595.39
₹5,884.63
List Price:  ₹8,406.97
You save:  ₹2,522.34
RM302.91
List Price:  RM432.75
You save:  RM129.84
₦114,885.78
List Price:  ₦164,129.58
You save:  ₦49,243.80
₨19,488.65
List Price:  ₨27,842.12
You save:  ₨8,353.47
฿2,343.26
List Price:  ฿3,347.66
You save:  ฿1,004.40
₺2,397.42
List Price:  ₺3,425.04
You save:  ₺1,027.61
B$398.18
List Price:  B$568.86
You save:  B$170.67
R1,230.46
List Price:  R1,757.88
You save:  R527.41
Лв126.45
List Price:  Лв180.65
You save:  Лв54.20
₩96,511.99
List Price:  ₩137,880.18
You save:  ₩41,368.19
₪264.63
List Price:  ₪378.06
You save:  ₪113.43
₱4,048.60
List Price:  ₱5,783.97
You save:  ₱1,735.36
¥10,557.68
List Price:  ¥15,083.05
You save:  ¥4,525.37
MX$1,394.68
List Price:  MX$1,992.49
You save:  MX$597.80
QR255.99
List Price:  QR365.72
You save:  QR109.72
P936.52
List Price:  P1,337.94
You save:  P401.42
KSh9,054.60
List Price:  KSh12,935.70
You save:  KSh3,881.10
E£3,408.49
List Price:  E£4,869.48
You save:  E£1,460.99
ብር8,438.78
List Price:  ብር12,055.92
You save:  ብር3,617.13
Kz63,749.24
List Price:  Kz91,074.25
You save:  Kz27,325.01
CLP$66,647.97
List Price:  CLP$95,215.47
You save:  CLP$28,567.50
CN¥498.38
List Price:  CN¥712
You save:  CN¥213.62
RD$4,222.81
List Price:  RD$6,032.85
You save:  RD$1,810.03
DA9,343.17
List Price:  DA13,347.96
You save:  DA4,004.79
FJ$156.30
List Price:  FJ$223.29
You save:  FJ$66.99
Q542.76
List Price:  Q775.41
You save:  Q232.64
GY$14,678.27
List Price:  GY$20,969.86
You save:  GY$6,291.58
ISK kr9,637.62
List Price:  ISK kr13,768.62
You save:  ISK kr4,131
DH695.98
List Price:  DH994.30
You save:  DH298.32
L1,257.18
List Price:  L1,796.04
You save:  L538.86
ден3,983.20
List Price:  ден5,690.53
You save:  ден1,707.33
MOP$561.93
List Price:  MOP$802.79
You save:  MOP$240.86
N$1,236.98
List Price:  N$1,767.20
You save:  N$530.21
C$2,583.15
List Price:  C$3,690.37
You save:  C$1,107.22
रु9,441.16
List Price:  रु13,487.95
You save:  रु4,046.79
S/263.44
List Price:  S/376.36
You save:  S/112.92
K276.43
List Price:  K394.91
You save:  K118.48
SAR262.85
List Price:  SAR375.51
You save:  SAR112.66
ZK1,872.33
List Price:  ZK2,674.87
You save:  ZK802.54
L321.47
List Price:  L459.26
You save:  L137.79
Kč1,633.58
List Price:  Kč2,333.78
You save:  Kč700.20
Ft25,900.67
List Price:  Ft37,002.55
You save:  Ft11,101.87
SEK kr737.56
List Price:  SEK kr1,053.70
You save:  SEK kr316.14
ARS$68,817.98
List Price:  ARS$98,315.62
You save:  ARS$29,497.63
Bs486.08
List Price:  Bs694.43
You save:  Bs208.35
COP$299,559.81
List Price:  COP$427,960.93
You save:  COP$128,401.11
₡36,093.90
List Price:  ₡51,564.93
You save:  ₡15,471.02
L1,748.14
List Price:  L2,497.45
You save:  L749.31
₲556,030.05
List Price:  ₲794,362.70
You save:  ₲238,332.64
$U2,909.40
List Price:  $U4,156.46
You save:  $U1,247.06
zł279.45
List Price:  zł399.23
You save:  zł119.78
Already have an account? Log In

Transcript

Hey guys, in this video we will look at SQL some function. The scenario that we're presented with today is to bring about the total number of credits that each student is doing. Now to get to this information, we will have to hop through a few tables starting with our enrollments table, which is tracking each student and what class they're doing. And then the classes table contains what course is associated with that class. And then by extension, our courses table has the details of that course and the number of credits, so we'll have to skip through a few tables to get this kind of information. But then the ultimate result should be that for any one students, we should see the total number of credits that they have, considering what they have been rostered for the semester.

So based on this data that I'm working with, we can expect that Students number seven and 21, at least should have more than three credits. With our sum function, we will actually see the end result of that. So I'm going to start off and use the school dB. And then I'm going to select, I'll just start with star from enrollments. All right. And then having done that we see okay, we're getting back all the enrollments.

And then we probably want to know the names of for students. So I'm going to enter join the students table firstly. And there we're bringing back all of the students that are currently taking classes. And then we probably want the details for the class to get the details for the courses. All right, I know that's an inner join the cast is on to the enrollment, Steve, that'd be even more details, but then I'm still lacking the details for courses. So I'm going to do one more INNER JOIN where I'm going to join our Courses table.

And I'll call it co on to our classes table. And now we're selecting all of the data possible across all four tables having inner joined an inner join. So these two are inner join the two enrollments and then courses is directly inter joined onto classes. And so the end result should be that we see one record for students, six tests six, and that should be six credits, because that's three plus three, and there was another student, or we weren't that's what essentially, we should only be seeing the maximum number of credits per student as opposed to seeing the drill of 33323. So let us refine our results set by process of elimination, what is it that we expect to see? I want to see the students name So I'm going to do a con cuts.

And I'm going to take the s dot last name and concatenate that with a comma on to s dot first on the score name. And I'm going to call this one student name. And then after that, I want to see the number of credits. So I'm going to see some and what am i assuming I'm summing the number of credits, column value, so that would be in my courses table. So that's c dot number of credits. And so I'm going to execute this and I have an error here.

Because I should have said SQL apologies. So I'm going to execute that. And we see that we're only getting back one student with some. So that's wrong. And that is wrong because we left off the fundamental part of our query, which is our group by. And what we're going to group by is, or are the columns last name and first name, because remember, whatever appears in our aggregate function, that column does not get grouped.

So we're going to just group by s dot last name, comma s dot first name. And then this will actually just aggregate everybody and show us who is doing which course how many credits based on the classes that they have been enrolled in. And so there are times you may have to jump through a number of fields to get to the results, but that is just the point of how SQL plays nicely All of these keywords on statements, they play nicely together, and they help you to formulate a query that gives you the results that you need in the moment. So I hope you enjoyed this lecture. Please feel free to play around with that and have fun

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.