Left, Right and Full Joins

Microsoft SQL Server 2017 for Everyone Relationships and Foreign Keys
10 minutes
Share the link to this page
You need to have access to the item to view this lesson.
One-time Fee
List Price:  $99.99
You save:  $30
List Price:  €92.18
You save:  €27.65
List Price:  £78.45
You save:  £23.53
List Price:  CA$136.63
You save:  CA$40.99
List Price:  A$150.66
You save:  A$45.20
List Price:  S$134.97
You save:  S$40.49
List Price:  HK$781.10
You save:  HK$234.35
CHF 63.92
List Price:  CHF 91.33
You save:  CHF 27.40
NOK kr736.32
List Price:  NOK kr1,051.93
You save:  NOK kr315.61
DKK kr481.39
List Price:  DKK kr687.74
You save:  DKK kr206.34
List Price:  NZ$163.04
You save:  NZ$48.91
List Price:  د.إ367.26
You save:  د.إ110.18
List Price:  ৳11,763.47
You save:  ৳3,529.39
List Price:  ₹8,335.70
You save:  ₹2,500.96
List Price:  RM470.30
You save:  RM141.10
List Price:  ₦142,731.72
You save:  ₦42,823.80
List Price:  ₨27,897.02
You save:  ₨8,369.94
List Price:  ฿3,675.65
You save:  ฿1,102.80
List Price:  ₺3,223.64
You save:  ₺967.19
List Price:  B$516.01
You save:  B$154.82
List Price:  R1,827.32
You save:  R548.25
List Price:  Лв180.25
You save:  Лв54.08
List Price:  ₩136,640.44
You save:  ₩40,996.23
List Price:  ₪369.26
You save:  ₪110.79
List Price:  ₱5,847.91
You save:  ₱1,754.54
List Price:  ¥15,721.81
You save:  ¥4,717.01
List Price:  MX$1,674.88
You save:  MX$502.51
List Price:  QR365.72
You save:  QR109.72
List Price:  P1,361
You save:  P408.34
List Price:  KSh13,198.68
You save:  KSh3,960
List Price:  E£4,729.59
You save:  E£1,419.01
List Price:  ብር5,759.79
You save:  ብር1,728.10
List Price:  Kz85,286.13
You save:  Kz25,588.40
List Price:  CLP$89,629.03
You save:  CLP$26,891.40
List Price:  CN¥711.16
You save:  CN¥213.37
List Price:  RD$5,907.53
You save:  RD$1,772.43
List Price:  DA13,457.26
You save:  DA4,037.58
List Price:  FJ$225.87
You save:  FJ$67.76
List Price:  Q778.50
You save:  Q233.57
List Price:  GY$20,983.69
You save:  GY$6,295.73
ISK kr9,608.22
List Price:  ISK kr13,726.62
You save:  ISK kr4,118.40
List Price:  DH995.96
You save:  DH298.82
List Price:  L1,768.33
You save:  L530.55
List Price:  ден5,676.49
You save:  ден1,703.11
List Price:  MOP$806.04
You save:  MOP$241.83
List Price:  N$1,843.10
You save:  N$552.98
List Price:  C$3,689.56
You save:  C$1,106.97
List Price:  रु13,341.39
You save:  रु4,002.81
List Price:  S/375.75
You save:  S/112.73
List Price:  K389.78
You save:  K116.94
List Price:  SAR375.03
You save:  SAR112.52
List Price:  ZK2,699.03
You save:  ZK809.79
List Price:  L458.70
You save:  L137.62
List Price:  Kč2,277.26
You save:  Kč683.24
List Price:  Ft35,631.69
You save:  Ft10,690.57
SEK kr742.57
List Price:  SEK kr1,060.86
You save:  SEK kr318.29
List Price:  ARS$89,340.66
You save:  ARS$26,804.88
List Price:  Bs692.46
You save:  Bs207.76
List Price:  COP$388,094.15
You save:  COP$116,439.89
List Price:  ₡51,546.97
You save:  ₡15,465.64
List Price:  L2,477.09
You save:  L743.20
List Price:  ₲754,796.40
You save:  ₲226,461.56
List Price:  $U3,856.05
You save:  $U1,156.93
List Price:  zł392.34
You save:  zł117.71
Already have an account? Log In


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 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.