Print last 3 rows in a table
How to retreive last 3 rows from a table which is not sorted in any order
upto my knowledge this is one way
select empno from emp where empno in (select top 3 empno from emp order by desc empno)
here we are sorting the rows and retreving the last 3 records is there any way to retrieve last 3 records with out sorting the table.
Any help is appreciated
Thanks vinay
Re: Print last 3 rows in a table
hello vinay
as per my knowledge i prepared a query.i think this will work for u.
[code]
select * from emp
minus
select * from emp where rownum<=(select (count(*)-3) from emp);
[/code]
thanxs vijaya
Re: Print last 3 rows in a table
[code]
select rownum,emp.* from emp
minus
select rownum,emp.* from emp where rownum<=(select (count(*)-3) from emp);
[/code]
This will fetch the exact last 3 rows from emp table. If we include rownum as a part of resultset; it will give the output as order by rownum.
Re: Print last 3 rows in a table
hi plss try it with yr table
with t as
(
select rownum as num,t.* from your_table t
order by rownum desc
)
select t.*from t
where rownum<&desired_row
order by rownum
Re: Print last 3 rows in a table
a change
with t as
(
select rownum as num,t.* from your_table t
order by rownum desc
)
select t.*from t
where num<&desired_row
order by rownum
Re: Print last 3 rows in a table
Here is my solution for ur query
try this.......
select * from
(select rownum, e.* from dept e order by rownum desc )
where rownum<=3