Union and union all in sql server Part 17 — Transcript
Full transcript
- 0:00hello welcome to preim Technologies I am
- 0:02wenet this is part 17 of SQL server in
- 0:06this session we'll talk about the
- 0:07purpose of Union and Union all the
- 0:09differences between them and finally
- 0:11we'll also talk about the differences
- 0:13between joins and
- 0:15unions Union and Union all operators in
- 0:18SQL Server are used to combine the
- 0:21result set of two or more select queries
- 0:24let's understand what we mean by this
- 0:25with an example we have two tables here
- 0:28TBL India customers and TBL UK customers
- 0:32and both of these tables have identical
- 0:34columns you know ID name email in both
- 0:37of the tables now let's say if I issue a
- 0:40select query against these two tables
- 0:43look at this I am issuing two different
- 0:46select queries and if I just execute
- 0:48them look at the result I get two
- 0:52separate result sets Okay in TBL India
- 0:54customers we have got two rows and TBL
- 0:57UK customers we have got two rows and I
- 0:59get these as two separate result sets
- 1:02now let's say I want to combine both of
- 1:04the result sets into one result set how
- 1:07do I do that using Union or Union all
- 1:11okay let's first try to use Union all
- 1:13and see what happens okay so select star
- 1:16from TBL India customers table and then
- 1:19I'm
- 1:20saying Union
- 1:23all select star from TBL UK customers
- 1:27and when I execute this query look at
- 1:29what's going to happen it's going to
- 1:30combine the row from TBL India customers
- 1:34with the row from TBL UK customers so
- 1:36when I use this Union all I'm combining
- 1:39the result sets from these two select
- 1:41queries okay now instead of using Union
- 1:44all if I just say Union let's see what's
- 1:47going to
- 1:48happen okay look at this I got only
- 1:52three rows but let's put it back to
- 1:54Union all we get four rows and if you
- 1:57look at the output here there is a d
- 1:59duplicate record here number two Sam and
- 2:02email is ss.com so when I use Union all
- 2:07I'm getting all the rows including the
- 2:09duplicates but the moment I remove Union
- 2:11all and if I just use Union look at what
- 2:15happens to Sam record when I execute
- 2:17this you know we have got only one Sam
- 2:20record and look at the output it is
- 2:22sorted as well okay but whereas when we
- 2:26use Union
- 2:28all you know it's basically not sorted
- 2:31it takes all the rows from TBL India
- 2:33customers you know combines them with
- 2:36the rows from TBL UK customers and just
- 2:38gives the output but whereas when you
- 2:40use Union it's doing two things here
- 2:43it's removing removing duplicates and
- 2:45it's also sorting the results okay uh
- 2:49we'll talk about the differences between
- 2:50union and Union all in the next
- 2:54slide so if you look at the differences
- 2:56between union and Union all Union remove
- 2:59moves duplicate rows whereas Union all
- 3:02doesn't remove them and another
- 3:04difference is for Union to actually
- 3:08remove the duplicates it has to perform
- 3:11a distinct
- 3:12sort and then it has to remove the
- 3:14duplicates so for this reason you know
- 3:18Union is a little slower than Union all
- 3:22and this distinct sort operation is
- 3:24actually resource intensive in a sense
- 3:27it's time consuming okay if you want to
- 3:29see that you can actually turn on the
- 3:31estimated query execution plan in SQL
- 3:33Server management studio and to do that
- 3:36you can either press control l or you
- 3:38have an icon here which displays an
- 3:41estimated execution
- 3:43plan so display estimated execution plan
- 3:47when we click that what's going to
- 3:49happen is it's going to show us the
- 3:50estimated execution plan look at this in
- 3:53the display here the distinct sort uh is
- 3:56actually taking 63% of the uh you know
- 4:00time okay but whereas on the other hand
- 4:03when I use Union all and we say display
- 4:06estimated execution plan there is no
- 4:09distinct
- 4:10sort okay so it just selects the records
- 4:13from TBL India customers and then
- 4:15combines them with the result from TBL
- 4:17UK customers and just displace them but
- 4:19whereas when we use Union it has to
- 4:23First sort the records and then remove
- 4:25the duplicates which is a little timeing
- 4:27time consuming so for this reason reason
- 4:30Union is a little slower than Union all
- 4:33so these are the two differences between
- 4:34union and Union all Union removes
- 4:36duplicate rows whereas Union all doesn't
- 4:39and Union is a little slower than Union
- 4:42all because of the fact that it has to
- 4:44perform that distinct sort to remove
- 4:47duplicates and this is a common
- 4:48interview question that's asked these
- 4:53days okay so obviously if you use Union
- 4:58we don't get the duplicates whereas when
- 4:59you use Union all we get the duplicates
- 5:03okay and remember for Union and Union
- 5:06all to work the number data types and
- 5:09the Order of the columns in the select
- 5:11statement should be the same okay
- 5:13because and that should make sense as
- 5:15well you know when we actually execute
- 5:17this query look at this you know if if
- 5:20we have to combine the results of this
- 5:23select statement with the results of
- 5:25this select statement obviously the data
- 5:28types and the number of columns has to
- 5:29be same okay on the other hand if you
- 5:32just say here I want ID and name and if
- 5:36you say here I want all the three
- 5:39columns
- 5:40ID name and email okay now if I try to
- 5:45execute this look at this looking at
- 5:48itself you can say Okay first select
- 5:49statement is giving two columns whereas
- 5:51this one is giving three columns how can
- 5:53we combine them we can't do that so
- 5:56obviously when we try to execute that we
- 5:58get an error all queries combined using
- 6:01a union must have an an an equal number
- 6:04of expressions in the in their target
- 6:07list which means equal number of columns
- 6:09not only that okay it's not enough if
- 6:11you just have equal number of columns
- 6:13you know they should be in the right
- 6:15order you know instead of if you say you
- 6:18know I want ID as the last column it
- 6:21doesn't really make sense okay how can
- 6:23you combine ID with email okay you can't
- 6:27do that so when we execute this look at
- 6:29this the conversion fail okay because
- 6:33what's happening you know it's trying to
- 6:35convert either this one I mean name to
- 6:40integer and it
- 6:42fails okay because it detects this as an
- 6:45integer column and it tries to convert
- 6:48this to integer and it can't be the
- 6:51implicit conversion fails and we get the
- 6:53error so the number of the columns has
- 6:55to be the same the data types have to be
- 6:57the same and they have to be in the same
- 6:59order okay now you can do this okay look
- 7:03at this I'm saying okay I want ID you
- 7:07can say ID and email and then name okay
- 7:13you can do this when I execute this look
- 7:15at what's going to
- 7:17happen Okay um I get the output but if
- 7:21you look at the output emails are
- 7:23combined with names and similarly here
- 7:25names are combined with emails okay so
- 7:29as long as the data types are similar it
- 7:31tries to combine them you know even if
- 7:34they are
- 7:35different it it tries to do an implicit
- 7:38conversion if the implicit conversion H
- 7:40works then it selects them and combines
- 7:43them okay uh but if the implicit
- 7:45conversion fails that's when we get an
- 7:47error so remember the number of columns
- 7:52and their data types have to be similar
- 7:54and even the order has to be the same
- 7:56you know if in your first query you have
- 7:59the First Column as integer in the
- 8:02second query the First Column has to be
- 8:03integer if here it's an nare care this
- 8:07has to be an N back basically a string
- 8:09one a string column okay so their data
- 8:12types and their orders have to match and
- 8:15you need to have equal number of columns
- 8:17otherwise the union or Union all doesn't
- 8:20work you get an
- 8:23error and we have already spoken about
- 8:25the differences between union and Union
- 8:27all and sorting the results of a union
- 8:30or Union all okay this is interesting
- 8:32and we know that to sort the results uh
- 8:34you know in SQL we use the order by
- 8:36Clause but let's say I have you know
- 8:40let's use star for
- 8:43now okay let's say I'm saying select
- 8:46star from India customers Union select
- 8:49star from UK customers when we execute
- 8:51this we get the results now let's say I
- 8:53want to sort the results by name okay
- 8:57how do I do that we use the order by CL
- 8:59class so order by maybe name when we do
- 9:03that look at this it's not sorted now
- 9:05but the moment I execute this query it
- 9:07sorts the results for us okay now if I
- 9:10use order by on the first query let's
- 9:14see what is going to happen now when I
- 9:16execute this look at this it gives us an
- 9:19error stating incorrect syntax near the
- 9:21keyword and it makes sense look at this
- 9:23how does Union all works it takes the
- 9:26rows from this table combines them with
- 9:28the rows from this table table now it
- 9:30doesn't make sense to First Take sorted
- 9:32rows and then combine your result will
- 9:34again become unsorted that's why after
- 9:37getting all the results then sort the
- 9:41results finally using your order by
- 9:43class that's why I think autoby should
- 9:46be only you know within the last select
- 9:48statement for example I have another
- 9:51table called uh TBL us
- 9:53customers so when I say Union
- 9:58all select star from
- 10:02TBL us customers and we execute that
- 10:05it's going to get all the rows from all
- 10:07the tables and then it's going to sort
- 10:09them okay but on the other hand if if
- 10:11you try to use it on the second select
- 10:14you still get an error you
- 10:16know okay so if you want to S the
- 10:19results of Union use the autoby clause
- 10:22on the last select
- 10:24statement like
- 10:27so all right and and differences between
- 10:30union and join we have spoken about
- 10:32joins in the previous sessions in this
- 10:34video series okay now unions and joints
- 10:38are two separate things okay but however
- 10:41you know these days people are asking
- 10:43this question what's the difference
- 10:45between a union and join okay join
- 10:48basically if you look at this Union
- 10:50combines the result set of two or more
- 10:52select queries into a single result set
- 10:55which includes all the rows from all the
- 10:58queries in that Union
- 10:59whereas what what a join does is it
- 11:03retrieves data from you know two or more
- 11:06tables based on The Logical relationship
- 11:08between those two tables so usually
- 11:10tables are related to each other using
- 11:12primary and foreign Keys okay so we will
- 11:15take you know the foreign key and look
- 11:17up its value in the other table and join
- 11:19the columns together in those related
- 11:23tables whereas in Union you know we
- 11:26basically combine the rows so in short
- 11:29the difference between a union and a
- 11:31join is that Union combine rows from two
- 11:34or more tables whereas join combines
- 11:36columns from two or more you know tables
- 11:39for example you know we have seen the
- 11:41examples of Union we have taken roles
- 11:43from TBL USA customers and joined them
- 11:46with TBL UK customer rows know we did a
- 11:49horizontal kind of a join whereas if you
- 11:51look at U you know basically not join
- 11:54it's um uh Union okay but whereas when
- 11:57you look at a join for example we have
- 12:00TBL employees table and TBL departments
- 12:02table in TBL employees table we have got
- 12:05Department ID column which we then look
- 12:08up in the Department's table and maybe
- 12:11we we retrieve the department name so
- 12:12from TBL employees table I get employee
- 12:15name and maybe his age and then from TBL
- 12:19Department table we get the Department
- 12:21name so enjoins we combining the columns
- 12:25between two or more tables whereas in an
- 12:27Union we are combining row between two
- 12:29or more
- 12:31tables on this slide you can find
- 12:33resources for asp.net and C interview
- 12:36questions that's it for today thank you
- 12:38for listening have a great day
About this transcript
This page contains the full transcript of Union and union all in sql server Part 17 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 2,073 words across 289 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.