Quick Tip: Randomising a resultset from a SQL query

A really random resultset

Sometimes I need a random (random enough for my purposes - I don't know, nor care, if it meets a formal definition of random) sample of N rows of data from a table. There's a very quick way to do that:

SELECT	TOP 10 *
FROM	[dbo].[ATable]
ORDER
BY	NEWID()

Ordering the data by the result of calling NEWID() seems to have very little overhead but your mileage may vary on bigger tables!

About Rob

I've been interested in computing since the day my Dad purchased his first business PC (an Amstrad PC 1640 for anyone interested) which introduced me to MS-DOS batch programming and BASIC.

My skillset has matured somewhat since then, which you'll probably see from the posts here. You can read a bit more about me on the about page of the site, or check out some of the other posts on my areas of interest.

No Comments

Add a Comment