query

  • how to retrieve last 5 records from emp table?

  • Sri, sorry for saying this, but your question is one of the vaguest i have seen in my stint with SSC..

    There is absolutely no information about the emp table in your post, but you have thrown a frisbee to us to find something..:doze:..

    You know, the amount you put to guess a solution will be the same for us too..Hmmm..

    Anyways, assuming your emp table has a column called EMPID and it is a INT/BIGINT/SMALLINT column, here is how you retrive the last 5 rows

    SELECT TOP 5 * FROM EMP ORDER BY EMPID DESC

    Hope this helps you!

    Cheers!

  • When posting questions, be sure to post more and detailed information in order for us to assist you faster and better.

    Do you have an ID column? or any column with computed numbers in a specific order?

    It could even be datetime stamps?

    If you have a column like that, try:

    Select top 5 * from emp

    order by ?column? DESC

    Note: this is not the best way to do this query, but given the limited info, this is a basic "retrieve last 5 records" query.

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle

  • Homework, Test, or Interview?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • ColdCoffee (4/29/2010)


    Sri, sorry for saying this, but your question is one of the vaguest i have seen in my stint with SSC..

    There is absolutely no information about the emp table in your post, but you have thrown a frisbee to us to find something..:doze:..

    You know, the amount you put to guess a solution will be the same for us too..Hmmm..

    Anyways, assuming your emp table has a column called EMPID and it is a INT/BIGINT/SMALLINT column, here is how you retrive the last 5 rows

    SELECT TOP 5 * FROM EMP ORDER BY EMPID DESC

    Hope this helps you!

    Cheers!

    Heh... I dunno CC... looks like you did alright. 😉 In fact, you've probably just helped someone graduate. 😛

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (4/30/2010)


    ColdCoffee (4/29/2010)


    Sri, sorry for saying this, but your question is one of the vaguest i have seen in my stint with SSC..

    There is absolutely no information about the emp table in your post, but you have thrown a frisbee to us to find something..:doze:..

    You know, the amount you put to guess a solution will be the same for us too..Hmmm..

    Anyways, assuming your emp table has a column called EMPID and it is a INT/BIGINT/SMALLINT column, here is how you retrive the last 5 rows

    SELECT TOP 5 * FROM EMP ORDER BY EMPID DESC

    Hope this helps you!

    Cheers!

    Heh... I dunno CC... looks like you did alright. 😉 In fact, you've probably just helped someone graduate. 😛

    😛 thanks Jeff! No sound from the OP, guess he/she is still finalizing on the schemas :-D!

  • Hi

    You can also use RowNumber, if you know the number of rows of data see

    http://msdn.microsoft.com/en-us/library/ms186734.aspx

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply