We help IT Professionals succeed at work.

SQL Union Different Tables

lcor used Ask the Experts™
I came across some code and I'm trying to understand unions in SQL.

The code has two select statements between a union.  The two select statements are on the same table.

Isn't that a bad use of a union?  Shouldn't unions between different tables with like data?

Just trying to understand theory.  I can't post examples as they are proprietary.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Information Services Manager
Can you post the query?

...it's not necessarily "bad" per se, it depends on what you are trying to accomplish.  There may be a better/more efficient way of writing it however.
Most Valuable Expert 2011
Top Expert 2012
it might or might not be appropriate.

in general, I try to avoid hitting the same table more than once in the same query
but sometimes it's necessary.

if you can't post the exact query,  can you post a modified version with generic column and table names?
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Sometimes it is much easier to use UNION, in particular if you want to retrieve different fields based on a value in another field (or any other condition). But in general it is suspicious. Both parts of UNION are executed independent, and the result is merged (!), removing duplicates, which involves an additional step - unless it is a UNION ALL, that is.
simple union only same type of data/records.
it is not necessary that all records from same table it may come from different tables only condition is that match number of columns and data type.