Is there any reason why or why not you should do an ‘order by’ in a subquery?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes: It should not be done, because it does not make sense conceptually.
The subquery will be used in some outer query (otherwise it would be pointless), and that outer query will have to do ordering anyway, so there’s no point ordering the subquery.
This is because query results in SQL will come in no particular order, unless you use an explicit ORDER. So even if you used ORDER in the subquery, you have no guarantee that this will affect the order of the results from the outer query; so it’s pointless.
It may of course make a difference in some specific RDBMS because of its implementation, but that will be implementation-specific, and not something you should rely on.
Edit: Of course, if you use TOP or LIMIT in the subquery, you will need to use ORDER. But that’s not standard SQL anyway…