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

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • 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.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • 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