Oracle’s ROWNUM
is a pseudo column that numbers the rows in a result set. Unfortunately, it numbers the rows in the set
before ordering is applied. So combining the two in the same query won’t get you the results you expect. Instead, you should move your
selection and ordering into a subquery, and use ROWNUM
only on the outer query.
Noncompliant code example
SELECT fname, lname, deptId
FROM employee
WHERE rownum <= 10
ORDER BY salary -- Noncompliant
Compliant solution
SELECT *
FROM ( SELECT fname, lname, deptId
FROM employee
ORDER BY salary
)
WHERE rownum <= 10