Read last entry of table

Hi All,<br /><br />This might be a very primary question for you.<br /><br />I want to read last entry of the table. Suppose I have table as,<br /><br />ID Name<br />1 a<br />2 b<br />3 c<br />4 d<br /><br />and this table is dynamic. I want to read last name i.e. 'd'. <br /><br />I wrote a query like, <br />select Name from table where ID = (select max(ID) from table)<br /><br />But this takes considerably long time. Is there any fastest way for this? I will be using this quite frequently so cannot afford delay. What will be the faster way?<br /><br />Thanks in advance. <img src='/community/emoticons/emotion-1.gif' alt='' /><br /><br />Chioo.

Ramkumar, both your queries may return incorrect results and the second query would perform the slowest of all queries posted in the thread. The problem is that some rows might be deleted so last ID would be greater then row count.

If you're going to use this to maintain some sort of sequence, you should reconsider this strategy as this is far too slow in a heavily inserted table. Change it to an IDENTITY column and let SQL Server take care of it and generate the sequence at the client.

&gt;&gt;Interesting what happens at almost the same time at three distinct places of the world. <br /><br />Yes Interesting [<img src='/community/emoticons/emotion-1.gif' alt='' />]<br /><br />&gt;&gt;Sorry. My mistake. I meant to say MSSQL only.<br /><br />You mean you are using SQL Server?<br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail