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 14061
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...
|
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,...
|
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...
|
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.
--
.....
|
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???
...
|
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...
|
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....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |