If you can use pseudo-random sampling and you are on SQL Server 2005/2008, then take a look at TABLESAMPLE. For example, an example from SQL Server 2008 / AdventureWorks 2008 that runs on a row basis:
USE AdventureWorks2008; GO SELECT FirstName, LastName FROM Person.Person TABLESAMPLE (100 ROWS) WHERE EmailPromotion = 2;
The trick is that TABLESAMPLE is not exactly random, as it generates a given number of rows from each physical page. You cannot get exactly 5,000 rows unless TOP is also limited. If you are running SQL Server 2000, you will either have to create a temporary table that matches the primary key, or you will have to do this using the NEWID () method.
K. Brian Kelley Mar 16 '09 at 20:46 2009-03-16 20:46
source share