NewID() in Order By

  • I have seen examples where NewId() is used in the order by. For example:

    Select TOP 10 *  From Authors

    ORDER BY NEWID()

    What does NewID() in the order by clause is responsible for?

    Thanks in Advance.

  • It is an acceptable randomizer used to return random rows.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • The NEWID() function gets a new GUID value from the operating system, and on Windows 2000 or later is very random ("very" meaning better than the typical 16-bit RND() function VB programmers had to live with for years).

    If your SQL Server is running on Windows NT the NEWID() function is not random.  Hopefully that is not an issue for you.

    What makes is so convenient for random ordering is that SQL knows that it is a non-deterministic function (returns a different value every time) and calls it once per row to get different unique values.  If you tried using the RAND() function instead, it is called once and the same value is used for every row.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply