SQL Server except operator — Transcript
Full transcript
- 0:00This is part 87 of SQL Server tutorial.
- 0:03In this video, we'll discuss the SQL
- 0:05Server accept operator with a few
- 0:07examples. So, what does this accept
- 0:09operator do? This operator returns
- 0:11unique rows from the left query that are
- 0:14not there in the right queries results.
- 0:16Let's understand this with an example.
- 0:18We'll be using these two tables, table A
- 0:20and table B. In this demo, I've already
- 0:22created these tables and here is the SQL
- 0:24script to create them and populate with
- 0:26test data. I'll have the script
- 0:28available on my blog in case you need
- 0:29it. Now let's write a query that's going
- 0:31to involve both these tables and the
- 0:34except operator. So what we want to do
- 0:37is select ID, name, gender from table A.
- 0:40So when we execute this query, as
- 0:42expected, we are going to get all the
- 0:43five rows that are present in this
- 0:45table, table A. Now along with that
- 0:48query, I also want to use the accept
- 0:50operator and we want to select ID name
- 0:54gender from table B. Now when we execute
- 0:57this query, what is the result that we
- 0:59expect? Now what is this accept operator
- 1:01going to do? It's going to return us the
- 1:03rows from the left query that are not
- 1:05there in the right queries results. And
- 1:07if you look at these two tables, these
- 1:09records that is employee John and Sarah,
- 1:12those two records are present in both
- 1:14the tables. So when we execute this
- 1:16except query, it should only return us
- 1:19the first three rows. So let's look at
- 1:22that in action. Notice that we get the
- 1:24rows from 1 2 3. So that's what the
- 1:27except operator is going to do. This
- 1:29operator is introduced in SQL Server
- 1:322005. The most important thing to keep
- 1:34in mind is that for the except operator
- 1:37to work, the number and the order of the
- 1:39columns must be the same in both the
- 1:41queries and the data types must also be
- 1:44either same or compatible. Now what's
- 1:46going to happen if the number and the
- 1:48order of the columns is not same? For
- 1:50example, let's remove this gender column
- 1:52here. So in this query, we only have two
- 1:55columns. Whereas in this query, we have
- 1:56got three columns. And if I execute
- 1:59this, notice that we get an error.
- 2:02Similarly,
- 2:04you know, in this case, the number is
- 2:06the same,
- 2:08but
- 2:10the order is different.
- 2:14Again, when we execute this, we are
- 2:16going to get the same error. Okay? So
- 2:18that's very important. And the number
- 2:20and the order of the columns and the
- 2:22data types must be either same or at
- 2:24least compatible. And if you are from
- 2:27Oracle background, we can relate this
- 2:30except operator to minus operator in
- 2:32Oracle.
- 2:35Now in this example,
- 2:38we have you know returned the results
- 2:41that are present in table A but not in
- 2:44table B. Now what if you want the other
- 2:46way around? I want the rows that are
- 2:48present in table B but not in table A.
- 2:50If that's the case, you can reverse
- 2:52these queries. So, select ID, name,
- 2:54gender from table B except ID, name,
- 2:57gender from table A. So, when we execute
- 3:00this, we only get the rows that are
- 3:01present in table B but not in table A.
- 3:04So, we only get here in this case 6 7 8
- 3:07which are present in table B but not in
- 3:09table A. Now, here we have seen how to
- 3:12use the except operator on two different
- 3:14tables. Is it possible to use it on a
- 3:16single table? Absolutely, you can use it
- 3:19on a single table and you can even have
- 3:22wear clause. So for the single table
- 3:24example, we'll use this table tbl
- 3:26employees. Again, I've already created
- 3:28that and populated it with test data.
- 3:30And here is the SQL script to do that.
- 3:32So what we want to do is select name, id
- 3:35name, gender, and
- 3:38salary from table tbl employees.
- 3:44So when we execute this query, it's
- 3:46going to give us all the employee rows.
- 3:49Right? Now what I want to do is apply a
- 3:51wear clause where salary is greater than
- 3:54or equal to 50,000. So what is this
- 3:57going to do? It's going to give us all
- 3:59the employees who has a salary greater
- 4:01than or equal to 50,000. Now I'm going
- 4:05to use except operator and I'm actually
- 4:08going to make a copy of this select
- 4:10query and then I'm going to change the
- 4:12wear clause here. So I'm going to say
- 4:15where salary is greater than or equal to
- 4:1860,000. Now when we execute this we
- 4:22should only get those employees whose
- 4:24salary is between 50,000 and 60,000.
- 4:29So we are using it on the same table.
- 4:32accept operator.
- 4:35Now we can also use order by class when
- 4:37we use the accept operator but you have
- 4:40to use it after the right query that is
- 4:42after the second query. You can't use
- 4:45order by right here. Let's say for
- 4:47example we want to order by ID in
- 4:49descending order. If I say order by ID
- 4:52descending here we will get an error.
- 4:56You have to use it after the second
- 4:59query that is the right query. So now
- 5:01when we execute that we get the result
- 5:03and notice that the result is sorted by
- 5:05ID in descending order.
- 5:10Thank you for listening and have a great
- 5:12day.
About this transcript
This page contains the full transcript of SQL Server except operator by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 911 words across 127 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.