Sure, here is how to order by random in series in SQLServer:
SQL
USE MyDatabase;
SELECT CustomerID, FirstName, LastName
FROM Customers
ORDER BY NEWID();
The NEWID() function returns a unique identifier, which is a random number that is guaranteed to be unique. The
ORDER BY clause orders the rows by the NEWID() 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 the
NEWID() function is not available, you can use the RAND() function, which returns a pseudo-random number.
The following query is equivalent to the previous query, but it uses the
RAND() function instead of the NEWID() function:
SQL
USE MyDatabase;
SELECT CustomerID, FirstName, LastName
FROM Customers
ORDER BY RAND();
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 the NEWID() function.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, 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.