Select n random rows from SQL Server table


select top 10 percent * from [yourtable] order by newid()
select top 10 percent * from [yourtable] order by newid()
  1. <code>
  2. select top 10 percent * from [yourtable] order by newid()
  3. </code>
<code>
select top 10 percent * from [yourtable] order by newid()
</code>
In response to the "pure trash" comment concerning large tables: you could do it like this to improve performance.

select  * from [yourtable] where [yourPk] in 
(select top 10 percent [yourPk] from [yourtable] order by newid())
select  * from [yourtable] where [yourPk] in 
(select top 10 percent [yourPk] from [yourtable] order by newid())
  1. <code>
  2. select * from [yourtable] where [yourPk] in
  3. (select top 10 percent [yourPk] from [yourtable] order by newid())
  4. </code>
<code>
select  * from [yourtable] where [yourPk] in 
(select top 10 percent [yourPk] from [yourtable] order by newid())
</code>
The cost of this will be the key scan of values plus the join cost, which on a large table with a small percentage selection should be reasonable.

Popular posts from this blog

Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=xx.0.0.0, Culture=neutral, PublicKeyToken='xxx' or one of its dependencies.

C# Crop white space from around the image

The specified version string contains wildcards, which are not compatible with determinism.