views:

329

answers:

8

Is there any reason why or why not you should do an 'order by' in a subquery?

+6  A: 

You should use it if the subquery uses some kind of LIMIT / TOP.

SQL Server will not allow it unless the subquery contains TOP or FOR XML clause as well:

-- Fails
WITH    q(id) AS
        (
        SELECT  1
        UNION ALL
        SELECT  2
        )
SELECT  *
FROM    (
        SELECT  *
        FROM    q
        ORDER BY
                id DESC
        ) q2

-- Succeeds
WITH    q(id) AS
        (
        SELECT  1
        UNION ALL
        SELECT  2
        )
SELECT  *
FROM    (
        SELECT  TOP 1 *
        FROM    q
        ORDER BY
                id DESC
        ) q2

-- Succeeds, but ORDER BY is ignored
WITH    q(id) AS
        (
        SELECT  1
        UNION ALL
        SELECT  2
        )
SELECT  *
FROM    (
        SELECT  TOP 100 PERCENT *
        FROM    q
        ORDER BY
                id DESC
        ) q2
Quassnoi
+1  A: 

You can do it, but I wouldn't usually unless you have a need.

The optimiser will ignore it (or throw an error?)

See "Intermediate materialisation" for some usages.

gbn
+1  A: 

Depending on the size of the sub-query, it will impact performance to a varrying degree.

Order shouldn't matter on a sub-query though. You should be able to move the Order By portion to the Outer Query (which should be the one returning the final results).

Justin Niessner
+7  A: 

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...

sleske
A: 

It's totally legit. I.e. SELECT id FROM entries WHERE author_id IN (SELECT id FROM authors ORDER BY name DESC) but you'll really get the same results usually.

loginx
Usually? Can you give any case where the results would differ?
sleske
It will differ if you add a LIMIT statement to your subselect. In this case, if you want to see all entries from the top 5 authors by name, you would change your subselect to (SELECT id FROM authors ORDER BY name DESC LIMIT 5).
loginx
@loginx: True. With TOP/LIMIT it makes a difference (and is necessary, sometimes even required). Otherwise, it does not make a difference.
sleske
Please note that `MySQL` does not support `ORDER BY ... LIMIT` in the `IN` subqueries.
Quassnoi
+2  A: 

Unless you use top it is not useful since you will be ordering in the outer query anyway

SQLMenace
A: 

No ORDER BY is valid in a subquery when you are interested in a subset of the overall data, hence you always need a TOP (SQL Server). There's no point having an ORDER BY without TOP in a subquery because the overall ordering of the results is handled by the outer query.

AdaTheDev
A: 

You should not use it. According to the 'Art of SQL', this actually prevents the optimizer from performing various optimizations that it could otherwise do because this pre-maturely transforms the data.

j0rd4n