SQL Server 2005 --Union

There is a good article 'SQL SERVER – Difference Between Union vs. Union All'

at http://blog.sqlauthority.com/2009/03/11/sql-server-difference-between-union-vs-union-all-optimal-performance-comparison/

Can you please provide REAL LIFE Example where Union is used TO select related information from two tables???

It's easy to understand how Union works...but where we can use it in real life?

Who is Participating?
Suppose you have two table called Teacher_tbl and Student_tbl. Now there may or mayn't exists any relation between both of them. Suppose you need to find out the list of students + teachers name from a school who are from a particular city. In this case you will select studentName from student_tbl and teachers name from Teacher_tbl. This can be easily achieved by union all.

Generally it is used to combine result from two tables which are union compatible and give you the result where there is no relation exists. You can also use them in case where relation exists but at that case, you can also write joins. But places where you don't have any relation but something common and you want to combine the result, you can use union.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
in short: use UNION ALL always, as it will not do a implicit DISTINCT over the whole result set.
first, it will be faster, and second, it will not do something that you won't see directly from the code.

if you really need a UNION , use that one, and comment it in the code.
apart from that, see this article for doing discount/group by if that functionality is really needed
niceguy971Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.