Solved

Unions for Dummies

Posted on 2011-03-10
1
413 Views
Last Modified: 2012-05-11
I created a query that involved a bunch of joins.  Had a real SQL pro look at the query results and apparently they included too much.  The pro redid the query using a UNION.

Can someone explain to me what the data would look like in general for the pro to use a join?

What are the steps in deciding what select statements on each side of the join would look like?  I guess I'm asking for "how to do a union" explanation.
0
Comment
Question by:lcor
1 Comment
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 35098594
The simple on a straight UNION.

Take the unique results from: Give me everything from queryA add it to the results of give me everything from queryB.

A "UNION ALL" doesn't take unique records.

The caveats:
The number of columns must match and the datatypes for each individual column across the multiple queries must match.

You can do:
select 'string1' col1 , to_number(1) col2 from dual
union
select 'string2' col1 , to_number(2) col2 from dual

You cannot do:

select 'string1' col1 , to_number(1) col2 from dual
union
select to_number(2) col1 , 'string2' col2 from dual

The data types don't match.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now