How to randomly select 10% of the rows from a table

  • I need to extract randomly to select 10% of the rows from a table. Kindly advise how to write the t-SQL.

     

    Thanks.

  • Use NEWID() to force randomness.

    SELECT TOP (10) PERCENT
    *
    FROM sys.columns
    ORDER BY NEWID();

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • thanks, Phil. Can I use random () to rewrite the same query?

    Thanks.

  • SQL_Hunt wrote:

    thanks, Phil. Can I use random () to rewrite the same query?

    Please post the link to the T-SQL random() function that you intend to use.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil's solution is definitely the more elegant but (for completeness) there is also a TABLESAMPLE clause.

Viewing 5 posts - 1 through 4 (of 4 total)

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