December 08, 2010

Select Random Rows from a Table – SQL Server

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


The second method is easy to use and fetches data more randomly.

Did you like this post?
kick it on
subscribe via rss subscribe via e-mail
print this post follow me on twitter

About The Author

Madhivanan,an MSc computer Science graduate from Chennai-India, works as a works as a Lead Subject Matter Expert at a company that simplifies BIG data. He started his career as a developer working with Visual Basic 6.0, SQL Server 2000 and Crystal Report 8. As years went by, he started working more on writing queries in SQL Server. He now has good level of knowledge in SQLServer, Oracle, MySQL and PostgreSQL as well. He is also one of the leading posters at and a moderator at His T-sql blog is at



5 Responses to "Select Random Rows from a Table – SQL Server"
  1. Mihir said...
    December 8, 2010 at 10:45 PM

    I am not able to follow what is difference in Method1 and 2. Can you explain in detail

  2. Madhivanan said...
    December 9, 2010 at 1:50 AM

    Run the queries and see the difference

  3. Jeremy said...
    December 14, 2010 at 1:21 AM

    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()

  4. Manjot said...
    December 20, 2010 at 11:16 PM

    great blog and good query. I am looking for a t-sql book to learn how to write queries. any suggestion?

  5. Suprotim Agarwal said...
    December 21, 2010 at 10:58 PM

    Manjot: Check this post to see if it helps

    SQL Server Books You Must Have In Your Collection


Copyright © 2009-2016 All Rights Reserved for by Suprotim Agarwal | Terms and Conditions