Self join in sql server - Part 14 — Transcript
Full transcript
- 0:00Hello, welcome to Premium Technologies.
- 0:02I'm Wenut. This is part 14 of SQL
- 0:04Server. In this session, we'll talk
- 0:07about self join which is nothing but
- 0:09joining a table with itself. We'll also
- 0:11talk about self- join classification.
- 0:14Most of the people think self join is a
- 0:16different type of join altogether which
- 0:18is not true. Self join can be classified
- 0:21as inner, outer or cross join depending
- 0:25on how you write the self join. Before
- 0:28continuing with the session, I strongly
- 0:30recommend watching parts 12 and 13 of
- 0:32this video
- 0:34series. Now, in parts 12 and 13, we have
- 0:37already seen how to join two different
- 0:40tables. Specifically, we have been
- 0:42working with TBL employees and TBL
- 0:44departments tables. But have you ever
- 0:47thought of a need to join a table with
- 0:49itself? If you look at the table here
- 0:51that we have, it has got three columns.
- 0:54Employee ID, name, and manager ID.
- 0:57Now let's say I want you to write a
- 1:00query which gives me an output like
- 1:02this. I want the employee name as well
- 1:05as his manager's name. Okay. Now if you
- 1:08look at the table on the left hand side,
- 1:09if you look at Mike, Mike's manager ID
- 1:13is three. And you take this three and
- 1:16look up in the employee ID column. Okay,
- 1:18three is Todd. So Mike's manager is
- 1:21Todd. And along the same lines, if you
- 1:24look at Rob, his manager ID is one. And
- 1:27you take this one and look up in the
- 1:29employee ID column, one is Mike. Okay?
- 1:32So Rob's manager is Mike. And we get
- 1:35that output here. So if you look at the
- 1:38situation here, we are using this
- 1:41manager ID column and looking up in the
- 1:44employee ID column within the same
- 1:46table, which means we are you know
- 1:50referencing the same table. Okay. So
- 1:54here if we want this kind of an output
- 1:57like employee name and manager's name,
- 1:59there is a need to join this employee
- 2:02table with
- 2:04itself. In parts 12 and 13, we have
- 2:07joined TBL employees and tbl department
- 2:09tables. But then in tbl employee table,
- 2:12I had department ID column which I have
- 2:16used to look up the ID column in tbl
- 2:19department's table which is another
- 2:21table. But here I'm using this manager
- 2:24ID column to look up employee ID column
- 2:28in the same table. So there is a need to
- 2:31reference the same table to join with
- 2:33the ta same table and that's nothing but
- 2:36a self join. Now you might be wondering
- 2:39how can we join the same table with
- 2:42itself. Okay. So if you look at this
- 2:45here this query right here you know we
- 2:48have done exactly the same thing. Okay
- 2:50select first of all whatever the we'll
- 2:52talk about these columns just in a bit.
- 2:55Okay. So if you look at the output we
- 2:56want the employee name and the manager
- 2:58name. So select employee name, manager
- 3:00name. We'll come to those bits in a in
- 3:03uh you know the column list in the
- 3:05select clause in a bit. If you look at
- 3:07the from clause, we are saying tbl
- 3:09employee and we are giving it an a table
- 3:12alias as e and we are saying okay join
- 3:16this table left join. We are doing a
- 3:18left join with tbl employee again. So if
- 3:22you look at this you know forget about
- 3:23the type of join here. It can be inner
- 3:26join left join whatever. Okay. So,
- 3:29select whatever columns you want from
- 3:31tbl employee. Give it an alias e join
- 3:35with the same table tbl employee and
- 3:38give it another alias m e for employee m
- 3:42for manager. So, you can assume now we
- 3:45have two instances of this table you
- 3:47know two copies of this table rather
- 3:50employee table and then another table
- 3:52like manager table. Okay. But both of
- 3:56the structures will be identical. You
- 3:58know, you're basically referencing the
- 4:00same table. And then what you're doing
- 4:03is in employee table, take the manager
- 4:06ID column. Okay? In the employee table,
- 4:10take the manager ID column and look up
- 4:13the employee ID in the manager table,
- 4:16which is nothing but another copy of the
- 4:18same
- 4:19table. Okay? And then from the employees
- 4:22table which is nothing but E I want the
- 4:25name which is nothing but the employee
- 4:27name. So I'm giving it an an alias as
- 4:30employee. And from the manager table I'm
- 4:33getting the name which is nothing but
- 4:34the manager name. Okay. So so that's how
- 4:38we get this output. If you look at the
- 4:41join here we are joining with the same
- 4:43table. So this is nothing but a self
- 4:45join. So let's write this in SQL server
- 4:47management studio.
- 4:49So we'll come to the column list in a
- 4:52bit. Okay. Select from from which table?
- 4:57From tbl
- 4:59employee. Let's give it an alias as
- 5:04E left
- 5:08join
- 5:10with the same table TBL employee. But
- 5:13then I'm going to call this M for
- 5:15manager. So this is an employee table.
- 5:18This is the manager table. And how do we
- 5:21want to join
- 5:24that? Okay, in the employees table, I
- 5:28want to take the manager ID column and
- 5:32look that up in the manager
- 5:36table in the employee ID column. So look
- 5:39up employee ID column in manager table
- 5:42using the manager ID column in employee
- 5:44table. You are treating the same table
- 5:46as two different tables now. which means
- 5:48you are actually joining the same table
- 5:50with itself. So what columns do you
- 5:52want? Now from the employee table if I
- 5:55pick up the name it will be an employee
- 5:57name. So give it a meaningful name. So
- 5:59as employee give it an alias and
- 6:02similarly from manager table I want the
- 6:06name but this is going to be the manager
- 6:08name. So manager. So that's it. So when
- 6:12we execute this
- 6:14query, we get the output as we expect.
- 6:18Okay. Now look at this. We are using a
- 6:21left join here. And if you remember from
- 6:23part 12 and 13 of this video series,
- 6:25left join will give you all the matching
- 6:27rows between these two tables plus
- 6:30non-matching rows from the left
- 6:32table. Okay? But if it's an inner join,
- 6:36it will only give the matching rows
- 6:38between the two tables. Which means look
- 6:40at this. when we were using left join we
- 6:43were getting Todd's record as well but
- 6:45the moment I change it to inner join
- 6:47okay see if you look at the output right
- 6:49now we've got five rows including the
- 6:51Todd's record now when I execute this
- 6:54one look at what's going to happen I
- 6:56only get four rows what happens to Todd
- 6:59record it's not retrieved anymore why
- 7:01because if you look at Todd's record the
- 7:04manager ID does not match with any of
- 7:07the employee IDs there is no matching
- 7:08record and if you Remember in a join
- 7:11will only give you the matching records
- 7:12between the two tables. Now you might be
- 7:14wondering where are two tables here but
- 7:16remember in the join we are joining this
- 7:19table with itself. So we are treating a
- 7:21single table like two tables. We're
- 7:23joining the same table with itself.
- 7:25Okay. So when you are using this column
- 7:27to look up in the employee ID column of
- 7:29the same table if there are no matching
- 7:32rows and you're doing an inner uh I mean
- 7:34um the matching rows are not returned if
- 7:37you do an inner self join. So you can
- 7:40call this as an inner self join. Okay.
- 7:43Why is it called a self join? Because
- 7:45you're joining the same table with
- 7:46itself and you're doing an inner join.
- 7:48So inner self join. And along the same
- 7:51lines you can call this query you know
- 7:53you're joining if you look at the first
- 7:56um picture here we are joining TBL
- 7:58employee with manager but we are doing a
- 8:00left join. So you can call this as left
- 8:02outer self join. Okay. So along the same
- 8:05lines you can do a right outer self join
- 8:07and a full outer self join. So a self
- 8:10join is not a different type of join
- 8:13altogether. It can be classified under
- 8:16any type of join inner outer any of the
- 8:18outer joints left right or full and
- 8:21cross join as well. We have seen how to
- 8:23write a self you know left outer self
- 8:25join inner self join but we haven't seen
- 8:28how to write a cross self join. It's
- 8:30pretty much identical. If you remember
- 8:31from part 12 of this video series, a
- 8:34cross join cannot have an on clause. So
- 8:37a cross sol self join can
- 8:40als it also shouldn't have the on
- 8:43clause. So if you want to convert this
- 8:46into a cross join, the first thing you
- 8:48have to do get rid of the on clause and
- 8:50then convert this inner join into cross
- 8:54join. That's about it. Since we are
- 8:56joining the same table with itself, we
- 8:58call it you know a self join. But since
- 9:01we are using cross join, it's called as
- 9:03cross self join. Okay. Now remember in
- 9:07the employee table we have got five
- 9:09rows. And if you remember from the
- 9:11definition of the cross join when you do
- 9:13a cross join between two tables you get
- 9:15the cartesian product which is nothing
- 9:17but the number of rows in the first
- 9:20table multiplied by the number of rows
- 9:22in the second table. Okay. Now here it's
- 9:26the same table. So in the the same table
- 9:29you know we're joining employees table
- 9:30with itself. In employee table we have
- 9:32got five rows and in the manager table
- 9:34obviously we have another five rows. So
- 9:37when we execute this query we get five
- 9:41multiplied by five which is 25 rows. So
- 9:44when I press F5 look at that in the
- 9:46output you should see there are 25 rows.
- 9:49So keep in mind you know it's very
- 9:51simple to understand joining a table
- 9:54with itself is nothing but a self join
- 9:56and self join is not a different type of
- 9:59join altogether. Self join can be
- 10:02classified under any type of joins like
- 10:05inner outer cross join depending on how
- 10:08you write your self join
- 10:11query. Now if you look at the output
- 10:14here, when we do a left
- 10:20um when we do a left
- 10:28join on employee dot manager ID is equal
- 10:31to manager dot employee ID. Look at
- 10:35this. When we execute this
- 10:37query, we get Todd's record. You know
- 10:39Todd doesn't have a manager. So the
- 10:41manager name is null. Now instead of
- 10:43showing null, you know, I want uh to
- 10:46display since Todd doesn't have a
- 10:48manager, maybe I want to display a text
- 10:51here saying no manager or I want to say
- 10:53super boss or super manager, whatever.
- 10:55Instead of null, I want to show my own
- 10:58string here. How do I do that? There are
- 11:00several ways you can do it. Okay, which
- 11:02we'll be talking about in our next
- 11:04session.
- 11:11On this slide, you can find resources
- 11:12for ASP.NET and C# interview questions.
- 11:15That's it for today. Thank you for
- 11:16listening. Have a great day.
About this transcript
This page contains the full transcript of Self join in sql server - Part 14 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,875 words across 257 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.