472,342 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Difference between UNION and UNION ALL

rsrinivasan
221 100+
Hi,
What is the difference between UNION and UNION ALL.
I refered some document. But i did not get any clear idea on that.

Thanks,
May 24 '07 #1
4 76255
MMcCarthy
14,534 Expert Mod 8TB
Hi,
What is the difference between UNION and UNION ALL.
I refered some document. But i did not get any clear idea on that.

Thanks,
The main difference is that UNION will eliminate any duplicate rows returned by the various select statements being unioned whereas UNION ALL will not. It will return all records even duplicates.
May 24 '07 #2
Dear Srinivas,
The union and union all are set operators. The main diffrence in btw Union and Union all is ,The Union will Display all rows selected my either queries but not duplicates where as union all will display all the rows including duplicates.
Its Better to Refer IVAN BAYROSS book u will find much information in that book.

regards
sandhya
May 25 '07 #3
Union eliminate duplicates, union all doesn’t eliminate dups..

Ex:

Expand|Select|Wrap|Line Numbers
  1.  
  2. TABLE_1 :
  3.  
  4. SELECT * FROM DB2.*.Table1
  5. WHERE NUM = '400005898'                                   WITH UR;                                                                
  6. ---------+---------+---------+---------+---------+---------+---------+-        
  7. NUMBER   APPLIED_DTSTMP              EFF_DT      EXP_DT      
  8. ---------+---------+---------+---------+---------+---------+---------+-        
  9. 191721709  2006-05-10-11.09.39.164189  05/10/2006  02/14/1997          191721709  2006-05-10-11.09.32.452921  05/10/2006  02/14/1997  
  10.  
  11. NUMBER OF ROWS DISPLAYED IS 2                                          
  12.  
  13. TABLE_2 :
  14.  
  15. SELECT * FROM DB2.*.Table2 
  16. WHERE NUM = '400005898'                                  
  17. WITH UR;                                                               
  18. ---------+---------+---------+---------+---------+---------+---------+-
  19. NUMBER  APPLIED_DTSTMP              EFF_DT      EXP_DT      
  20. ---------+---------+---------+---------+---------+---------+---------+-
  21.         191721709  2006-05-10-11.09.39.164189  05/10/2006  02/14/1997  
  22.         191721709  2006-05-10-11.09.32.452921  05/10/2006  02/14/1997  
  23.         191721709  2002-03-14-09.46.20.512614  03/14/2002  02/14/1997  
  24.         191721709  2002-03-14-09.46.17.356447  03/14/2002  02/14/1997  
  25.         191721709  1997-10-10-23.05.28.750012  02/14/1997  02/14/1997  
  26.         191721709  1997-10-10-23.05.28.750011  02/14/1997  ----------  
  27. DSNE610I NUMBER OF ROWS DISPLAYED IS 6                                          
  28.  
  29. UNION ALL :
  30.  
  31. SELECT * FROM DB2.*.Table1
  32. WHERE NUM = '400005898'                                
  33. UNION ALL                                                            
  34. SELECT * FROM DB2.*.Table2
  35. WHERE NUM = '400005898'                                
  36. WITH UR;                                                             
  37. ---------+---------+---------+---------+---------+---------+---------
  38. NUMBER  APPLIED_DTSTMP              EFF_DT      EXP_DT    
  39. ---------+---------+---------+---------+---------+---------+---------
  40.         191721709  2006-05-10-11.09.39.164189  05/10/2006  02/14/1997
  41.         191721709  2006-05-10-11.09.32.452921  05/10/2006  02/14/1997
  42.         191721709  2006-05-10-11.09.39.164189  05/10/2006  02/14/1997
  43.         191721709  2006-05-10-11.09.32.452921  05/10/2006  02/14/1997
  44.         191721709  2002-03-14-09.46.20.512614  03/14/2002  02/14/1997
  45.         191721709  2002-03-14-09.46.17.356447  03/14/2002  02/14/1997
  46.         191721709  1997-10-10-23.05.28.750012  02/14/1997  02/14/1997
  47.         191721709  1997-10-10-23.05.28.750011  02/14/1997  ----------
  48. DSNE610I NUMBER OF ROWS DISPLAYED IS 8                               
  49.  
  50.  
  51. UNION :
  52.  
  53. SELECT * FROM DB2.*.Table1
  54. WHERE NUM = '400005898'                                
  55. UNION                                                                
  56. SELECT * FROM DB2.*.Table2
  57. WHERE NUM = '400005898'                                
  58. WITH UR;                                                             
  59. ---------+---------+---------+---------+---------+---------+---------
  60. NUMBER  APPLIED_DTSTMP              EFF_DT      EXP_DT    
  61. ---------+---------+---------+---------+---------+---------+---------
  62.         191721709  1997-10-10-23.05.28.750011  02/14/1997  ----------
  63.         191721709  1997-10-10-23.05.28.750012  02/14/1997  02/14/1997
  64.         191721709  2002-03-14-09.46.17.356447  03/14/2002  02/14/1997
  65.         191721709  2002-03-14-09.46.20.512614  03/14/2002  02/14/1997
  66.         191721709  2006-05-10-11.09.32.452921  05/10/2006  02/14/1997
  67.         191721709  2006-05-10-11.09.39.164189  05/10/2006  02/14/1997
  68. DSNE610I NUMBER OF ROWS DISPLAYED IS 6
  69.  
  70.  
Feb 29 '08 #4
The common answer is already available to you. One more thing you should definitely know is .. UNION ALL is faster as it doesnt involve sorting rows etc. Mere UNION is too costly as it has to sort all the results to remove redundant rows. HTH.
Oct 11 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: JasBascom | last post by:
if validdata is a ifstream object of mode type binary. ifstream validdata; fstream sortfile; how would I read a union. union Allrecords...
6
by: Ioannis Vranos | last post by:
In the union union test { int i; int j; }a; Is there any guarantee that a.i and a.j share the same memory address? In the standard it is...
5
by: NAJH | last post by:
I've been trying to do a union with a subquery - I've made a different example which follows the same principles as follows: First bit brings...
3
by: Dalan | last post by:
From reading Access 97 help text, it seems that to do what I need to do will require a Union Query. As this would be my first, I think I might...
2
by: sumit.sharma | last post by:
Hi, I have some experience in C prog language. I have small doubt abt using unions in C language. I have the foll. union union MyUnion {...
0
by: jimmyshaw83 | last post by:
I am sorry if this topic is tired, but after all that I have read I am still in the woods here. I have built a database to handle weekly and...
1
by: RoSsIaCrIiLoIA | last post by:
I have a union union r32{ uint8_t l; uint16_t x; uint32_t val; }; union r32 reg; and a function
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
50
by: Mikhail Teterin | last post by:
Hello! The sample program below is compiled fine by gcc (with -Wall), but rejected by Sun's SUNWspro compiler (version 6 update 2). The point...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.