HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

What is the best way to get a random ordering?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
randomthewhatwayorderinggetbest

Problem

I have a query where I want the resulting records to be ordered randomly. It uses a clustered index, so if I do not include an order by it will likely return records in the order of that index. How can I ensure a random row order?

I understand that it will likely not be "truly" random, pseudo-random is good enough for my needs.

Solution

ORDER BY NEWID() will sort the records randomly. An example from SQLTeam.com
SELECT *
FROM Northwind..Orders
ORDER BY NEWID()

Context

StackExchange Database Administrators Q#955, answer score: 31

Revisions (0)

No revisions yet.