Select a random row from a table in SQL Server using NewId()

To select a random row from a table using NewID() UniqueIdentifier use this code:

SELECT TOP 1 NewId()
AS random_column, *
FROM Table_name ORDER BY random_column

NewId() creates unique value of type uniqueidentifier.

Here is the same result without displayed NewId() column:

SELECT TOP 1 *
FROM Table_name ORDER BY NewId()

To select a random row from a Person.Person table:

SELECT TOP 1 *
FROM Person.Person ORDER BY NewId()

random_person_person_row

To select 100 random rows from a table using NewID() UniqueIdentifier use this code:

SELECT TOP 100 *
FROM Table_name ORDER BY NewId()

If the post helped you, please share it:
Pin It

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">