SQL Tips by Namwar Rizvi

May 9, 2007

More simpler way to return a random row from table

Filed under: Random row,tips — namwar @ 11:21 PM

Few days back, I posted a query to return a random row by using RowNum function. A reader has pointed out a more simpler way to achieve the same result. Following is the new and quicker way to return the random row:

select top 1 ColumnList
from TableName
order by newid()

Just replace “ColumnList” and “TableName” with list of columns and name of table respectively.

I will really appreciate, if someone can give comments about the performance of both approaches.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: