MS SQL Tips & Tricks

How to select X random records from a table

Let's say that the table we are going to use looks like that:

Images Table:

ID Image
1 Image1.jpg
2 Image2.jpg
3 Image3.jpg
4 Image4.jpg
5 Image5.jpg
6 Image6.jpg

We want to select 3 random images using one query. We can do that like this:

Select Top 3 ID, Image
From Images
Order by NEWID()

Remember that this method is going to scan whole table or whole index to get result, so use it on small amount of data. This method is available on MS SQL 2005.

Download SQL script


Learn more tricks

Add Comment

Comments

31st August 2021

Propecia

Gursefs

19th September 2021

generic tadalafil 40mg

JudyLoalf

19th September 2021

viagra original

AnnaLoalf

19th September 2021

buy sildenafil online usa

AmyLoalf

19th September 2021

cialis usa prescription ivermectin malaria how to order provigil online price of ivermectin liquid sildenafil fast shipping how to buy viagra online uk order zithromax over the counter generic viagra 25 cialis cheap india plaquenil tab 200mg cost

JasonLoalf

19th September 2021

cost of generic viagra in india

EvaLoalf

20th September 2021

generic priligy

Luhteerge

20th September 2021

http://buyplaquenilcv.com/ - Plaquenil

Prusina