Search code examples
sql-serversql-server-2012

Delete 75% Of a Table Randomly


Trying to make a smaller sample database but still have the data be somewhat statistically relevant. How can I delete x % of rows from the table ? Been fooling around with the NEWID() function.


Solution

  • DELETE 
    FROM TABLE_NAME
    WHERE PK IN (SELECT TOP (75) PERCENT PK
                 FROM TABLE_NAME
                 ORDER BY NEWID())
    

    Suggestion by Martin Smith

    DELETE T 
    FROM (SELECT TOP (75) PERCENT * 
          FROM TABLE_NAME 
          ORDER BY NEWID()) T