Is there a succinct way to retrieve a random record from a sql server table?
I would like to randomize my unit test data, so am looking for a simple way to select a random id from a table. In English, the select would be ‘Select one id from the table where the id is a random number between the lowest id in the table and the highest id in the table.’
I can’t figure out a way to do it without have to run the query, test for a null value, then re-run if null.
Ideas?
Yes
Explanation
A
NEWID()is generated for each row and the table is then sorted by it. The first record is returned (i.e. the record with the ‘lowest’ GUID).Notes
GUIDs are generated as pseudo-random numbers since version four:
—A Universally Unique IDentifier (UUID) URN Namespace – RFC 4122
The alternative
SELECT TOP 1 * FROM table ORDER BY RAND()will not work as one would think.RAND()returns one single value per query, thus all rows will share the same value.While GUID values are pseudo-random, you will need a better PRNG for the more demanding applications.
Typical performance is less than 10 seconds for around 1,000,000 rows — of course depending on the system. Note that it’s impossible to hit an index, thus performance will be relatively limited.