Intersect operator in sql server — Transcript
Full transcript
- 0:00this is part 89 of SQL Server tutorial
- 0:03in this video we'll discuss the
- 0:05intersect operator in SQL server and the
- 0:07difference between intersect and in a
- 0:10join so what does this intersect
- 0:12operator do this operator retrieves the
- 0:15common records from both the left and
- 0:17the right query of the intersect
- 0:19operator let's understand this with an
- 0:21example here we've got two tables table
- 0:24a and table B notice both the tables
- 0:26have got the same number and type of
- 0:28columns if you look at the data employee
- 0:31records 2 and three are present in both
- 0:34the tables now what we want to do is
- 0:36write a query that's going to retrieve
- 0:38the common rules that are present in
- 0:40both the tables we can achieve this two
- 0:42ways we can achieve this either by using
- 0:45the intersect operator or by using inner
- 0:48join let's look at both the ways let's
- 0:50flip to SQL of management studio so this
- 0:53query is going to retrieve all the rows
- 0:55from table a now I'm going to use
- 0:58intersect operator and retrieve ID name
- 1:01gender from table B so if we execute
- 1:04this intersect query we should get all
- 1:07the rules that are common between both
- 1:08tables now if you look at these two
- 1:10tables the common rows are with IDs 2
- 1:13and three so we get those in our result
- 1:15set right here now let's see how to
- 1:18achieve exactly the same thing using in
- 1:20a join so what we want to do now is
- 1:23Select ID name gender from table a in a
- 1:26join with table B and we have to to
- 1:30specify the join condition and the
- 1:31common column between both the tables is
- 1:33ID so on table a. ID equals table b. ID
- 1:40now if we execute this in a join query
- 1:42we are going to get an error that's
- 1:44because in both the tables we've got the
- 1:46same column names ID name gender so
- 1:49that's why we get this ambiguous column
- 1:51name error three times so we will have
- 1:53to specify the name of the table from
- 1:55which we want these columns we can
- 1:57either use table a or table B
- 2:00but I'm going to use table a for this
- 2:02example so table a. ID table
- 2:06a.name and table a. agenda now let's
- 2:10execute these two queries together
- 2:11notice we get exactly the same result
- 2:15now the obvious question that comes to
- 2:17our mind at this point is then what's
- 2:18the difference between in a join and
- 2:21intersect operators we'll discuss those
- 2:24differences in just a bit but before
- 2:26that let's discuss a few more points
- 2:28about this intersect operator this
- 2:30operator is introduced in SQL Server
- 2:322005 just like the except operator for
- 2:36intersect operator to work the number
- 2:38and the Order of the columns must be the
- 2:41same in both the queries the data types
- 2:43must be same or at least
- 2:45compatible all right now let's look at
- 2:47the differences between intersect and in
- 2:50a join intersect filters duplicates and
- 2:53returns only distinct rows that are
- 2:55common between the left and right query
- 2:57whereas in a join does not filter the
- 2:59duplicates let's understand this
- 3:01difference with an example now I'm going
- 3:04to insert This Record into table a so
- 3:08the record is going to have an ID value
- 3:09of two name Mary gender female now if we
- 3:13retrieve data from table a we should
- 3:18have Mary's record repeated twice notice
- 3:21ID name and gender are the same for
- 3:24record 2 and record 4 okay so now let's
- 3:28execute our intersect query notice that
- 3:32we only get the unique rows the
- 3:35duplicates are filtered out we only get
- 3:37once this Mery record but if you look at
- 3:40inner join we are going to get Mery
- 3:43record twice so that's one of the
- 3:45primary differences between intersect
- 3:47and inner join to make this inner join
- 3:50behave like the intersect operator use
- 3:52the distinct operator so now notice if
- 3:55we use distinct operator this is only
- 3:58going to retrieve the distinct row so in
- 4:00this case the inner join is going to
- 4:03behave like intersect operator notice we
- 4:06get exactly the same result now the
- 4:10other difference is inner joint treats
- 4:12two nulls as two different values so if
- 4:14you're joining two tables based on a
- 4:16nullable column and if both tables have
- 4:19nulls in that joining column then inner
- 4:21join will not include those rows in the
- 4:23result set whereas intersect treats two
- 4:26nulls as a same value and it returns all
- 4:29matching rows let's look at this in
- 4:31action so to understand this difference
- 4:34I'm going to insert a row with null
- 4:37value for ID column into table a and
- 4:39similarly into table b as well so let's
- 4:42execute these
- 4:43two insert queries and let's select the
- 4:47data that we have at the moment in table
- 4:49a and table B so there is one null
- 4:54column you know for one of the rows and
- 4:57if you look at name and gender columns
- 4:58the data is same okay now let's see
- 5:01what's going to happen when we execute
- 5:03the intersect query so when we execute
- 5:06the intersect query notice that we get
- 5:09that null row okay whereas when we
- 5:12execute the inner join we will not get
- 5:16that null row okay so that's another
- 5:19difference between inner join and
- 5:21intersect
- 5:24queries thank you for listening and have
- 5:26a great day
About this transcript
This page contains the full transcript of Intersect operator in sql server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 903 words across 126 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.