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
ID | MGR_ID | DEPT_ID | NAME | SAL | DOJ |
---|---|---|---|---|---|
5.0 | 2.0 | 2.0 | Anno | 80.0 | 01-Feb-2012 |
5.0 | 2.0 | 2.0 | Anno | 80.0 | 01-Feb-2012 |
SELECT * FROM EMPLOYEE WHERE ID = 5 UNION SELECT * FROM EMPLOYEE WHERE ID = 5
ID | MGR_ID | DEPT_ID | NAME | SAL | DOJ |
---|---|---|---|---|---|
5.0 | 2.0 | 2.0 | Anno | 80.0 | 01-Feb-2012 |