SQL SERVER order by random in series
SQL SERVER order by random in series
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Aryan Kumar
31-Jul-2023Sure, here is how to order by random in series in SQL Server:
SQL
The
NEWID()function returns a unique identifier, which is a random number that is guaranteed to be unique. TheORDER BYclause orders the rows by theNEWID()function, which will sort the rows in a random order.The
NEWID()function is a database-specific function, so it may not be available in all databases. If theNEWID()function is not available, you can use theRAND()function, which returns a pseudo-random number.The following query is equivalent to the previous query, but it uses the
RAND()function instead of theNEWID()function:SQL
The
RAND()function is not guaranteed to be unique, so it is possible that the rows will be sorted in the same order every time the query is executed. If you need to ensure that the rows are sorted in a different order every time the query is executed, you can use theNEWID()function.