What is the difference between 2 queries?
When you combine multiple queries with union, the column aliases are determined by the first query.
An order by
clause applies to the entire result set and must refer to the columns as defined by the first query in the set of unioned queries.
Your second example works because you are not applying order by
to the unioned results, you are using it within the context of a derived table which is fine, it applies to that sub-query only.