How does Oracle order the results of a union?
In Oracle, the UNION operator is used to combine the results of two or more SELECT statements. The UNION operator by default returns results in the order of the queries without any sorting.
If you want to sort the merged result set, you can either use a subquery or use the ORDER BY clause.
Using subqueries:
SELECT * FROM (
SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2
) ORDER BY column1;
Use the ORDER BY clause:
SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2
ORDER BY column1;
Both of these methods can sort the merged result set by column1.