How to get top 10 random rows from table in SQL Server

← PrevNext →

Let us assume, I have table in SQL Server and it has few thousand rows of data. How do I retrieve the top 10 records from the table, randomly? One of the simplest methods in SQL Server to get random rows is by using the NEWID function. I’ll show you how.

See the below SQL query.

SELECT TOP 10 * FROM dbo.wordmeaning ORDER BY NEWID()

It’s a not a big table. It has just over 5000 rows.

The NEWID returns a unique id or a random value. So, the above query returns Top 10 rows order by a unique or random value.

Therefore, when you run the above query, it will return a new set of 10 records.

Depending upon the size of the table or the number of records, the query may return rows "repeatedly".

Thought you should know

The NEWID() function executes on each row and in the process generates unique GUID’s (Globally Unique Identifier) against each row in the memory. Therefore, the execution process “slows” down depending upon the number of rows in the table. It can the performance of the query against large tables. Therefore, apply this function on small tables for great results. 😀

← PreviousNext →