Run Queries on Related Tables (Inner Joins)

Microsoft SQL Server 2017 for Everyone Relationships and Foreign Keys
19 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.70
List Price:  €92.43
You save:  €27.73
£55.05
List Price:  £78.64
You save:  £23.59
CA$95.98
List Price:  CA$137.12
You save:  CA$41.14
A$105.75
List Price:  A$151.09
You save:  A$45.33
S$94.56
List Price:  S$135.09
You save:  S$40.53
HK$547.16
List Price:  HK$781.70
You save:  HK$234.53
CHF 63.45
List Price:  CHF 90.65
You save:  CHF 27.20
NOK kr738.30
List Price:  NOK kr1,054.77
You save:  NOK kr316.46
DKK kr482.62
List Price:  DKK kr689.49
You save:  DKK kr206.87
NZ$114.62
List Price:  NZ$163.75
You save:  NZ$49.13
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,222.07
List Price:  ৳11,746.32
You save:  ৳3,524.24
₹5,828.55
List Price:  ₹8,326.85
You save:  ₹2,498.30
RM329.23
List Price:  RM470.35
You save:  RM141.12
₦97,690.64
List Price:  ₦139,564.04
You save:  ₦41,873.40
₨19,499.79
List Price:  ₨27,858.04
You save:  ₨8,358.25
฿2,572.30
List Price:  ฿3,674.88
You save:  ฿1,102.57
₺2,260.88
List Price:  ₺3,229.98
You save:  ₺969.09
B$364.14
List Price:  B$520.22
You save:  B$156.08
R1,304.32
List Price:  R1,863.40
You save:  R559.07
Лв126.57
List Price:  Лв180.83
You save:  Лв54.25
₩96,278.96
List Price:  ₩137,547.26
You save:  ₩41,268.30
₪260.22
List Price:  ₪371.76
You save:  ₪111.54
₱4,095.18
List Price:  ₱5,850.51
You save:  ₱1,755.32
¥10,983.18
List Price:  ¥15,690.93
You save:  ¥4,707.75
MX$1,193.03
List Price:  MX$1,704.41
You save:  MX$511.37
QR256.47
List Price:  QR366.40
You save:  QR109.93
P952.83
List Price:  P1,361.25
You save:  P408.41
KSh9,133.69
List Price:  KSh13,048.69
You save:  KSh3,915
E£3,308.47
List Price:  E£4,726.59
You save:  E£1,418.12
ብር4,039.90
List Price:  ብር5,771.53
You save:  ብር1,731.63
Kz59,799.99
List Price:  Kz85,432.22
You save:  Kz25,632.23
CLP$63,667.80
List Price:  CLP$90,957.90
You save:  CLP$27,290.10
CN¥496.98
List Price:  CN¥710
You save:  CN¥213.02
RD$4,142.41
List Price:  RD$5,917.99
You save:  RD$1,775.57
DA9,432.20
List Price:  DA13,475.15
You save:  DA4,042.95
FJ$158.10
List Price:  FJ$225.87
You save:  FJ$67.76
Q544.32
List Price:  Q777.63
You save:  Q233.31
GY$14,658
List Price:  GY$20,940.90
You save:  GY$6,282.90
ISK kr9,646.72
List Price:  ISK kr13,781.62
You save:  ISK kr4,134.90
DH698.65
List Price:  DH998.11
You save:  DH299.46
L1,239.20
List Price:  L1,770.37
You save:  L531.16
ден3,980.20
List Price:  ден5,686.25
You save:  ден1,706.04
MOP$563.76
List Price:  MOP$805.41
You save:  MOP$241.64
N$1,284.80
List Price:  N$1,835.51
You save:  N$550.70
C$2,588.54
List Price:  C$3,698.08
You save:  C$1,109.53
रु9,341.71
List Price:  रु13,345.87
You save:  रु4,004.16
S/263.77
List Price:  S/376.83
You save:  S/113.06
K272.38
List Price:  K389.14
You save:  K116.75
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,910.82
List Price:  ZK2,729.87
You save:  ZK819.04
L321.97
List Price:  L459.98
You save:  L138
Kč1,603.41
List Price:  Kč2,290.69
You save:  Kč687.27
Ft25,203.86
List Price:  Ft36,007.06
You save:  Ft10,803.20
SEK kr743.98
List Price:  SEK kr1,062.87
You save:  SEK kr318.89
ARS$62,542
List Price:  ARS$89,349.55
You save:  ARS$26,807.54
Bs484.12
List Price:  Bs691.63
You save:  Bs207.51
COP$269,661.49
List Price:  COP$385,247.22
You save:  COP$115,585.72
₡36,378.56
List Price:  ₡51,971.60
You save:  ₡15,593.04
L1,737.15
List Price:  L2,481.76
You save:  L744.60
₲527,980.62
List Price:  ₲754,290.36
You save:  ₲226,309.74
$U2,698.07
List Price:  $U3,854.56
You save:  $U1,156.48
zł277.62
List Price:  zł396.62
You save:  zł118.99
Already have an account? Log In

Transcript

Hey guys, in this video we will be looking at some advanced querying techniques. So yes, we went through some select in previous videos and we went through some filtering and other more advanced topics. But then when it comes to having multiple tables that share data across the tables, we need to employ certain other SQL commands and functions in order in order to facilitate that kind of query. So I'm just going to go ahead and create a new query window. And we can select our database to be used, which is school. And of course, I always say use school above here in the script so that we can be absolutely sure, even if we move it to another computer that we're using the right database.

Alright, so let's start off by running a few selects. So select star from teacher and we have three to four tables. So I'm just going to I'll just Ctrl C and V and that will We'll actually duplicate this line. And I'm just going to run these sorry, not school courses, and students. Alright, so if I have these three commands and I say execute, it will actually execute all three and give us different results for each different result pins. And remember that you can always just highlight the one that you want to execute and it will give you just that.

Now I want to run a select star from my enrollments table. And we saw how messy and confusing that looked do that oh, sorry, I left over from that's embarrassing. So excuse me, and then we see that we have all of these numbers and literally they mean nothing to us sitting down and looking at them because all we have to do now is go back and we have to try and find who is teacher to. And then we go back and find out which student three and that backing and for thing is Really just tedious and absolutely not necessary. Because in SQL, we have a construct called joints. So we can do joins in our queries.

Alright, so this script file will be included in the resources for this video. And I'm going to show you how you can go about doing a join, join pretty much says, I want to literally join the data from two different tables based on the one thing that they have in common. And based on the fact that we just went through relationships and foreign keys and primary keys and those constraints, we can always assume that what the two tables have in common would be the value in the primary key and the value in the foreign key. All right, so the value in the foreign key column for enrollments is what is something that it has in common with the teacher table, right or in the student ID with the student table. Alright, so the syntax for Join is use a select so the Select part stays the same star from and you can choose your primary table enrollments since that's the real table, we want to see the details of all of these enrollments, we want to see which teacher is teaching this course.

So who is teacher to want to see the details of teacher two, on all the courses that teacher number two is teaching. So we select star from enrollments. And then we say, inner join. And you have multiple joins, you see inner, you'll see left, you see right, but for now, let's just do enter. And you'll say which table is is it that you want to join the data onto? Which table is it that you want to look for the details in?

So we want the details of the teacher? Id two. So obviously, the details of anything from teacher ID would be in our teacher table. So he's a teacher, and then we want to see on So join us On teacher table on the condition that which column from teacher column is ID, and remember that what they have in common is the value in the ID column of the teacher table. And the teacher ID column of enrollments table. So inner join this new table teacher on the condition that ID from teacher table.

And you can always see something like teacher dots just to make sure you're dealing with the right one in case both of them have the same name. All right, so you can see a teacher dots and then the IntelliSense is asking which column is it, and we'd already wrote ID is equal to and I can just say enrollments, dots, and I will say teacher ID. And then what this will do is say, give me everything from enrollments and also join on the details from the teacher table, where the ID matches that so it will bring back the details. So teacher to where teacher two matches the ID and teacher table will bring back all of the details for teacher three, where those two values match, etc. So that is why if we had 10 in the room and stabilize the teacher ID, it would cause a problem because we don't have those would never match.

Alright, so when I run this query, now we see and I said select star. Now we see that the details are coming back. So for teacher ID two, once it's two in the enrollments table, then it's bringing back the corresponding record for to where it's three. And then Romans is bringing about the corresponding records for three. And the same goes on and on. So teacher one, teacher one so we can know see which teacher is actually teaching these courses.

But we have more details that we need to see because we don't know the student IDs. We don't know the courses. So I can enter join us As many tables as I need to, so I can now go ahead and enter join. And let's say we want the student details alongside the teacher details. And then I can see the student ID. So the student ID is coming from the students table.

And I'm going to enter join that on the condition that students, so that's another thing you see how teacher ended up students, and then of course is, so it's good to just keep those things uniform. So that's, that's my design flaw right there. And then students.id is equal to and romans.io. And in this case, I want students student ID, and then I can execute this again. And what will happen is that you'll see know that the teachers details are still there. And then to the right, it's appending columns for the students.

So where their students ID three, were seeing the details for the students. Alright, I'm going to do it one more time. And at this point, it's probably less or more confusing, because at least we're seeing the details. No. But then we're seeing all of these irrelevant information because we don't need to see the IDs. And for institution, that is, we probably don't want to see the date join, and the date of birth of the students.

I know it's stuck on the course. And we see more possibly irrelevant data. So we'll go ahead and refine that as we go along. So I'm just going to, you know, join courses on and what do courses, what does the courses table have in common with our enrollments course ID, though, remember, I did say that it could get confusing when you have the same name across the table. so in this situation, it would be extremely good to see a courses dot course ID, so we know exactly which table this course ID is coming from versus which table the other one is coming from enrollments, dots, and then we can go ahead and do this and we use See, we're getting back all of the courses and the number of credits on the course score. So we're getting back all the details off everything.

So that's why I'm saying that we don't need to repeat the details in more than one tables, right? Because if we have all the details, or at least repetitions one place, we can always cross reference it with another table. And in a setting where you're selecting, then repetition is quite fine. What you don't want is a repetition when you're storing. All right. So you don't want to have john McDonald five different places in five different tables, you just have his details one place, and you make reference to him by his ID.

And so when you need to find more details on him, you just enter join that table with his details based on that foreign key primary key relationship that you would have set up so we can start refining this a bit. So I'm going to leave this query and I'm going to right Another one, where we're going to select the specific columns that we actually want for our report. Now, I did start off by saying we only wanted the courses being taught by john Gibson. So I want to bring about john McDonnell, sorry. So we only want to bring back where the teacher ID is two, we want to bring about the first and last name of the lecture, as well as the students first and last name, and the title of the course being talks. Alright, so that's all we want.

So we can actually go ahead, and I'll just copy these Inner Joins since I already have them here. But it would be good if you just re typed them for practice purposes. So I'm going to actually just go ahead and select that specific columns. And then this is where this query and kind of becomes more tedious because then you'll see that first name is present in two different tables. So we need to be very specific Which first name it is that we want. We're Alright.

So if we want to first name, last name, and then first name, last name, again, whose first name is whose last name is it the students or the teachers, those kind of things. So what is a common practice in database design is in ss, where when you have a table, you can actually just press space. So in this inner join line, here, I have teacher, I'm just going to press space. And I'm going to say something like T. All right, and students I'm going to call an S, and courses I'm going to call it C. And what this does is wherever we would have said teacher dot some column, we can actually just say, alias dot, that column, in this column, in this dot that column. So this makes writing the query much easier when when we when it comes to this situation, so we don't have to write Oh teacher dots first name, and then write student dot first name, we can just see t dots first name.

And sometimes IntelliSense may not necessarily chip in for this part. So I'm just going to run the first square with this. So select T dot first name from, and I'm just going to say enrollments. And even this can get them in this right here, I can just see E. Alright, so everywhere I had the word enrollments, I can replace it with E. And so this query just shrunk in size. Look at that. Look at the difference there.

Alright. So once you have a table, you can just press space and put on that alias. And then I'm just going to run this query quickly. So when I bring this back, I should only be seeing john or whatever is in the first name column, often lectures coming back, and there we go. So first name is what is coming back. And only the teacher first names because we did specify this alias and we're reusing it here.

I did say, let me just rerun this query. I did see that we would be bringing back the first and last name of The teacher. And then that no brings up another problem because if I'm only saying first name, last name, first name, last name, I still don't know who is so so I have to put an alias on the column itself so that it prints back. So teacher first name, and I'm just going to do this, I'm going to break the line and just do it in a cascading form. So teacher first name, comma, this will be the next one, Li Li, my core is being very readable. So it's good when you have readable queries.

And I can just duplicate this since a lot of this is going to repeat last name. So let's see the IntelliSense is chipping in no because it does realize that okay, T is the teacher table set as T right. So remember that as keyword we could have used as so last name, and they'll say last name here. And the reason for the square brackets is that because we have spaces in between what we want as a column headers SQL or any SQL, just take it literally as it is. Without that it would try to create certain keywords. Also, if someone saying don't use any keywords, just do this literally as we have it typed.

So have the teachers first name and last name. And we also want the students first name and last name. So as I said, we can just duplicate these lines because it will repeat. And we just refined so students student, this will be last name. And then finally, and remember we use commas for every column separation, a bunch of the last column. And finally, we want the course title.

So of course, would have the aliases, I can just see c dot title, and then we can give it a custom header Of course, title. And then this is a this is essentially how you go about running reports. You know you're working in an organization they ask you to pull a report directly from the database. This is how you would go about writing these squares. When you have multiple tables with multiple data across the place, you know, you just find out what they have in common, write up your inner joints, and then select the data that is absolutely necessary to the report that the person asked for. Because if your superiors asked for this report, you really don't need to show them the IDs and the, you know, the course code, maybe you want the course code.

So let's, let's include coursework. So I'll just duplicate this, add that comma. And I'll say C course code. And this is course for all right. And then also when you're when you're going to be render reports, you know, on missing names, you want something human readable. I think we went through this when we went through our select queries, you want to make sure that the person can read.

It's, you know, it looks neat. So we just try to keep some uniformity and some quality, some standard because your supervisor or whoever is reading the report won't see a database not all the time, so they won't know enrollment data is here as one word. And last, but not least when they get it in a dump in, you know, in a presentable form, you want it to look good. And so we run this query and we see teacher first name, teacher, last name, student. So we can clearly distinguish who is who we see that john is teaching Judy, or I have an error here because of Judy appearing in last name and first name, so I can discard that. And didn't change that.

All right. So we can just re execute this. And that's why it's good to check your work. So we have john McDonald, teaching Judy McIntosh, and all of those sports titles. And then if we only wanted to see John's students, which is the way I think we started off, then we can add a where clause so went through the where clause when we looked at filtering on our we're queries, and the condition would be, it could be that the lecturer his first name is Maybe, john, but I wouldn't do it that way. I would prefer to filter on the ID.

So I would say ID is two. Alright. So I mean, maybe a supervisor came to you and said, Hey, find all the students that john is teaching. You know, if you said, john, maybe I have more than one John's in the database, so he should maybe in the conversation, you know, it's john McDonald that is being spoken off. But if you just try to do it on that, then you may have more than one john McDonald's in your database. So the ID is the most unique thing to separate the tool, john McDonnell is that maybe it is, so I would just find what is the ID of the specific person being asked for, and it is true in this case, so I'll just say we're t.id using a my alias is equal to two and so this will actually go ahead and filter everything thing Oh, it's just to show me, john McDonald's and all of his students.

So you see that Jody is taking HTML, SQL, PHP with him. petroi is doing HTML and PHP. And Trisha is doing SQL and Petra is also doing SQL. And those are the course codes as you go along. So of course, there there are different ways to run this kind of reports, I've got all of these repetitions and refine it. But at the end of the day, you want to ensure that you have a good understanding of how you go about doing your Inner Joins when you have relationships, or you bring back data from different sources.

And finally, how you go about filtering know the filter in the where clause should, most often than more often than not be last. All right, so we could have ordered by so you do an order by after all of this and let's say we want to order by a course title. So we say C dot and we can say title. So on the order by The title, and that will come after you we're and then here we have, okay, john is teaching Troy and God in HTML is teaching those, those are his PhD students, and these are his SQL students. So based on the way or the data, you can get different things from it, because you know, it's done all the time. But then the students seem are seemingly random and all over the place, but at least we can now see why is HTML students?

Yes, to course, PHP students he has to and he has three SQL students. And so once again, based on how you write your query, you can get different data from it and you want it to be nice and clean and ready for export. So if we want to export this, we can always just right click, and we can say save results as and then we choose the data type that we're more comfortable with. So we can leave it as CSV because CSV can open in Excel, and then we just say student enrollment report. And then we save that and then we can send that off to our supervisors. Of course the title didn't give the full story because it's really students and john and john students and enrollments read and truly based on the data we have here, so this script file will be included in the resources for this video stick are owned.

See you next time.

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.