Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

desc nulls first in row_number

user13328581Jul 5 2016 — edited Jul 5 2016

hello experts;

I was just wondering is the use of nulls first in the row_number really necessary. I tested it out in the sample data below and it gives me the same result in both cases

case 1 using nulls first

with t as

(

select null as id, 1 as ver from dual

union all

select 1 as id, 1 as ver from dual

union all

select 1 as id, 2 as ver from dual

)

,p as (

select id

,row_number() over (partition by id order by ver desc nulls first) as rn

from t)

select * from p

where p.rn = 1

results

ID  RN

1    1

null  1

case 2 without using nulls first

with t as

(

select null as id, 1 as ver from dual

union all

select 1 as id, 1 as ver from dual

union all

select 1 as id, 2 as ver from dual

)

,p as (

select id

,row_number() over (partition by id order by ver desc) as rn

from t)

select * from p

where p.rn = 1

ID  RN

1    1

null  1

This post has been answered by Frank Kulash on Jul 5 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 2 2016
Added on Jul 5 2016
3 comments
2,465 views