Thursday, May 29

How to select random rows from a table

How to get a random row or multiple random rows from a table ?

--#SQL to get a single random row from a table.
select top 1 column1, column2, column3 from tableName order by newid()

To get multiple random rows from a table, you need to just change the 'top' count.

--#SQL to get 10 random rows from a table.
select top 10 column1, column2, column3 from tableName order by newid()

0 comments:





Disclaimer :
Unless, otherwise mentioned, TF's SQL tips/tricks are for Sybase ASE 12.5.x