On Thursday, July 14, 2022, Shay Rojansky <
roji@roji.org> wrote:
If there's a guarantee that UNION ALL preserves ordering - as Tom seems to indicate in the thread quoted above - then the above works. If there's no such guarantee, then AFAIK the above can't be rewritten; putting the ORDER BY outside - on the results of the UNION ALL - would order all results rather than preserving each resultset's ordering.
Yes, an order by outside the union will sort the union results as a whole. You can still write an order by and the union all so you get any conceivable ordering, though it may possibly require putting the union into a subquery depending on the order and output column combination desired.
David J.