How to restrict and order in MS SQL?

I am familiar with MySQL, but not in MS SQL,

In MySQL, the query will look like this:

SELECT * from tablename ORDER BY RAND() LIMIT 5 

The above query gives 5 random numbers from a table. The same query, how can I work with MS SQL?

+6
source share
3 answers

Try

 SELECT TOP 5 * from tablename ORDER BY NEWID() 
+10
source

Look at using TOP and order NEWID

Sort of

 SELECT TOP 5 * FROM TableName ORDER BY NEWID() 
+3
source

It will be:

 SELECT TOP 5 * FROM tablename ORDER BY NEWID() 

Tested in MSSQL 2005.

+1
source

All Articles