title: Why does SQL Server 2008 order when using a GROUP BY and no order has been specified?
body: <p>I'm running into a very strange issue that I have found no explanation for yet. With SQL Server 2008 and using the GROUP BY it is ordering my columns without any ORDER BY specified. Here is a script that demonstrates the situation.</p>
  
  <pre><code>CREATE TABLE #Values ( FieldValue varchar(50) )
  
  ;WITH FieldValues AS
  (
      SELECT '4' FieldValue UNION ALL
      SELECT '3' FieldValue UNION ALL
      SELECT '2' FieldValue UNION ALL
      SELECT '1' FieldValue
  )
  INSERT INTO #Values ( FieldValue )
  SELECT
      FieldValue 
  FROM FieldValues
  
  -- First SELECT demonstrating they are ordered DESCENDING
  SELECT
      FieldValue
  FROM #Values
  
  -- Second SELECT demonstrating they are ordered ASCENDING
  SELECT
      FieldValue
  FROM #Values
  GROUP BY
      FieldValue
  
  DROP TABLE #Values
  </code></pre>
  
  <p>The first SELECT will return</p>
  
  <pre><code>4
  3
  2
  1
  </code></pre>
  
  <p>The second SELECT will return</p>
  
  <pre><code>1
  2
  3
  4
  </code></pre>
  
  <p>According to the <a href="http://msdn.microsoft.com/en-us/library/ms177673.aspx" rel="nofollow">MSDN Documentation</a> it states: "<em>The GROUP BY clause does not order the result set</em>"</p>