QUERY type values (QTVs) are like dynamic view definitions, and ORDER BY in a QTV is thus ignored if there is no LIMIT clause, since a view is logically a rowset without a specified order. Suppose you have a QTV like this:
declare q query(…)= SELECT a, b from t order by b;
Then if you want ordering on b in your final result, you need to do something like this: