I have a table with 1.3 million rows.
How do I just select a random 100 rows?
My table name is [March Value Mailer Offer Audience] and the attribute in the table is individual_id
I just want it to randomly pick 100 rows for a test that I am running
I have a table with 1.3 million rows.
How do I just select a random 100 rows?
My table name is [March Value Mailer Offer Audience] and the attribute in the table is individual_id
I just want it to randomly pick 100 rows for a test that I am running
The NEWID technique that people suggested here may be a performance challenge, as each row of your 1.3M rows will be assigned a NEWID, all will be sorted, and only then your top 100 will be returned.
SQL Server offers a much more efficient way to sample rows, using the table hint TABLESAMPLE. For example,
select * from [March Value Mailer Offer Audience] TABLESAMPLE(100 ROWS);
See more information here
HTH
An expensive method is:
select top (100) t.*
from t
order by newid();
If you want arbitrary rows, you can just do:
select top (100) t.*
from t;
Or, a convenient solution that is both random and should be quicker:
select top (100) t.*
from t
where rand(checksum(newid())) < 0.001 -- about 1 in 1000 rows
order by newid();
This pares down the rows to a random subset of about 1300 (0.1%) rows and then randomly chooses among them.
Use newid() :
select top (100) a.*
from [March Value Mailer Offer Audience] a
order by newid();
However, only top (n) would do what you want without any ordering :
select top (100) a.*
from [March Value Mailer Offer Audience] a;