Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return rows in random order [duplicate]

Is it possible to write SQL query that returns table rows in random order every time the query run?

like image 557
Arsen Mkrtchyan Avatar asked Jul 13 '09 04:07

Arsen Mkrtchyan


People also ask

How do you randomize rows in SQL?

MySQL select random records using ORDER BY RAND() The function RAND() generates a random value for each row in the table. The ORDER BY clause sorts all rows in the table by the random number generated by the RAND() function. The LIMIT clause picks the first row in the result set sorted randomly.

Is Newid random?

SQL Server NewId() generates a random GUID or unique identifier which can be used to return randomized rows from a SELECT query. T-SQL developers will realize that the return list of a SQL SELECT query is sorted randomly when they place "NEWID() function in the "ORDER BY" clause of the SELECT statement.

What is Tablesample?

Introduced in SQL Server 2005, TABLESAMPLE allows you to extract a sampling of rows from a table in the FROM clause. The rows retrieved are random and they are not in any order. This sampling can be based on a percentage of number of rows.

What does Rowcount return?

SQL Server @@ROWCOUNT is a system variable that is used to return the number of rows that are affected by the last executed statement in the batch.


1 Answers

SELECT * FROM table ORDER BY NEWID() 
like image 125
Dave Barker Avatar answered Oct 07 '22 19:10

Dave Barker