Building a view with union

Posted on 2004-09-01
Last Modified: 2007-12-19
Hi everybody:

I need to build a view in Oracle 8i or 9i. This view must make a UNION from to tables.


I have this:

table: employee
name: varchar
age: number

table: app_user
name: varchar
age: number

I need this:

create view myuser as
  select name, age from employee
  select name, age from app_user

Thanks in advanced
Question by:vcarballo
LVL 12

Expert Comment

ID: 11960210

I do not know why you are asking the question while you are providing the anwser at the same time. Have you tried yours? It should work:

SELECT name, age FROM employee
SELECT name,age FROM app_user;



Expert Comment

ID: 11960976
I agree with geotiger.  you have already answered your query.

Are you getting any error while creating view?
LVL 11

Accepted Solution

cjjclifford earned 250 total points
ID: 11961424
Note that if you don't mind duplicates in the view (i.e. if there are possibly the same rows in both the "employee" and "app_user" tables, and these both occurring in the view), then use "UNION ALL" rather than "UNION" - UNION is a strict union, so any row in the resulting view will only appear once - to do this the UNION performs a full sort and filters any duplicate rows. UNION ALL is all the rows from the first SELECT and all the rows from the second SELECT, any duplicated rows will remain duplicates, but there is no sort etc... (basically, UNION with large tables is very unadvised, if at all possible to avoid... its very similar to using DISTINCT without needing it...


Expert Comment

ID: 11961441
One thing to be aware of is that the UNION option will cause any identical entries in both tables to be only displayed once (which is probably what you want).  However, these are really big tables, remember that there will be a unique sort possibly causing a large performance overhead.

UNION ALL will simply join the results from both tables without the unique sort but this is only useful if each table stores different entries.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

828 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