Rownum [message #560973] |
Wed, 18 July 2012 01:16 |
|
satheesh_ss
Messages: 61 Registered: July 2012
|
Member |
|
|
A table has 150 records. How do you retrieve 100th row to 120th row from that table ?
|
|
|
|
|
|
|
|
|
Re: Rownum [message #561007 is a reply to message #561006] |
Wed, 18 July 2012 04:39 |
_jum
Messages: 577 Registered: February 2008
|
Senior Member |
|
|
or with analytic function only two SELECT:
SELECT *
FROM
(SELECT ROW_NUMBER () OVER (ORDER BY sal DESC) rn, e.* FROM emp e)
WHERE rn BETWEEN 1 AND 3;
|
|
|
|
|
|
Re: Rownum [message #642889 is a reply to message #642886] |
Tue, 22 September 2015 08:44 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Actually that one will work, but this one won't:
SELECT ROWNUM RN, e.* from emp e where RN BETWEEN 2 AND 3
It's because of the point at which rownum is assigned - It's assigned after the row is fetched from the table and compared to the where clause. You can't get a row with rownum = 2 until you've successfully selected a row with rownum = 1. Having an inner select allows that to happen.
Also, to re-iterate what Maaher pointed out above - a query that restricts by rownum that doesn't have an order by clause makes no sense - you're just selecting random rows unless you add order by.
|
|
|
|
|
Re: Rownum [message #642899 is a reply to message #560997] |
Tue, 22 September 2015 09:01 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
satheesh_ss wrote on Wed, 18 July 2012 04:38Thanks for your Answer Mr.Muralikri.....Its working
You didn't provide Oracle version. Oracle 12C added new row limiting feature (although under the hood it is still same old ROWNUM + nesting similar to Muralikri's solution):
SELECT *
FROM your_table
OFFSET 99 ROWS FETCH NEXT 21 ROWS ONLY;
However, and many already noted that, without ORDER BY the above query is not deterministic and can return different set of 21 rows next time you run it.
SY.
|
|
|