Solved

Unions for Dummies

Posted on 2011-03-10
1
412 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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 …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

744 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

11 Experts available now in Live!

Get 1:1 Help Now