Union vs. Union All

UNION and UNION ALL both unify for add two structurally similar data sets, but UNION operation returns only the unique records from the resulting data set whereas UNION ALL will return all the rows, even if one or more rows are duplicated to each other.


In the following example, I am choosing exactly the same employee from the emp table and performing UNION and UNION ALL. Check the difference in the result.
 
 
SELECT * FROM EMPLOYEE WHERE ID = 5
UNION ALL
SELECT * FROM EMPLOYEE WHERE ID = 5
 
 
IDMGR_IDDEPT_IDNAMESALDOJ
5.02.02.0Anno80.001-Feb-2012
5.02.02.0Anno80.001-Feb-2012
 
 
SELECT * FROM EMPLOYEE WHERE ID = 5
UNION 
SELECT * FROM EMPLOYEE WHERE ID = 5
 
 
IDMGR_IDDEPT_IDNAMESALDOJ
5.02.02.0Anno80.001-Feb-2012

No comments:

Post a Comment