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
€64.99
List Price:  €92.84
You save:  €27.85
£55.80
List Price:  £79.72
You save:  £23.91
CA$95.74
List Price:  CA$136.78
You save:  CA$41.04
A$105.97
List Price:  A$151.39
You save:  A$45.42
S$94.71
List Price:  S$135.31
You save:  S$40.59
HK$547.38
List Price:  HK$782
You save:  HK$234.62
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr760.18
List Price:  NOK kr1,086.02
You save:  NOK kr325.83
DKK kr484.74
List Price:  DKK kr692.51
You save:  DKK kr207.77
NZ$116.49
List Price:  NZ$166.43
You save:  NZ$49.93
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.15
List Price:  ৳10,970.69
You save:  ৳3,291.53
₹5,844.24
List Price:  ₹8,349.28
You save:  ₹2,505.03
RM331.61
List Price:  RM473.75
You save:  RM142.14
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,466.20
List Price:  ₨27,810.05
You save:  ₨8,343.85
฿2,579.91
List Price:  ฿3,685.75
You save:  ฿1,105.83
₺2,258.19
List Price:  ₺3,226.13
You save:  ₺967.93
B$355.28
List Price:  B$507.56
You save:  B$152.28
R1,291.06
List Price:  R1,844.45
You save:  R553.39
Лв127.20
List Price:  Лв181.73
You save:  Лв54.52
₩95,163.27
List Price:  ₩135,953.36
You save:  ₩40,790.08
₪260.34
List Price:  ₪371.93
You save:  ₪111.59
₱4,006.12
List Price:  ₱5,723.27
You save:  ₱1,717.15
¥10,811.89
List Price:  ¥15,446.23
You save:  ¥4,634.33
MX$1,180.56
List Price:  MX$1,686.59
You save:  MX$506.03
QR255.22
List Price:  QR364.61
You save:  QR109.39
P950.05
List Price:  P1,357.27
You save:  P407.22
KSh9,308.67
List Price:  KSh13,298.67
You save:  KSh3,990
E£3,339.92
List Price:  E£4,771.52
You save:  E£1,431.60
ብር4,017.22
List Price:  ብር5,739.13
You save:  ብር1,721.91
Kz58,559.69
List Price:  Kz83,660.29
You save:  Kz25,100.60
CLP$65,083
List Price:  CLP$92,979.70
You save:  CLP$27,896.70
CN¥496.09
List Price:  CN¥708.73
You save:  CN¥212.64
RD$4,059.13
List Price:  RD$5,799
You save:  RD$1,739.87
DA9,404.13
List Price:  DA13,435.05
You save:  DA4,030.92
FJ$157.14
List Price:  FJ$224.49
You save:  FJ$67.35
Q543.86
List Price:  Q776.98
You save:  Q233.11
GY$14,638.84
List Price:  GY$20,913.53
You save:  GY$6,274.68
ISK kr9,768.50
List Price:  ISK kr13,955.60
You save:  ISK kr4,187.10
DH701.39
List Price:  DH1,002.03
You save:  DH300.64
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,006.46
List Price:  ден5,723.76
You save:  ден1,717.29
MOP$563.24
List Price:  MOP$804.66
You save:  MOP$241.42
N$1,288.69
List Price:  N$1,841.06
You save:  N$552.37
C$2,575.52
List Price:  C$3,679.48
You save:  C$1,103.95
रु9,351.66
List Price:  रु13,360.08
You save:  रु4,008.42
S/260.79
List Price:  S/372.58
You save:  S/111.78
K270.67
List Price:  K386.69
You save:  K116.01
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,901.47
List Price:  ZK2,716.50
You save:  ZK815.03
L323.43
List Price:  L462.07
You save:  L138.63
Kč1,625.98
List Price:  Kč2,322.93
You save:  Kč696.95
Ft25,280.20
List Price:  Ft36,116.11
You save:  Ft10,835.91
SEK kr759.20
List Price:  SEK kr1,084.61
You save:  SEK kr325.41
ARS$61,608.98
List Price:  ARS$88,016.60
You save:  ARS$26,407.62
Bs483.53
List Price:  Bs690.78
You save:  Bs207.25
COP$273,394.26
List Price:  COP$390,579.97
You save:  COP$117,185.71
₡35,797.53
List Price:  ₡51,141.53
You save:  ₡15,343.99
L1,728.55
List Price:  L2,469.47
You save:  L740.91
₲523,227.64
List Price:  ₲747,500.10
You save:  ₲224,272.45
$U2,673.53
List Price:  $U3,819.50
You save:  $U1,145.96
zł280.44
List Price:  zł400.65
You save:  zł120.20
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.