title: Where is the official documentation for T-SQL's "ORDER BY RAND()" and "ORDER BY NEWID()"?
body: <p>I'm looking for the official T-SQL documentation for "ORDER BY RAND()" and "ORDER BY NEWID()".
  There are numerous articles describing them, so they must be documented somewhere.</p>
  
  <p>I'm looking for a link to an official SQL Server documentation page like this:  <a href="http://technet.microsoft.com/en-us/library/ms188385.aspx" rel="nofollow">http://technet.microsoft.com/en-us/library/ms188385.aspx</a></p>
  
  <p>CLARIFICATION:</p>
  
  <p>What I'm looking for is the documentation for "order_by_expression" that explains the difference in behavior between a nonnegative integer constant, a function that returns a nonnegative integer, and a function that returns any other value (like RAND() or NEWID()).</p>
  
  <p><br />
  ANSWER:<br />
  <br />
  I appologize for the lack of clarity in my original question.  As with most programming-related problems, the solution to the problem is primarily figuring out what question you're actually trying to answer.<br />
  <br />
  Thank you everyone.<br />
  <br />
  <br />
  The answer is in this document:  From: <a href="http://www.wiscorp.com/sql200n.zip" rel="nofollow">http://www.wiscorp.com/sql200n.zip</a><br /></p>
  
  <pre><code>Information technology — Database languages — SQL — Part 2: Foundation (SQL/Foundation)
  
  22.2 &lt;direct select statement: multiple rows&gt; includes a &lt;cursor specification&gt;.
  </code></pre>
  
  <p>At this point we have the first half of the answer:<br />
  <br />
  A SELECT statment is a type of CURSOR, which means that operations can be performed iteratively on each row.  Although I haven't found a statement in the docs that explicity says it, I'm content to assume that the <em>expression</em> in the order_by_expression will be executed for each row.<br />
  <br />
  Now it makes sense what is happening when you use RAND() or NEWID() or CEILING(RAND() + .5) / 2 as opposed to a numeric constant or a column name.<br />
  The <em>expression</em> will never be treated like a column number.  It will always be a value that is generated for each row which will be used as the basis for determining the order of the rows.<br />
  <br />
  However, for thoroughness, let's continue to the full definition of what an <em>expression</em> can be.<br /></p>
  
  <pre><code>14.3 &lt;cursor specification&gt; includes ORDER BY &lt;sort specification list&gt;.
  
  10.10 &lt;sort specification list&gt; defines:
  
  &lt;sort specification&gt; ::= &lt;sort key&gt; [ &lt;ordering specification&gt; ] [ &lt;null ordering&gt; ]
      &lt;sort key&gt; ::= &lt;value expression&gt;
      &lt;ordering specification&gt; ::= ASC | DESC
      &lt;null ordering&gt; ::= NULLS FIRST | NULLS LAST
  </code></pre>
  
  <p>Which takes us to:</p>
  
  <pre><code>6.25 &lt;value expression&gt;
  </code></pre>
  
  <p>Where we find the second half of the answer:</p>
  
  <pre><code>&lt;value expression&gt; ::= 
        &lt;common value expression&gt; 
      | &lt;boolean value expression&gt; 
      | &lt;row value expression&gt;
  
  &lt;common value expression&gt; ::= 
        &lt;numeric value expression&gt; 
      | &lt;string value expression&gt;
      | &lt;datetime value expression&gt;
      | &lt;interval value expression&gt;
      | &lt;user-defined type value expression&gt;
      | &lt;reference value expression&gt;
      | &lt;collection value expression&gt;
  
      &lt;user-defined type value expression&gt; ::= &lt;value expression primary&gt;
      &lt;reference value expression&gt; ::= &lt;value expression primary&gt;
      &lt;collection value expression&gt; ::= &lt;array value expression&gt; | &lt;multiset value expression&gt;
  </code></pre>
  
  <p>From here we descend into the numerous possibile types of expressions that can be used.<br />
  <br />
  NEWID() returns a uniqueidentifier.<br />
  It seems reasonable to assume that uniqueidentifiers are compared numerically, so if <em>expression</em> is NEWID() our &lt;common value expression&gt; will be a &lt;numeric value expression&gt;.<br />
  <br />
  Similarly, RAND() returns a numeric value, and it will also be evaluated as a &lt;numeric value expression&gt;.<br />
  <br />
  So, although I wasn't able to find anything in Microsoft's offical documentation that explains what ORDER BY does when called using an order_by_expression that is an <em>expression</em>, it really is documented, as I knew it must be.<br /></p>