Didn't quite see this variation in the answers yet. I had an additional constraint where I needed, given an initial seed, to select the same set of rows each time.
For MS SQL:
Minimum example:
select top 10 percent *from table_nameorder by rand(checksum(*))
Normalized execution time: 1.00
NewId() example:
select top 10 percent *from table_nameorder by newid()
Normalized execution time: 1.02
NewId()
is insignificantly slower than rand(checksum(*))
, so you may not want to use it against large record sets.
Selection with Initial Seed:
declare @seed intset @seed = Year(getdate()) * month(getdate()) /* any other initial seed here */select top 10 percent *from table_nameorder by rand(checksum(*) % @seed) /* any other math function here */
If you need to select the same set given a seed, this seems to work.