MySQL/MariaDB - order by inside subquery
Solution 1:
After a bit of digging, I can confirm both your scenarios:
MySQL 5.1 does apply the ORDER BY
inside the subquery.
MariaDB 5.5.39 on Linux does not apply the ORDER BY
inside the subquery when no LIMIT
is supplied. It does however correctly apply the order when a corresponding LIMIT
is given:
SELECT t2.Code
FROM (
SELECT Country.Code FROM Country ORDER BY Country.Code DESC LIMIT 2
) AS t2;
Without that LIMIT
, there isn't a good reason to apply the sort inside the subquery. It can be equivalently applied to the outer query.
Documented behavior:
As it turns out, MariaDB has documented this behavior and it is not regarded as a bug:
A "table" (and subquery in the
FROM
clause too) is - according to the SQL standard - an unordered set of rows. Rows in a table (or in a subquery in theFROM
clause) do not come in any specific order. That's why the optimizer can ignore theORDER BY
clause that you have specified. In fact, SQL standard does not even allow theORDER BY
clause to appear in this subquery (we allow it, becauseORDER BY ... LIMIT
... changes the result, the set of rows, not only their order).You need to treat the subquery in the
FROM
clause, as a set of rows in some unspecified and undefined order, and put theORDER BY
on the top-levelSELECT
.
So MariaDB also recommends applying the ORDER BY
in the outermost query, or a LIMIT
if necessary.
Note: I don't currently have access to a proper MySQL 5.5 or 5.6 to confirm if the behavior is the same there (and SQLFiddle.com is malfunctioning). Comments on the original bug report (closed as not-a-bug) suggest that MySQL 5.6 probably behaves the same way as MariaDB.
Solution 2:
In newer versions of MySQL and MariaDB you can force the ORDER BY in a sub query by applying a LIMIT. If you don't want to limit the rows, use the biggest number of BIGINT as a LIMIT.
This may come in handy at times, when the sub query needs to be generated in a desired order, for applying line numbers, for example.