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: -
SELECT *
-
FROM ( SELECT ENAME
-
FROM EMP
-
WHERE UPPER(ENAME) LIKE 'A%'
-
ORDER BY ENAME ASC
-
)
-
WHERE ROWNUM=1
-
Now if i want to find out the second employee in alphabetical order who's name starts with 'A'. I modified the query as: -
SELECT *
-
FROM ( SELECT ENAME
-
FROM EMP
-
WHERE UPPER(ENAME) LIKE 'A%'
-
ORDER BY ENAME ASC
-
)
-
WHERE ROWNUM=2
-
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
6 14073
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: -
SELECT *
-
FROM ( SELECT ENAME,rownum rn
-
FROM EMP
-
WHERE UPPER(ENAME) LIKE 'A%'
-
ORDER BY ENAME ASC
-
)
-
WHERE rn = 2
-
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: -
SELECT *
-
FROM ( SELECT ENAME,rownum rn
-
FROM EMP
-
WHERE UPPER(ENAME) LIKE 'A%'
-
ORDER BY ENAME ASC
-
)
-
WHERE rn = 2
-
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
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: -
rownum = 1
-
for x in ( select * from A )
-
loop
-
if ( x satisifies the predicate )
-
then
-
OUTPUT the row
-
rownum = rownum + 1
-
end if;
-
end loop;
-
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
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: -
SELECT *
-
FROM ( SELECT ENAME,rownum rn
-
FROM EMP
-
WHERE UPPER(ENAME) LIKE 'A%'
-
ORDER BY ENAME ASC
-
)
-
WHERE rn = 2
-
/*************** *************** *************** *************** *************** ************/
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
-
rownum = 1
-
for x in ( select * from A )
-
loop
-
if ( x satisifies the predicate )
-
then
-
OUTPUT the row
-
rownum = rownum + 1
-
end if;
-
end loop;
-
THE ROWNUM -- is incremented only AFTER the row is output
This is exactly what I was looking for!!
Thanks Amit.
Thank you. Got the needed info
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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)
|
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,
|
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
|
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
| |
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
|
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?
|
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?
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |