Advanced or intelligent joins in sql server - Part 13 — Transcript
Full transcript
- 0:00hello welcome to preim Technologies I'm
- 0:02wenet this is part 13 of SQL server in
- 0:05this session we'll discuss about
- 0:07Advanced our intelligent joints in SQL
- 0:09Server as part of this we will see how
- 0:12to retrieve only the non-matching rowes
- 0:14from the left table and along the same
- 0:16lines how to retrieve only the
- 0:19non-matching rows from the right table
- 0:20and finally we will see retrieving only
- 0:24the non-matching rows from both the left
- 0:26and right tables before continuing with
- 0:29the session I strongly recommend
- 0:31watching Part 12 of this video
- 0:34series now in part 12 of this video
- 0:36series we have seen how inner join left
- 0:39join right join and full join works we
- 0:42have seen examples of that if you
- 0:44recollect from that part inner join
- 0:47retrieves only the matching rows between
- 0:49both the tables whereas left join
- 0:51retrieves the matching rows plus
- 0:53non-matching rows from the left table
- 0:55and right join matching rows plus
- 0:58non-matching rows from the right table
- 1:01full outer join matching rows from both
- 1:03the tables plus non-matching rows from
- 1:06the left table as well as non-matching
- 1:07rows from the right table now if you
- 1:09look at all these joints you know the
- 1:12queries underneath them they are pretty
- 1:14much identical and the only difference
- 1:16you find is the type of join rest of the
- 1:19query you know is almost identical so if
- 1:22you look at here it's inner join but
- 1:25whereas in the left join you just say
- 1:27it's a left join for right join right
- 1:30join and full join you say full join
- 1:31that's the only difference otherwise the
- 1:33query is
- 1:35identical all right now let's assume
- 1:39okay so if you look at these four types
- 1:41of joints here all the types of joints
- 1:44you know they give you the matching rows
- 1:48plus non-matching parts okay so but
- 1:53let's say I want an output like this
- 1:56okay I want only the non matching rows
- 2:01from the left table and along the same
- 2:03lines I want only the non-matching rows
- 2:06from the right table and maybe finally I
- 2:09want only the non-matching rows from the
- 2:12left table and the non-matching rows
- 2:13from the right
- 2:15table okay so basically in this three
- 2:19result sets we are we are actually
- 2:22excluding the matching Parts in the
- 2:24previous slide we see in all the four
- 2:27types of joints we are getting the
- 2:29matching rows plus something
- 2:32extra okay but here no matching rows at
- 2:36all non-matching rows from the only
- 2:38non-matching rows from the left table
- 2:40only non-matching rows from the right
- 2:41table non-matching rows from the left
- 2:44table and non-matching rows from the
- 2:46right table and how do we do that you
- 2:48know it's a it's a it's very easy to
- 2:51understand this you know you write your
- 2:52query if you want only the non-matching
- 2:54rows from the left table you write the
- 2:57query as you do it for left join and and
- 2:59then you'll have a wear Clause okay
- 3:02let's see how to do that now if you look
- 3:03at these two tables here the it's the
- 3:06same employees and departments table we
- 3:07have been working with in part 12 of
- 3:09this video series so if you look at this
- 3:11employees table you know James and
- 3:14Russell records have got Department IDs
- 3:17now okay so this is our left table and
- 3:20this is our right table now I want only
- 3:23James and Russell's records which means
- 3:26the non-matching rows in the left table
- 3:29okay so how do we do that let's write a
- 3:31query for that so when you write your
- 3:34query the output should be like this
- 3:36name gender salary Department name for
- 3:39James and Russell there is no matching
- 3:41GR in the Department's table that's why
- 3:43Department name will be null okay but
- 3:45basically I want only those non-matching
- 3:48rows from the left table so let's see
- 3:50how to write the query for that so
- 3:52select these columns whatever you want
- 3:55from what's the name of the table
- 3:58TBL employee
- 4:02and we want to left join with that
- 4:05table and which table is that
- 4:09TBL Department table and obviously join
- 4:13condition
- 4:14on TBL
- 4:17employee. Department ID is equal to
- 4:20departments
- 4:22table TBL department. ID so that's our
- 4:27joint condition so if you look at this
- 4:29result I mean this query now it's a left
- 4:32join query now if we execute this query
- 4:35we get all the 10 rows from the left
- 4:38table okay the matching rows plus the
- 4:40non-matching rows so that's what is the
- 4:43inner join query I mean left join query
- 4:46select whatever columns we want from
- 4:48employee left join department on you
- 4:51know the join condition so we get the
- 4:53matching rows as well as the
- 4:54non-matching rows but what's our uh
- 4:57Target our Target is basically to
- 4:59retrieve only the non-matching rows from
- 5:02the left table which means we want only
- 5:05James and Russell's records which means
- 5:08we have to filter out all these these
- 5:11eight rows okay so obviously if we have
- 5:15to filter we will use the wear Clause
- 5:18okay and there are two ways to achieve
- 5:20this one is look at this you can simply
- 5:23say Okay I want all these rows but I
- 5:28want to filter out okay where employee
- 5:32table you know I basically want only the
- 5:35rules which has got
- 5:38Department ID as null so if you look at
- 5:41this who has got Department ID as null
- 5:45James and Russell in the employees table
- 5:48so what you can basically do here is you
- 5:50can say TBL
- 5:52employee. Department ID is null so the
- 5:56moment you do that what's going to
- 5:58happen
- 6:00when we press F5 we get all these rows
- 6:04you know all this only the non-matching
- 6:06rows from the left table another way to
- 6:09do it is okay if you look at the
- 6:11presentation you know another way is to
- 6:13do it is department. ID is null so
- 6:17Department
- 6:21table
- 6:26dot ID is now so when we execute this
- 6:31query we get the same result and and
- 6:33it's pretty simple to understand what
- 6:35you're saying okay when you when you say
- 6:39okay I want to do a left join what do we
- 6:41get we get everything the matching rows
- 6:44plus the non-matching rows from the left
- 6:46table okay so but we want to filter out
- 6:49James and Russell's record so as part of
- 6:52that what is the condition we are using
- 6:54we are saying department. ID is null so
- 6:57basically for all these records
- 6:59Department ID is not null hence they
- 7:01will be filtered out only these two
- 7:03records has got Department ID is null so
- 7:06you know we don't have a match you know
- 7:08as it is we get all the records from the
- 7:10left table so these records who has a
- 7:13department ID will be filtered
- 7:15out so that's how we get only the
- 7:19non-matching rows from the left table
- 7:21now let's say I want you know if you
- 7:24look at this one I want only the
- 7:27non-matching rows from the right table
- 7:29now if you remember how do we write a
- 7:31right join instead of left join you just
- 7:34say right join and when we do this what
- 7:37do we get we get you know right join
- 7:39will basically give us all the matching
- 7:41rows between the two tables plus the
- 7:44non-matching rows from the right table
- 7:46so there are eight matching rows between
- 7:48TBL employee and department so we get
- 7:50those eight rows plus one additional
- 7:53record you know this other department
- 7:56record does not have you know no no
- 7:58employee
- 8:00is assigned that
- 8:02department okay so so this is the
- 8:05non-matching row in the right table okay
- 8:09so we get this row as well as the
- 8:11matching rows between these two tables
- 8:13you know these eight if we are using a
- 8:15right joint but what is Our intention
- 8:18Our intention is basically to retrieve
- 8:19only the non-matching rows from the
- 8:21right table which means I just want this
- 8:24other department row okay so how do we
- 8:28do that all you need to do is look at
- 8:30this we basically want to check okay
- 8:33wherever the employee. department ID is
- 8:37null okay so basically if you look at
- 8:40this right join here what is it doing
- 8:44it's giving you all the matching rows
- 8:47plus the non-matching rows from the um
- 8:50right table okay now when you when you
- 8:54say where e employee. Department ID is
- 8:58null for all of these Department ID is
- 9:02is not null so obviously these rows will
- 9:05be filtered out which means these
- 9:07matching rows will also be filtered out
- 9:09and you only be left with this other
- 9:11department
- 9:14row so when I
- 9:17say TBL
- 9:20employee. Department
- 9:25ID is
- 9:28null we only get that other department
- 9:31so we only get the non-matching rowes
- 9:33from the right table okay now let's say
- 9:36I want the nonmatching rows from the
- 9:38left table plus the non-matching rows
- 9:40from the right table by now you would
- 9:42have already guessed what to do okay you
- 9:44will just use an or or condition okay so
- 9:50in the we class we'll say TBL employee
- 9:54um TBL employee. Department ID is null
- 9:58or
- 10:02TBL do Department do ID is
- 10:06null so what's going to happen we are
- 10:09going to
- 10:10get only the non-matching rows um we're
- 10:16saying okay I mean look at this when I
- 10:19run this query I'm not getting the
- 10:22non-matching rows from the left table
- 10:25why because you are using a right join
- 10:28so you will have to use a full join
- 10:31there so what does full join give you
- 10:33full join will give us all the matching
- 10:34rows
- 10:35between both the tables plus the
- 10:38non-matching rows from the left table
- 10:39plus the non-matching rows from the
- 10:40right table so if we execute the query
- 10:43until here we end up getting 11 rows
- 10:47okay eight matching rows two
- 10:49non-matching rows from the left table
- 10:51and one non-matching row from the right
- 10:53table but our Our intention is to filter
- 10:57out these matching rows these eight
- 10:59matching rows you we only want the
- 11:01non-matching rows from the left table
- 11:03and the non-matching row from the right
- 11:05table so in order to do that we are
- 11:07using this wear Clause so when we
- 11:10execute this now we should only get the
- 11:13non-matching rows from the left table as
- 11:15well as from the right
- 11:18table so we get these three
- 11:22rows all right and one important thing
- 11:24to keep in mind is never ever use you
- 11:28know
- 11:30equal to When comparing null value you
- 11:32cannot say you know TBL employee.
- 11:35Department ID is equal to null and along
- 11:38the same lines here you cannot say you
- 11:40cannot use equal to operator to compare
- 11:43nulls in SQL Server you always have to
- 11:45use e
- 11:49keyword all
- 11:52right on this slide you can find
- 11:54resources for asp.net and C interview
- 11:56questions that's it for today thank you
- 11:58for listening have a great day
About this transcript
This page contains the full transcript of Advanced or intelligent joins in sql server - Part 13 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,831 words across 269 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.