Order by With Union SQL

If I get your question correct, you want to order the indivual SELECTs seperately and you cannot use two ORDER BY clause per each statement instead, add an extra integer column to each SELECT as shown below:

SELECT *, 1 as orderBy from t1 
   UNION ALL
SELECT *, 2 as orderBy from t2
ORDER BY orderBy desc. 

Leave a Comment