473,625 Members | 3,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Working of ROWNUM in Oracle

102 Recognized Expert New Member
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 14073
amitpatel66
2,367 Recognized Expert Top Contributor
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 Recognized Expert New Member
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 Recognized Expert Top Contributor
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
Dharmaraju
13 New Member
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 Recognized Expert New Member

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
3975
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 not using ROWNUM in the where clause, I'm just selecting it. I can't think of any reason that just selecting ROWNUM would make any difference. This is Orace 8.1 I spent a day searching Google groups without finding anything on this. Any ideas?
9
98870
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
18274
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, hist.message,
7
2762
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 when I click on the link is that the popup window is displayed correctly but the frame then changes to my default.htm. It is supposed to stay the same here is my code
1
2357
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. -- .. http://sf-f.org, weblog and search engine for fans and writers of
3
5144
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??? Regards Reshmi
2
36220
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 rownum?
1
20801
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
1805
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. it's value changes depending on the where clause, so putting it in the where clause can cause unexpected behavior. Rowid doesn't have this problem; it is what it is independent of what you do with the where clause. I generally avoid using...
0
8256
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7184
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.