Reputation: 51
I ran the following query by accident and it works, and now im' confused as to why it works. Can someone please explain how to interpret this query...
(select name from employe_info limit 100) order by name;
I always assumed the 'order by' has to be associate with an explicit select clause, but in this case it's outside the () and it works. This is probably not a good form, but i just want to know if this works by accident or is this expected?
Thanks!
Upvotes: 3
Views: 482
Reputation: 5534
You just apply sorting to subquery See more in manual: https://dev.mysql.com/doc/refman/5.5/en/select.html
If ORDER BY occurs within a subquery and also is applied in the outer query, the outermost ORDER BY takes precedence. For example, results for the following statement are sorted in descending order, not ascending order:
(SELECT ... ORDER BY a) ORDER BY a DESC;
Upvotes: 2
Reputation: 852
Looks like that is only a partial query.
From UNION Syntax:
To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT
and a paragraph later:
To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one.
Upvotes: 0