Vous êtes sur la page 1sur 4

Row num is being evaluated

after the records are selected


from database and before the
execution of order by clause.
Generating a row number that is a running sequence of numbers for each row is not easy
using plain SQL. In fact, the method I am going to show below is not very generic either. This
method only works if there is at least one unique column in the table. This method will also work
if there is no single unique column, but collection of columns that is unique. Anyway, here is the
query:
SELECT name, sal, (SELECT COUNT(*) FROM EMPLOYEE i WHERE o.name >= i.name)
row_num
FROM EMPLOYEE o
order by row_num
Read more at http://www.queryhome.com/28986/how-to-generate-row-number-in-sql-withoutrownum#DGPIyCGUzcMpqgeC.99

Using ROWNUM with ORDER BY


in Oracle SQL
Lot of times you have tried to use ROWNUM clause
along along with the ORDER BY clause and must have
been treated with a different output than you
expected. You would expect the ROWNUM to be
applied after the results have been ordered by the
column, but instead it applies the ROWNUM and then
does an order by.
Lets look at an example:
Support you had the following data in

a student table:

And we want to find out the youngest student, you


would immediately write:
1 SELECT * FROM student WHERE ROWNUM=1 ORDER BY age;

which would give the output:

But looking at the data above John/David would


should have been the youngest. As I said before, the
ORDER BY clause is applied after the ROWNUM
selects one row. Lets edit the query a bit and try:
1 SELECT * FROM (SELECT * FROM student ORDER BY age)WHERE ROWNUM=1;

which would give us the correct output:

The idea here is to first order the rows/records by


moving the order by query as a nested sql and then
applying ROWNUM on the resultant data.

In fact, ROWNUM only exists for a row once it is retrieved from


a query. It represents the sequential order in which Oracle has

retrieved the row. Therefore it will always exist, be at least 1,


and be unique (among the rows returned by the query).
Obviously it will change from query-to-query. Let's look at a
quick example:
scott@Robert> SELECT ROWNUM, ENAME, SAL
2 FROM EMP;
ROWNUM
---------1
2
3
4
5
6
7
8
9
10
11
12
13
14

ENAME
SAL
---------- ---------SMITH
800
ALLEN
1600
WARD
1250
JONES
2975
MARTIN
1250
BLAKE
2850
CLARK
2450
SCOTT
3000
VOLLMAN
5000
TURNER
1500
ADAMS
1100
JAMES
950
FORD
3000
MILLER
1300

Ok so let's say we want the 5 highest paid employees. Should


be easy:
scott@Robert> SELECT ROWNUM, ENAME, SAL
2 FROM EMP
3 WHERE ROWNUM < 6
4 ORDER BY SAL DESC;
ROWNUM
---------4
2
3
5
1

ENAME
SAL
---------- ---------JONES
2975
ALLEN
1600
WARD
1250
MARTIN
1250
SMITH
800

Whoops! Turns out ROWNUM is assigned before results are


ordered, not after. Knowing that, we can write it like this:
scott@Robert> SELECT ENAME, SAL
2 FROM (SELECT ENAME, SAL FROM EMP ORDER BY SAL DESC) E
3 WHERE ROWNUM < 6;
ENAME

SAL

---------- ---------VOLLMAN
5000
SCOTT
3000
FORD
3000
JONES
2975
BLAKE
2850

Vous aimerez peut-être aussi