Skip to content
Advertisement

Use rownum to indicate what rows I have pulled back

I have table of people, with firstname and lastname.

firstname lastname
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j

I want to query from a specific point. For example.

SELECT FIRSTNAME, LASTNAME WHERE FIRSTNAME > 'e' order by firstname, lastname

Great. But what I would really like is for the result to tell me that result is actually from the 6th row. I have tried

SELECT FIRSTNAME, LASTNAME, ROW_NUMBER(OVER ORDER BY lastname) WHERE FIRSTNAME > 'e' order by firstname, lastname

But in that case, row_number will start from 1 when I want it to tell me that is actually the 6th element from the query

SELECT FIRSTNAME, LASTNAME order by firstname, lastname

I want to do that without actually retrieving the first five rows.

Thanks

Advertisement

Answer

You can use a subquery to enumerate the rows, then filter in the outer query:

select *
from (
    select firstname, lastname, row_number() over(order by firstname, lastname) as rn
    from mytable
) t
where firstname > 'e' 
order by firstname, lastname
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement