There are multiple ways to randomly select rows from a table. In this blog post, I will show two ways of doing so:
Method 1: Random Number of Rows
DECLARE @n int
SET @n=RAND()*10
SELECT TOP (@n) * FROM sysobjects
Method 2: Random Number of Rows as well as Data
DECLARE @n int
SET @n=RAND()*10
SELECT TOP (@n) * FROM sysobjects
ORDER BY NEWID()
The second method is easy to use and fetches data more randomly.
Did you like this post?
|
|
|
||
|
|
|
|
|
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |




comments
5 Responses to "Select Random Rows from a Table – SQL Server"I am not able to follow what is difference in Method1 and 2. Can you explain in detail
Run the queries and see the difference
Its interesting that you use sysobjects here although I am not sure why. Can't I just say?
SELECT top 10 percent * from [tablenaame] order by newid()
great blog and good query. I am looking for a t-sql book to learn how to write queries. any suggestion?
Manjot: Check this post to see if it helps
SQL Server Books You Must Have In Your Collection
Post a Comment