Tricks: How to get N random rows from a table in T-SQL

петък, 18 юли 2008 г.

Hi readers!

What is this blog post for?

Today I am going to present you the first post of my second series - Tricks. As I said before I will show "Technology Specific Tricks - short posts about something, which I have researched and which I want to preserve and share with you all."
The first one is about T-SQL


The problem - How to get N random rows from a table?



A little explanation - actually ORDER BY NEWID() sorts the rows in a random way. In our case we just take as much of the first as we need.


Conclusion

You just saw what you can expect from this type of posts.
Hope this helps!

3 коментара:

Павелъ Дончевъ каза...

Very, very interesting!

I was wondering if this is standard behaviour (that the records are sorted random)?

Because if it is not - you can't rely on it as it may change in the future. Furthermore I am really curious how do they sort those records in a random order? Are they stored random and simply returned? Or they are sorted in some order and then randomized? If you have any clue on this - please tell us as this is very interesting behaviour.

Veselin Kolev каза...

Hi Pavel,
Actually it is right from the kitchen - Microsoft has shown this technique. I do not want to explain something, which is already explained - you can find more information on this topic here .

Thanks and good luck!

Cecito;) каза...

Hi,What is this?I can1t unerstand nothing....and my english is very bad,sorry.But i speak spanish better!!;)Bye ,vesko;)

 
Vesko Kolev's Blog : IDeveloper -