Difference between except and not in sql server — Transcript
Full transcript
- 0:00this is part 88 of SQL Server tutorial
- 0:03in this video we'll discuss the
- 0:04difference between accept and not in
- 0:07operators in SQL Server this is
- 0:09continuation to part 87 so please watch
- 0:12part 87 before proceeding in the
- 0:15previous video session we discussed that
- 0:17the accept operator returns all the rows
- 0:20from the left query that are not there
- 0:22in the right query's results n an
- 0:24operator also does exactly the same
- 0:27thing let's understand this with an
- 0:29example here we've got two tables table
- 0:31a and table B notice both the tables
- 0:34have got the same number and type of
- 0:37columns if you look at the data the
- 0:39employee Rows 2 and three are present in
- 0:42both the tables now what we want to do
- 0:44is write a query that's going to return
- 0:46us only the rows that are present in
- 0:48table a but not in table B to achieve
- 0:51this we can either use the accept
- 0:53operator or not in operator let's look
- 0:56at that in action so when we execute
- 0:58this select query we're going to to get
- 1:00all the rows and columns from table a
- 1:02now let's use the accept operator and
- 1:06then select ID name gender from table B
- 1:10so if we execute this query we should
- 1:12get only the rows that are present in
- 1:14table a but not in table B so notice we
- 1:17just get that one row which is present
- 1:19only in table a but not in table B now
- 1:23we can achieve this exact same thing
- 1:25using not in operator as well let's see
- 1:28how to do it using the not an operator
- 1:31so we want to select ID name gender from
- 1:34table a where
- 1:38ID not
- 1:41in
- 1:44select ID
- 1:47from table B so let's execute these two
- 1:51queries together notice we get exactly
- 1:54the same result for both the queries so
- 1:57the obvious next question that comes to
- 1:58our mind is well then what's the
- 2:00difference between accept and not an
- 2:03operators there are two key
- 2:06differences except operator filters
- 2:09duplicates and returns only distinct
- 2:11rows from the left query that are not
- 2:13there in the right queries results
- 2:15whereas noton does not filter the
- 2:18duplicates let's understand this
- 2:19difference with an example at the moment
- 2:22if you look at the data that we have got
- 2:23in table a we don't have any duplicate
- 2:26rows now what I'm going to do is insert
- 2:28another record for Mark so we will have
- 2:31the same ID name and gender so let's
- 2:33execute this insert query right here now
- 2:37if we select what we have in table a
- 2:39notice that Mark record is repeated
- 2:42twice now when we execute this accept
- 2:46query notice we get only one row for
- 2:49Mark that means the duplicates are
- 2:52filtered whereas when we execute this
- 2:54not in
- 2:55query notice that we get the duplicates
- 2:59so the primary difference is that accept
- 3:02filters the duplicates whereas not will
- 3:05not filter the duplicates another
- 3:07difference is that accept operator
- 3:10expects the same number of columns in
- 3:12both the queries whereas not in Compares
- 3:15a single column from the outer query
- 3:17with a single column from the subquery
- 3:19so if you look at the accept operator
- 3:21here notice we have got two select
- 3:23queries you know on the left side of the
- 3:26accept operator and on the right side of
- 3:28the accept operator and if you look at
- 3:30the number of columns they are exactly
- 3:32the same now what's going to happen if
- 3:34the number changes so now one query has
- 3:37got two columns and the other query has
- 3:39got three columns when we execute that
- 3:41we get an error saying all queries
- 3:43combined using a union intersector
- 3:45except operator must have an equal
- 3:48number of expressions in the Target list
- 3:51whereas if you look at the not in
- 3:53operator it Compares a single column
- 3:56from the outer query with a single
- 3:58column from the subquery so if you look
- 4:00at this not in query here so we are
- 4:03comparing ID with ID so there's only one
- 4:07column in the outer query that we are
- 4:09comparing with one column in the
- 4:11subquery now what do you think is going
- 4:13to happen if this subquery is going to
- 4:15return maybe two columns so when we
- 4:18execute this again we are going to get
- 4:20an error and if you look at the error it
- 4:22says only one expression can be
- 4:24specified in the select list when the
- 4:27subquery is not introduced with exess so
- 4:30those are the key differences between
- 4:33accept and not in operators in SQL
- 4:36Server thank you for listening and have
- 4:38a great day
About this transcript
This page contains the full transcript of Difference between except and not in sql server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 776 words across 111 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.