SQL AVG Aggregate Function

9 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 start looking at our aggregate function starting with the average function. So to take an average of the rules are of values in rosewood need some numerical values that would not be IDs. So for this example, and well, we would probably eventually have to add it anyway, we will be adding a column to our enrollments table for grades. So we would be adding grades the enrollments table because this is where we see that a particular student took a particular class. So the best place to put the grid for the students performance in said class would be in the same table where we know that they took that class. So I'm going to modify the enrollments table put in our grades column, and put in some dummy data and then we can proceed.

So to add an add a column to an existing table, we just hover over that tables name In the design viewer, click that arrange in the middle, which will bring up or designer and then we can just add that new column. Alright, so in adding this new column, I am calling it greed. I'm setting it as a float. And I'm setting it as nullable. So sometimes you when you're adding a rule, a new column, sorry, after having a few records, if you are not careful with setting invert null versus not nullable, you might run into some design flaw and problem because if it's not nullable, and you already have 100 Records, then you're going to be attempting to insert a new column that shouldn't be no against 100 existing columns with no preset data. So you want to be very careful with that.

Maybe you want to start off as null, fill in the the records retroactivity and then make it not no going forward. So far, no, I just said it's to be knowledgeable and Having me that change, I'll just click Apply. And then it will run an ALTER TABLE statement, which is the SQL statement that is used to modify a table like when you're adding a column, or you're adding a key retroactively or doing some operation that is morphing, the, the structure of your table. So I just go ahead and click Apply. And that was successful. And I'll just go back to my design viewer and execute once again.

And then I'll just add a few grids here. Alright, so we've added some grades. And having done all that I just click apply and allow it to run that update command setting the grids where necessary, and that was successful. And now we have some data against which we can do a few computations. Now we have our task ahead of us we were asked to bring by the average grades per course. No once again presses elimination, we know that we need courses.

So I can select. Well actually, let's start with enrollments because it's average grade per course. So, enrollments is what really has the grades. So let's start with enrollments select. And I'll say start from enrollments. Alright.

And let me just add my use school underscore dB. So that is our first statement. And we can execute that. And we see that we're bringing back all enrollments. Now we wanted to know, the average grade. For course, based on that objective, I really don't need the course Well, I need the course name because you're seeing of class ID here.

I don't know what course is associated with the class ID five. So that means I'm going to have to inner join my glasses table. So I just read that quickly. And now Start using my NSS. So em for enrollments and seal for classes, and then co.id must be equal to n dot class underscore ID. And when I execute this, then I bring about more details more numbers, though.

But I'm really interested in maybe the course name. So I'm going to have to see, I want, whichever, whichever course has the ID of five. Let me let me take a second to look at these IDs because this is no confusing. But you see here where we have ID and we have ID. All right, yeah. So we're back on track.

So our courses ID, here are our classes ID here is being energized on the class ID here. So that's why we have 5555555 and six on one, but then we're really interested in the courses because we will Want to see the name of the course, as well as the average grid. So I'm just going to duplicate this line and change this out to courses. And I'm going to call this one SEO, and I'm going to see seo.id. Or Well, that would be course ID, and there is the IntelliSense helping me along. So we just do that quickly.

So that's course ID being equal to c l dot courses ID. All right, and then once we do that, then we can bring back more details. And we see here internet authoring is a very popular course this term, and we have website development and data mining. Now the objective once again, is to bring about the average grades per course, which means that I want to see one rule seeing internet authoring and the average grid for that course. And I want to see one rule with website development and wonder with the demand grid Did we only have one row of those right now. But essentially, we shouldn't be seeing this many instances of internet authoring by the end of this query.

So now I just focus on all of the columns that I know I want back. So I want back the course title, which in this case, would be SEO dots title. And I also want back and I just space those out so we can see exactly what's happening here. And I also want back my grade, which would be n dot agreed. All right, and then when I select once again, now I'm seeing that these are the grants for internet authoring and other courses. But once again, this should all be one value, which would represent the average grade for that class.

And well, the average would this would be 87, on average for this will be 34. Now if you're a c++ developer or any other type of developer, you would probably go into a for loop or a while loop mode, where you'd rather statement for this board in SQL, they gave us a command because you can see where it's not quite like the other languages. So we actually have the opportunity to use a function a VG and wrap that grade value inside a VG and a VG will say, I want to average for whatever comes in here. So when we take a look at the results set, we see that we're going to get about one course with some average value, but we had three courses, and we should have at least three rules. And the reason for this is that the Photoshop ABG is not functioning properly because we are not grouping by so I'm going to add a group by and once again, we will no group by whatever we're selecting, but in this case, because we're also selecting a value that we're putting in the aggregate function ABG we won't include this one.

In our group by, so when we're using one of these functions, we don't include that column in our group by. And then if I execute this once again, then we see better values coming back where we see internet offering with its real, average, and website development with those values. Of course, if we add more values for website development and data mining, then we would get different values than the one greed that is available to us. But otherwise, that is how our ABG function works. As always, we can beautify our columns by adding our aliases and we can see course, title, and course average or average grade. And then we re execute and there we have our report, which we can then export and share with our superiors.

Thanks for viewing and have fun with that.

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.