Left, Right and Full Joins

Microsoft SQL Server 2017 for Everyone Relationships and Foreign Keys
10 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 picking up where we left off last time about inner joints. And we'll be looking at other types of joins that we can do between tables, namely left joins and right joins. Now before I proceed, I just want us to review some of the design decisions that we made on our enrollments table. And those would be surrounding the necessity of values for the foreign key values. You'll recall that we allowed notes on each of our foreign keys and of course, those design decisions can be changed, because an enrollment really shouldn't occur if a student and of course don't go together. However, in practical terms, there can be an enrollment of students in a course, but no teacher being assigned to that enrollment just yet.

And so in keeping with that design decision, I actually added a few more rows a few more rows of data to our Courses table, teacher table and the enrollments table. So here you see that I selected all from enrollments and I have a few columns down to the end where the teacher ID is actually no. But we have students being assigned to course with ID seven, just no lecture being assigned to that course just yet. I have included scripts that will insert additional courses and additional teachers into the database so you can go ahead, I also made a design change, I changed teacher to teachers, that's the name of a table teacher to teachers as I lift up, so I made that part and so you can feel free to change that design or just modify the script according to what you have. Now let's take a look at the concept of left and right giants.

Now once again, let's take a look at our SELECT statement on our enrollment. Instead, which is bringing back every single enrollment that is currently in the system Well, at least the top 1000. Since we have fewer than 1000, then we're getting about all of them. And we see here that the between IDs 17 through 21, there would be no lecturer or teacher assigned to those enrollments. Now here I've written an inner join statement, which is going to bring back all the columns between the two tables, enrollments and teachers. That's what the star means, if you remember.

And the expectation is that the inner join is actually just going to bring back on either side where the condition is met, meaning the teacher ID in the enrollment stable and I'm using that aliases, EMT, II for enrollments, D for teachers. So he dot teacher ID most exactly much the ID in the teacher table. And if that condition is not met, then that rule will not be returned. And so the expectation is that When we execute this query the rows 17 through 21, or IDs 17 through 21. And enrollments will not be returned because there is no teacher in the teachers table with an ID of null. So no will not match the any of those values.

So I press f5. And here we see that both query results are here stucked. And in the top result, we see that we're bringing back 17 and a total of 17 rows for the enrollments. However, in the lower stock, we're seeing that we're only bringing back 12, which clearly means that there were admissions as we went along. So columns 17 through 21, or enrollments 17 through 21. Were not returned because they did not meet the minimum condition.

Now in a scenario where you want it to bring back every single enrollment, regardless of the presence of the data, too much this exact condition, that's where your left join comes in. So the left joins really easy. It's just a modification of a typical INNER JOIN where instead of seeing inner, you literally just say left. And what happens here is that it will bring back every single thing on the left side of the query. And I'm seeing the left side of the query, because if you read from left to right, and I'll just put this all in one line, then you see that the enrollments table is to the left of the giant. And the teacher said this to the right of the giant.

So the left join is saying bring back every single rule every single row of data from the table that's on the left. And if there is a much then you can bring back any matching data on the table and condition on the right. But even if there is no matching data, still bring back that column on the left. So when I execute this query, the expectation is that this will bring back every single enrollments, just that there should be a few more nodes in our results. So that's Execute and see what happens. All right, so I wrote an entirely different statement for our left joints.

Now we have three queries being run in this script alone, each one being displayed here in its own stack. So up top, we have Select star from enrollment, so we know that enrollments would have about five more rows. And below we have the inner join data set, which should only have about 12. So this one is only bringing back the exact matches. But then the last one, which would be the result set, from our left join, would actually have every single enrollments. Alright, so we do have enrollments and see we're bringing back IDs 17 through 21 with the enrollment details, but because there is a null value in the teacher ID, there is a no match because well, it didn't bring back the corresponding data from our teacher table.

So that's the power for left join. You may be faced with a situation where you know you need by Everything from one side, but then you need additional data if it's available. But the table, the main table is absolutely important. And that would be the situation the enrollments are absolutely important. Maybe the administration wants to see how many students have enrolled in that class, regardless of if they have a lecture or not. So this query would actually bring back every single enrollment that exists regardless of the presence of a teacher.

The reverse may also be true. So we're right now we're catering to the table on the left. And we could always swap this out, we could put teachers on the left side and the words and T and enrollments on the right side. And that would actually bring back every single teacher, regardless of if they are currently assigned to an enrollment or to a class. But instead of doing all of that, and that switching, we could actually just use our right join, which would basically do the reverse of the left Join, where it would bring back everything in the table to the right of the join. And regardless of if it has matching data in the table to the left, so here I've written a select statement with our right join instead.

So this time the bias is on the teachers table where you want to see every single teacher know I included some teachers, and certain some more teachers, not all of them have been assigned to an enrollment. So the expectation is that we're going to see every single teacher and an enrollment if it exists, and if it doesn't exist, then the enrollments would be blank. So once again, I have all of my queries here stuck to have my Select star from enrollments up top, I have my inner join here, I have my left join here. And then when I go down, I see that I have my right here. And if I scroll through, we see that the lecture details do repeat for each enrollment for which they are associated, right? So everywhere there's ID two in an enrollment, that's where john McDonald is.

However, we can see here that these three lectures that were just added, were not assigned to any class. So we bring back all of the lectures on the right. Alright, so remember, we're using right join. So the teacher still is to the right, so we're bringing back every single lecture. And if there's an enrollment and we see the details, if not, then everything is not. And you may note also, that the that the enrollments that we're not given a teacher ID, and I'll just go back quickly, these enrollments with IDs 17 through 21.

They're not being returned here because once again, the right join is giving the bias to the teacher to the teachers table, which is on the right, so the left join gives by us which of our tables on the left bringing back everyone single record regardless of if there is a match or not, and the right join basically does the opposite. So it brings back everything to the right regardless of if there is a much for the data that's on the left. The final statement I will show you is the full join, which basically says, I want everything on either side. If there's a much, then bring me back one rule with all the details. If there is no much, then bring me back from either side regardless, and that is what the full joint does. So we have the left, we have the right, and we have the full And let's not forget or enter.

So enter sifts through and make sure that there's an exact much left says if there's an exact match, then that's fine, but bring me everything to the left side of my query. Right join does the opposite of left join. What was right isn't that what was left isn't all right. And the full join says I don't care which direction you are. Just bring the buff and if there's a And then fine. And so the last results that we're looking at below actually depicts what happens during the full join.

So we see here that all of the roles that would have been missing in the inner join or left join, right join, they're all being brought back right here. So are lectures without an abiding city and our enrollments without the lecture, and all enrollments that have lectures assigned or teachers assigned, everything is going back and that's the power of the full join. So you can go ahead and play around with this. The three script files have been included in the resources for this video. 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.