473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Working of ROWNUM in Oracle

102 Expert 100+
Hi All,
I was just going through EMP table in oracle Scott schema.

Say, I want to find out the first employee in alphabetical order who's name starts with 'A'. the following query gives me the desired result:
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM (  SELECT ENAME 
  3.         FROM EMP
  4.         WHERE UPPER(ENAME) LIKE 'A%'
  5.         ORDER BY ENAME ASC
  6.         )
  7. WHERE ROWNUM=1
  8.  
Now if i want to find out the second employee in alphabetical order who's name starts with 'A'. I modified the query as:
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM (  SELECT ENAME 
  3.         FROM EMP
  4.         WHERE UPPER(ENAME) LIKE 'A%'
  5.         ORDER BY ENAME ASC
  6.         )
  7. WHERE ROWNUM=2
  8.  
But this does not give me any rows although there are many employees whos names start with 'A'.

Please let me know what could be the problem in this case.

Thanks,
Pradeep
Nov 21 '07 #1
6 14061
amitpatel66
2,367 Expert 2GB
Hi Pradeep,

ROWNUM are assigned every time when the query is executed and it starts with 1.

You cannot work the way you tried in second query to get second employee.

Try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM (  SELECT ENAME,rownum rn
  3.         FROM EMP
  4.         WHERE UPPER(ENAME) LIKE 'A%'
  5.         ORDER BY ENAME ASC
  6.         )
  7. WHERE rn = 2
  8.  
Nov 21 '07 #2
pradeep kaltari
102 Expert 100+
Hi Pradeep,

ROWNUM are assigned every time when the query is executed and it starts with 1.

You cannot work the way you tried in second query to get second employee.

Try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM (  SELECT ENAME,rownum rn
  3.         FROM EMP
  4.         WHERE UPPER(ENAME) LIKE 'A%'
  5.         ORDER BY ENAME ASC
  6.         )
  7. WHERE rn = 2
  8.  
Hi amit,
Thanks for your reply.

I agree that the query you posted would give the results, I also know there are other methods to get the same result.

What I wanted to know is how exactly ROWNUM works. If i can say ROWNUM=1, then why doesn't ROWNUM=2 work??

- Pradeep
Nov 21 '07 #3
amitpatel66
2,367 Expert 2GB
Hi amit,
Thanks for your reply.

I agree that the query you posted would give the results, I also know there are other methods to get the same result.

What I wanted to know is how exactly ROWNUM works. If i can say ROWNUM=1, then why doesn't ROWNUM=2 work??

- Pradeep
Pradeep,

rownum is assigned to rows AS THEY SATISFY the predicate.

the logic would be:

Expand|Select|Wrap|Line Numbers
  1.    rownum = 1
  2.    for x in ( select * from A )
  3.    loop
  4.        if ( x satisifies the predicate ) 
  5.        then
  6.              OUTPUT the row
  7.              rownum = rownum + 1
  8.        end if;
  9.    end loop;
  10.  
in the case of where rownum = 1, the first row passes the test, is output and rownum goes
to 2. No other row ever satisfies the predicate and rownum stays at 2 for the rest of
the query.

in the case of where rownum = 2, the first row is rownum 1, it fails. The next row is
ALSO rownum = 1 and likewise fails. And so on. There can be NO row 2 if there is not a
row 1.

THE ROWNUM -- is incremented only AFTER the row is output
Nov 21 '07 #4
Hi Pradeep,

ROWNUM are assigned every time when the query is executed and it starts with 1.

You cannot work the way you tried in second query to get second employee.

Try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM (  SELECT ENAME,rownum rn
  3.         FROM EMP
  4.         WHERE UPPER(ENAME) LIKE 'A%'
  5.         ORDER BY ENAME ASC
  6.         )
  7. WHERE rn = 2
  8.  
/************************************************** *************************************/

HI IN ORACLE 8i WE CANT ABLE TO USE = OPERATOR FOR ROWNUM SO U CAN TRY USING <,<=

SELECT *
FROM ( SELECT ENAME,rownum rn
FROM EMP
WHERE UPPER(ENAME) LIKE 'A%'
ORDER BY ENAME ASC
)
WHERE rn <= 2
Dec 3 '07 #5
pradeep kaltari
102 Expert 100+

Expand|Select|Wrap|Line Numbers
  1.    rownum = 1
  2.    for x in ( select * from A )
  3.    loop
  4.        if ( x satisifies the predicate ) 
  5.        then
  6.              OUTPUT the row
  7.              rownum = rownum + 1
  8.        end if;
  9.    end loop;
  10.  

THE ROWNUM -- is incremented only AFTER the row is output
This is exactly what I was looking for!!

Thanks Amit.
Dec 4 '07 #6
Thank you. Got the needed info
Oct 7 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paul Bradford | last post by:
I have a query that takes about two minutes and returns 97 rows. If I change the query only by adding ROWNUM to the outermost select clause, the query never returns (I let it run overnight). I'm...
9
by: Sandy | last post by:
I am trying to do the following: EXEC SQL UPDATE MY TABLE SET COL1 = :newValue WHERE COL1 = 0 AND ROWNUM = 1 ORDER BY COL2; (index on COL1, COL2)
4
by: pierig.gueguen | last post by:
Hello, I would like to know if the equivalent Oracle rownum exist in SQLServer. Here is a sample SQL code to explain what I want to do : select jobs.name, jobs.job_id, jobs.description,...
7
by: Terren | last post by:
The following code is not working properly in IE6 but it does work for mozilla firefox. I am trying to open a new popup window without affecting the frame from "where" it came. But what happens...
1
by: jimb | last post by:
I can get the dropdownlist into the datagrid, and I can populate it, but I can't read it. Anybody have a working example of a dropdownlist in an editable grid? Thanks. -- .....
3
by: Reshmi Jacob | last post by:
Hi all I would like to know the order in which the records are saved in a table. I am a beginner in SQL, in Oracle we have the ROWNUM to retrive the same. Any option for this in SQL Server??? ...
2
by: krishhhna | last post by:
If we want to retrive 5,6,7 rows from a table,then we used rownum in a query in group by clause,but my doubt is rownum has individual values for each rows then how did you group rows based on...
1
by: joshua.h.song | last post by:
hi all. i need to convert the following oracle-based sql to DB2. SELECT rownum, name, birthday FROM person WHERE rownum < 10 could someone convert for me?
0
by: Walt | last post by:
Darta wrote: I haven't read the question in detail, so this may be way off base, but have you considered using rowid instead of rownum? rownum is a psuedo-column that's a bit "slippery", i.e....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.