Greg's Blog

helping me remember what I figure out

Randomly Displaying Query Rows

| Comments

Ever wanted to have a result set to be displayed in a random fashion on each display?? This is a really short and hopefully useful tit bit. Programmatically I am sure you have all figured out how to retrieve a random record, but have you ever considered letting the database do the work? Or have an ordered result set be displayed in a random fashion on each refresh of the page?

The trick is to order the query based on the newID() function, like such:

SELECT * FROM tablename ORDER BY NewID();

As you may guess for eacxh record retrieved a new UUID is generated and the output order by that value. Simple as…