Run Queries on Related Tables (Inner Joins)

MySQL Database Development Mastery Relationships and Foreign Keys
15 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.28
List Price:  €96.12
You save:  €28.84
£55.84
List Price:  £79.78
You save:  £23.93
CA$100.66
List Price:  CA$143.81
You save:  CA$43.15
A$112.30
List Price:  A$160.44
You save:  A$48.13
S$95.06
List Price:  S$135.81
You save:  S$40.74
HK$543.65
List Price:  HK$776.68
You save:  HK$233.03
CHF 62.95
List Price:  CHF 89.94
You save:  CHF 26.98
NOK kr796.08
List Price:  NOK kr1,137.30
You save:  NOK kr341.22
DKK kr501.96
List Price:  DKK kr717.11
You save:  DKK kr215.15
NZ$124.09
List Price:  NZ$177.29
You save:  NZ$53.19
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,350.53
List Price:  ৳11,929.84
You save:  ৳3,579.31
₹5,966.48
List Price:  ₹8,523.91
You save:  ₹2,557.43
RM312.78
List Price:  RM446.85
You save:  RM134.07
₦108,176.54
List Price:  ₦154,544.54
You save:  ₦46,368
₨19,454.09
List Price:  ₨27,792.76
You save:  ₨8,338.66
฿2,392.25
List Price:  ฿3,417.65
You save:  ฿1,025.40
₺2,467.97
List Price:  ₺3,525.82
You save:  ₺1,057.85
B$472.19
List Price:  B$674.59
You save:  B$202.39
R1,303.83
List Price:  R1,862.70
You save:  R558.86
Лв131.65
List Price:  Лв188.08
You save:  Лв56.43
₩102,536.89
List Price:  ₩146,487.56
You save:  ₩43,950.66
₪256.46
List Price:  ₪366.39
You save:  ₪109.93
₱4,057
List Price:  ₱5,795.97
You save:  ₱1,738.96
¥11,019.26
List Price:  ¥15,742.47
You save:  ¥4,723.21
MX$1,411.83
List Price:  MX$2,016.98
You save:  MX$605.15
QR254.14
List Price:  QR363.08
You save:  QR108.93
P970.50
List Price:  P1,386.50
You save:  P415.99
KSh9,031.50
List Price:  KSh12,902.70
You save:  KSh3,871.20
E£3,562.42
List Price:  E£5,089.40
You save:  E£1,526.97
ብር8,897.26
List Price:  ብር12,710.92
You save:  ብር3,813.65
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$69,297.02
List Price:  CLP$99,000
You save:  CLP$29,702.97
CN¥510.85
List Price:  CN¥729.81
You save:  CN¥218.96
RD$4,256.60
List Price:  RD$6,081.12
You save:  RD$1,824.52
DA9,464.46
List Price:  DA13,521.24
You save:  DA4,056.78
FJ$162.28
List Price:  FJ$231.84
You save:  FJ$69.55
Q538.26
List Price:  Q768.97
You save:  Q230.71
GY$14,619.81
List Price:  GY$20,886.35
You save:  GY$6,266.53
ISK kr9,762.20
List Price:  ISK kr13,946.60
You save:  ISK kr4,184.40
DH704.68
List Price:  DH1,006.73
You save:  DH302.05
L1,286.35
List Price:  L1,837.72
You save:  L551.37
ден4,141.74
List Price:  ден5,917.03
You save:  ден1,775.28
MOP$559.01
List Price:  MOP$798.63
You save:  MOP$239.61
N$1,299.34
List Price:  N$1,856.28
You save:  N$556.93
C$2,571.30
List Price:  C$3,673.45
You save:  C$1,102.14
रु9,517.06
List Price:  रु13,596.38
You save:  रु4,079.32
S/260.20
List Price:  S/371.74
You save:  S/111.53
K283.61
List Price:  K405.18
You save:  K121.56
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,933.89
List Price:  ZK2,762.82
You save:  ZK828.92
L334.80
List Price:  L478.31
You save:  L143.50
Kč1,689.59
List Price:  Kč2,413.80
You save:  Kč724.21
Ft27,569.95
List Price:  Ft39,387.34
You save:  Ft11,817.38
SEK kr774.42
List Price:  SEK kr1,106.36
You save:  SEK kr331.94
ARS$71,763.27
List Price:  ARS$102,523.35
You save:  ARS$30,760.08
Bs482.86
List Price:  Bs689.84
You save:  Bs206.97
COP$309,145.61
List Price:  COP$441,655.52
You save:  COP$132,509.90
₡35,480.70
List Price:  ₡50,688.88
You save:  ₡15,208.18
L1,775.44
List Price:  L2,536.46
You save:  L761.01
₲544,980.94
List Price:  ₲778,577.57
You save:  ₲233,596.63
$U3,110.44
List Price:  $U4,443.67
You save:  $U1,333.23
zł287
List Price:  zł410.02
You save:  zł123.01
Already have an account? Log In

Transcript

Hey guys, in this video we will be looking at how we go about querying from multiple tables that are related. Oh as by way of quick recap, we just entered some records into our enrollments and our classes tables. And these both these tables are related to at least one or two of the base tables, which are students, lectures and courses. So classes is related to lectures and courses depicted by the names that we gave our foreign key columns, all right, and then enrollments is related to classes as well as students. Alright, so now we will start looking at how we can look at the details of the classes. So looking at a table here, just just that, that it's kind of difficult to decipher who will lecture one who lecture three is and which course is they are teaching because we may not be in a position to memorize these IDs.

So we want to look at how exactly we will go about bringing about the details associated with each referenced record. So stay in this script file, which already has the part the parts of the query that's saying Select star from school or DB classes, school DB classes, sorry. And so we'll just expand on this. And we're going to start looking at a new keyword or a new concept in databases, which is called joins, because essentially, we need to join on the related records from the respective tables to this table. So the base of the queries classes, and we're going to be joining on the lecture details and joining on the courses details. All right.

So we can just go to the next line and remember you always immediately Hold on, I'll just remove it for now. So it doesn't get in the way. But we go to the next line. And then we introduce a new keyword, which is inner join. Now, let me say that you have different types of joins. And we'll get into the other types of joins later on.

But you also have different ways to accomplish this kind of join. So I'm going to teach you a very standard way which you can actually apply to almost any other relational database engine, because the reality is that each one has its own, you know, quirks in its syntax. There's certain things that my SQL does that Microsoft SQL is a different way that PostgreSQL does another way. So what then this inner join technique is fairly standard across all the platforms. Okay, so following or inner join. I'm going to go very slowly here so that we can understand exactly what's going on.

He states the table that we want to join. So we have one table which I The information we want, but then we need the details. So what table contains the details that we see. So the first table that we would want details from would probably be our lecturers table, because we want to see who this lecture is. So I can just say inner join. lecturers reference the table by name.

And of course, we're not using a using statement up top. So if I use a table, just like that, I have to put on school D underscore dv dot and you see the intelligence is actually helping us there. Any hole, we say INNER JOIN, and then we stay at our table. And then we see on so that's another key word. So INNER JOIN table name on and then we state the condition that the two tables must be joined on. I like to say what do the two tables have in common, what two values do These two tables have in common that they should be joined on.

So simply put, it would be the primary key, sorry, the primary key value of the joining table onto the foreign key value of the next table. All right, so in this case, the foreign key, which is lecture, Id must have a matching value with the primary key column in our lectures table. So these values are what they have in common. And so these columns are what they have in common. So what I'm going to do is say inner join this table on the condition that the ID from classes and I'm going to have to spell this out a bit. And you can see how tedious he can get when you're not using you're using statements, especially when more tables are getting involved, have to be seen database dot table name and then dot again, to get to That ID.

All right, well, then that's not it, it's classy some inner joining on lecture ID being equal to, and then I'm going to have to do the same again, school DB dot, lecturers dot, and then it would be the ID. So once again, I'm joining these two tables on the condition that the lecture ID value that is present in the classes table matches is equal to the primary key ID value in the lecturers table. Now, please know we have six classes listed here. And when I execute this query, you're going to notice that we will only have three and I want to do it and then explain why. So we're down to three. And you see, because I'm selecting star, it's selecting all columns from our classes table.

It's selecting all columns from any table that is being joined on. And so we have all columns. that were there before. And now we have more columns coming back from our lectures table. And if you look at the values that are in the lecture ID in from the classes set up columns versus the values in the ID column coming back from the lectures table, you'll notice that they're the same. And so that would kind of inform why we have three fewer rules coming about than we had before.

And that's because remember that we had no in those lecture ID columns. And I'll just quickly re execute that. Oh, apologies. Let me just read type query. All right, there we go. So we have the results.

That's like Star from classes. And notice we had six three off which had no lecture ID. And so because our condition for joining the two tables, states that both values must be the same, they must be equal. There is no lecturer with an ID as no in our lecturer stable So those three would have been almost emitted from our inner join operation. Alright, and so when we go back to our results, we only have those three with the names and the details off each lecture. So now I have the details on the ledgers, but I'm still missing the details on the course.

So I'm going to have to repeat this line. All right, and I'm going to have the inner join once again. And if you just go to the line and press control, well hold on Ctrl and press D, it actually duplicates that line for you. So that's what I just did. And then we can just go ahead and modify. But then you see that as we start, you know, joining, joining and joining, the more tables that gets into this query is the more I would say messy it looks.

So SQL actually gives us the ability and the opportunity to use what we call aliases. And we would have gone through that when we're looking at simple select queries, but we would have looked at them relative to the columns, you can ask Use aliases on your table names also. So I could call classes instead of writing school DB classes every time I can just see see, all right, and then instead of writing school.db dot lectures, school DB dot lecturers, every time I can just say l or let me let me do it a bit better, so I can see class, and I can see elect. Alright, and then instead of writing all of this, once again, I can just replace all of this part that references the table and just see class. Dogs dots, lecture ID, and I can just see lect.id. So now instead of having to write out, especially since I'm not once again using the using statement, I can replace all of this with just a reference to an alias and then use this in this in the referencing elsewhere.

So I'm going to rewrite this last INNER JOIN statement for our classes using this technique. So I'm going to inner join our inner joining classes with to see Oh, sorry, I'm going to joining courses. So courses, and I'm going to use my ID. So I'm just going to use this you can use, of course, a more descriptive expression in your aliases or it could be very abstract, but of course, you can't have C here and see here that would cause a conflict right so just be very Just be careful with that. So we have courses core and then I'm seeing that I want to enter join class dot courses ID and see the intelligence is helping me along because intelligence knows that class represents classes and inner joining that on core dots, and I believe this would be course ID. All right.

Is it course it is Images double check. Yes, course I There we go. All right. And so and once again, we're still selecting star. So when I execute this, it will bring back Oh, so when you have multiple queries, it will actually give you this result pane with more than one. So results seven is the first one.

And we see here that we're getting back all of our class details, we're getting back all of our lecture details, and we're getting back all of the course details. Alright, so this can come in handy when you are charged with furnishing a report. Maybe your instruction was select all the scheduled classes that have a lecture. And so the inner join is a nice way to just eliminate any null values that may appear. So in this case, we know that lectures can be no if a lecture is no then there's no lecture assigned. So we can just use the inner join, which does does an inherent elimination of that that missing value.

So no, you won't To start tweaking our results set a bit because we have the details that we need. So we really don't need to be seeing lecture ID courses ID, the primary key columns and so on. So I'm just going to tweak the select statement, and I'm going to start selecting the columns that I know are absolutely necessary. So I would want maybe the lecturers name. And then we see we have last underscore name first underscore name for the lectures, but we did look at it earlier. And we saw that we could use concat, which would allow us to see the left dots last underscore name, and I'm going to concatenate that onto a space and bring back and I'll just put a comma there since its last name, first name.

So left dot first name Then I'm going to give this the DNS name. And remember we use our apostrophe, lecturer, full name. All right. So we bring back the lecturers full name, we probably don't need the degree in this in this report. So we just want the lecture. And we want the title of the course.

So I could see core dot title. So I will be notice what's going on here. Because I gave these tables a ss, wherever they were referenced, I can no use those it is is to make sure that I'm getting the cards column from the correct table because if we had students being joined into the square will also have last name and first name, then it would be ambiguous if we only said last name and first name. So by seeing elect dot last name, or if I had students here and I said call that one student, by seeing students or sorry, it's still that last name, we know exactly which table the values should be coming from. Alright, so core, the title, and I'm going to rename this one as course title. And we probably don't need the course core, but we could use a number of credits.

So it's core dotnet core scored. And of course, if it's the only column of its kind, like core score is only column with that name, so it wouldn't be any harm nots referencing it by the eighth. So what I like to do that because if I look back at the query, I can tell exactly where what is coming from. So I just stick to that evil that I know. And I say course code. Alright, sorry, we were said credits, we said number of credits, actually.

So it's number of credits, and that is as number of credits. Alright, and so we can execute this query one Again, and then we'd see that the details sorry, though the query is being run, the details that were once there are no eliminated as well as the obstructs foreign key values, they're all eliminated. And that while they're not going back in the query, they're still playing a vital role in the background pulling the strings are loading the joints. And then once all of the data is, is present and available to us, we're simply selecting the columns that we're absolutely interested in. Alright, so I'm going to share this and I'm going to challenge you. And I'm going to leave this here I'll upload this with the with the quote, of course, but I'm going to challenge you to write your own inner join queries for enrollments to bring back all the details on the course the time So remember, class ID is referencing classes and classes has references to these guys.

So I would want To to bring back the students and the name of the course who is teaching this course and the time of day that this course our class or other will be so always teaching this class or classes or courses it and what time is it and who is the students. So that's my challenge to you. So happy hunting.

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.