Oracle SQL does not support anything like LIMIT in MySQL. You can just limit the
number of rows returned with rownum. So if you only want the first ten
records use:
SELECT * FROM table WHERE ROWNUM<10;
However the following won't work:
SELECT * FROM table WHERE ROWNUM>90 AND ROWNUM<100;
This is because Oracle checks for the first row and the condition is false,
as the rownum would be 1. So this row is not returned, thus the rownum is
still 0. So the rownum will never get to 90.
You can try :
SELECT * FROM table WHERE ROWNUM<101;
minus
SELECT * FROM table WHERE ROWNUM<91;
Enrico Faccin wrote :321
You can limit the search on both side with this
nested query:
SELECT * FROM (
SELECT *, ROWNUM
FROM mytable
WHERE myconditions
) WHERE ROWNUM>xx AND ROWNUM<yy;
Marcelo Toledo wrote :614
If I do something like: select * from (select aaa, bbb, ROWNUM as X from table where aaa=bbb order by aaa desc) where X = 2;
If I want to get the second row returned this will not work as expected. Look why:
DATA_ENTRADA ROWNUM
------------------- ----------
16/05/2001 12:57:08 32 <- this is the firt row
16/05/2001 11:51:45 31 <- second
15/05/2001 18:05:53 30
15/05/2001 18:04:47 29
15/05/2001 18:04:10 28
So, to get the real row number in this case you will have to make 3 selects. For example:
select * from (select a, ROWNUM as X from (select a from table where ... order by ...)) where X = 2;
Marcelo Toledo wrote :615
If I do something like: select * from (select aaa, bbb, ROWNUM as X from table where aaa=bbb order by aaa desc) where X = 2;
If I want to get the second row returned this will not work as expected. Look why:
DATA_ENTRADA ROWNUM
------------------- ----------
16/05/2001 12:57:08 32 <- this is the firt row
16/05/2001 11:51:45 31 <- second
15/05/2001 18:05:53 30
15/05/2001 18:04:47 29
15/05/2001 18:04:10 28
So, to get the real row number in this case you will have to make 3 selects. For example:
select * from (select a, ROWNUM as X from (select a from table where ... order by ...)) where X = 2;
Didier Berard wrote :1056
Hier is a full example for the limitation:
select * from (select a, b, c, rownum as limit from mytable where conditions order by whatiwant) where limit>x and limit <y;
with: whatiwant="a DES"C, x=3 and y=6
the result will be:
A B C LIMIT
--- --- --- ----------
30 1 2 4
29 2 2 5