Oracle Top-N Query: Are Results Guaranteed?

Most sources, including the Oracle Top-N Queries , provide syntax like the one below to execute a Top-N query:

SELECT val
FROM (
    SELECT val
    FROM rownum_order_test
    ORDER BY val DESC
)
WHERE ROWNUM <= 5;

This is guaranteed to give 5 top values; however, is it guaranteed in the correct order? This question , citing Wikipedia , indicates otherwise:

Although some database systems allow you to specify an ORDER BY clause in subqueries or view definitions, being present there has no effect.

Does this mean that the above code does not guarantee any order and requires additional ORDER BY, or is it an exceptional case?

SELECT val
FROM (
    SELECT val
    FROM rownum_order_test
    ORDER BY val DESC
)
WHERE ROWNUM <= 5
ORDER BY val;   -- Is this line necessary?

. , ( ). , , ( Oracle) ORDER BY, , .

+4
1

, , , , , , top-n.

, , ORDER BY , ORDER BY, .

, ( , ), , .

+1

Source: https://habr.com/ru/post/1658369/


All Articles