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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.