Friday, February 12, 2010

Selecting a random sample

If you are interested in querying for a random X number of records, or TOP Y percent of records, you can use the following syntax:


SELECT TOP 5
        *
FROM    YourTable
ORDER BY NEWID()



SELECT TOP 50 PERCENT
        *
FROM    YourTable
ORDER BY NEWID()