Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unions for Dummies

Posted on 2011-03-10
1
Medium Priority
?
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Via a live example, show how to take different types of Oracle backups using RMAN.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

670 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