Results 1 to 6 of 6

Thread: Print last 3 rows in a table

  1. #1
    Junior Member
    Join Date
    Jun 2007
    Answers
    4

    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


  2. #2
    Contributing Member
    Join Date
    Sep 2007
    Answers
    35

    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);
    thanxs vijaya

    Last edited by debasisdas; 01-16-2008 at 08:28 AM. Reason: added code tags

  3. #3
    Junior Member
    Join Date
    Nov 2007
    Answers
    1

    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);
    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.

    Last edited by debasisdas; 01-16-2008 at 08:29 AM. Reason: added code tags

  4. #4
    Contributing Member
    Join Date
    Apr 2006
    Answers
    46

    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


  5. #5
    Contributing Member
    Join Date
    Apr 2006
    Answers
    46

    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


  6. #6
    Junior Member
    Join Date
    Feb 2008
    Answers
    3

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact