SQL to Select a random row from a database table

If we want to select random data (for any specific number TOP 3… or for all data set), we can order the data by using NEWID().


declare @Product table
     ProductID int,
     Color varchar(5)
insert into @Product
     select 1, 'Black' union all
     select 2, 'Red' union all
     select 3, 'Blue' union all
     select 4, 'Black' union all
     select 5, 'Black' union all
     select 6, 'Red' union all
     select 7, 'Grey' union all
     select 8, 'Red' union all
     select 9, 'Blue'

SELECT ProductID, Color 
FROM @Product
By simplemsexchange Posted in SQL 2008

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s