Results 1 to 9 of 9

Thread: Retrieve last record

  1. #1
    Junior Member
    Join Date
    Dec 2007
    Answers
    2

    Retrieve last record

    How to retrieve last record from a table
    for eg we have emp table from that i wan to retriew last inserted record without seeing or knowing how can i get it


  2. #2
    Contributing Member
    Join Date
    Nov 2007
    Answers
    53

    Re: Retrieve last record

    Dear Friend,

    Try this,

    SELECT * FROM (
    SELECT ROWNUM RN,A.* FROM TABLE_NAME A WHERE ROWNUM < (SELECT COUNT(*)+1 FROM TABLE_NAME) )
    WHERE RN = (SELECT count(*) FROM TABLE_NAME)

    Have a pleasant time.


  3. #3
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    Re: Retrieve last record

    Thank your for your query sreekumar.
    What is the use of
    WHERE ROWNUM < (SELECT COUNT(*)+1 FROM TABLE_NAME
    in the above query.We can get output even without this where clause.

    Last edited by krishnaindia2007; 12-24-2007 at 11:03 PM.

  4. #4
    Contributing Member
    Join Date
    Nov 2007
    Answers
    53

    Re: Retrieve last record

    Quote Originally Posted by krishnaindia2007 View Post
    Thank your for your query sreekumar.
    What is the use of
    WHERE ROWNUM < (SELECT COUNT(*)+1 FROM TABLE_NAME
    in the above query.We can get output even without this where clause.
    Dear Krishna,

    Thank you for your reply.

    Yes you are right, we can get the output without
    WHERE ROWNUM < (SELECT COUNT(*)+1 FROM TABLE_NAME

    Actually i was first selecting all rows using the above WHERE clause.
    Now from this list, i was selecting the last row. COUNT(*)+1 was used
    just to select all rows. Because if i use only count(*) then i might no get
    the last row, thats why.

    Well your query is right.

    Thank you once again.

    Have a pleasant time.

    Merry Christmas


  5. #5
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    Re: Retrieve last record

    Try this also
    select * from emp
    minus
    select * from emp where ROWNUM < (select max(rownum) from emp)


  6. #6
    Junior Member
    Join Date
    Dec 2007
    Answers
    3

    Re: Retrieve last record

    In sql server hw we will do that


  7. #7
    Junior Member
    Join Date
    Dec 2007
    Answers
    3

    Re: Retrieve last record

    Quote Originally Posted by susarlasireesha View Post
    Try this also
    select * from emp
    minus
    select * from emp where ROWNUM < (select max(rownum) from emp)
    in sql server hw we will do this


  8. #8
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    Re: Retrieve last record

    Quote Originally Posted by amisha_me View Post
    in sql server hw we will do this
    for sql i posted above query ,for sql server use this
    select top 1 * from TABLE_NAME order by ID desc


  9. #9
    Junior Member
    Join Date
    Dec 2007
    Answers
    3

    Re: Retrieve last record

    Quote Originally Posted by susarlasireesha View Post
    for sql i posted above query ,for sql server use this
    select top 1 * from TABLE_NAME order by ID desc
    select * from emp
    minus
    (select * from emp where ROWNUM < (select top 1 * from productstate order by productid desc)

    There is no TOP keyword in awl server


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