logo
logo
Sign in

How does an order by newid SQL server work?

avatar
Nikhila

Newid's order We can sort random rows of query results with the aid of an SQL server. Continue reading to learn more.


As part of our Server Management Services, we at Skynats provide answers to all queries, no matter how large or small.


Let's look at how the SQL Server Support Team is prepared to assist clients with Order by Newid in SQL Server.


Using Order by NewID in SQL Server?


We can use the SQL Server's order by newid function to randomly order the records. The newid function additionally creates a random GUID that is useful for returning randomized rows from a SELECT query. 


When the NEWID() function is used in the ORDER BY clause of a SQL select query, the returned list of records is made up of random records. This results in a SQL query that returns results that are sorted randomly.


NEWID() is called for each row when Order by newid is used in a SELECT query. The rows are then sorted using a random number provided by the NEWID (). The SELECT query then returns the outcome by the remaining clauses in the query.


Consider a table with 3478 rows that contain the personal information of employees. We can use the following query to randomly choose 100 employee details from this table:


SELECT TOP 100 * 
FROM Employees 
ORDER BY NEWID()



In the aforementioned example, it sorts the rows according to the random number provided by NEWID and then returns a result that includes the top 100 rows (). In the end, NEWID creates a distinctive ID that we can include in the SELECT query's ORDER BY clause to order random rows of results.


collect
0
avatar
Nikhila
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more