Newsletter Subscription | Glossary | Contact Us
Home > All Categories > Oracle > SQL and PLSQL > Union vs Union All
Question Title Union vs Union All
Authored by: Patrick Barel
Viewed: 379 times so far

In short, UNION does a sort-unique on both collections. UNION ALL just retrieves all rows, regardless of any double values.

I came across a (similar) piece of code during my work at BT:

select distinct * from ...
union
select distinct * from ...;

It looks like there is a distinct done twice here.

To play around with this a bit, I created a simple table:

create table union_test ( value number );

Then I inserted a couple of record (with duplicate values):

insert into union_test(value) values (1);
insert into union_test(value) values (1);
insert into union_test(value) values (2);
insert into union_test(value) values (2);
insert into union_test(value) values (3);
insert into union_test(value) values (3);
insert into union_test(value) values (4);
insert into union_test(value) values (4);
insert into union_test(value) values (5);
insert into union_test(value) values (5);

Now I can run some queries to see the differences:

select * from union_test where value 

The result for this query is as follows:








VALUE
----------
1
1

2
2
3
3 6 rows selected


Note, the duplicate values. To get only unique values I need to add the distinct operator.

You can read the rest of this article at http://blog.bar-solutions.com

.php
Click Here to View all the questions in SQL and PLSQL category.
File Attachments File Attachments
There are no attachment file(s) related to this question.
User Comments User Comments
There are no user comments for this question. Be the first to post a comment. Click Here
Post Comment Add a Comment
Email Address:
Comment/Message
Verify Code

Post Comment How helpful was this article to you?
Related Questions Related Article
  1. Oracle 11G - Compound Trigger
  2. Parameters in Oracle
  3. Oracle 11G - Virtual Columns (Dates)
  4. Constraints and Row Level Security
  5. WITH Clause
  6. Ansi SQL vs Oracle SQL
  7. Simple comparison SQL
  8. Oracle 11G - Follows in Trigger
  9. Tri-state boolean
  10. Cascading Delete
  11. Conditional Compilation...
  12. Oracle 11G - Virtual Columns
  13. Recursive programming.
  14. Handy date functions
  15. A boolean property that's really an integer.
  16. Ordering Results
  17. Oracle errors
  18. Shrinking tablespaces
  19. Testing Rulegen using Codetester
  20. Functions as default values
  21. By position or by name.
  22. Defaults
  23. Open cursors
Article Information Additional Information
Article Number: 371
Created: 2008-08-14 9:11 PM
Rating No Rating
 
Article Options Article Options
Print Question Print this Question/Article
Email Question Email this Question/Article to Friend
Export to MS Word Export to MS Word
Bookmark Article
del.icio.us Bookmark del.icio.us Bookmark
Digg It Digg It
Furl It Furl It
Subscribe to Article Subscribe to Article
 
Language Translation Language Translation
 
Search Knowledge Base Search Knowledge Base