[thelist] Free SQL Tip

Joshua Olson joshua at waetech.com
Tue Aug 31 13:50:45 CDT 2004

<tip type="SQL" author="Joshua Olson">
Here's a great way to get fields from a table back in random order on SQL
Server.  First, add a new column, call it "rank".  Set it to data type
"uniqueidentifier" and then set its formula to "(newid())".

Then, simply ORDER BY this new columns.  Example:

SELECT id, name, eyeColor
FROM people

The results will be in random order every time!  The randomness of the
results is much better then if you try to use random numbers and seeds, as
found in other techniques.

If you don't want to add a new column, you can also ORDER BY newid(),

SELECT name, age, weight
FROM othertable
ORDER BY newid()

Joshua Olson
Web Application Engineer
WAE Tech Inc.

More information about the thelist mailing list