You can specify an ORDER BY clause before or after a WITH clause.
When you specify both WITH and ORDER BY clauses in the same SELECT request, the clauses function together as follows:
- The WITH clause defines the major sort key.
- The ORDER BY clause defines the minor sort key, regardless of the structure of the query or the number of WITH clauses. See Example: Combining WITH and ORDER BY Clause.