<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Order of Result Set of SELECT Statement on Clustered Indexed Table When ORDER BY is Not Used</title>
	<atom:link href="http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:54:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Clive Strong</title>
		<link>http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/#comment-33738</link>
		<dc:creator>Clive Strong</dc:creator>
		<pubDate>Thu, 14 Feb 2008 09:41:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/#comment-33738</guid>
		<description>I&#039;m having issues with the ordering of a clustered index at present.

Assume you have a table with one column of type INT which is the primary key.  When you insert say 10000 rows in DESC order and then do a select * from that table with no order by clause, it returns the data in order from 1 to 10000.

Do the same thing, but on on a temporary/variable table and it&#039;s out of order.

With smaller datasets, it works just fine on the temp/variable tables.

This is on SQL2000 SP4.  I tested the same code in 2005 and the ordering is correct in both instances.

Does anyone know why I would get a different sort order in this instance?</description>
		<content:encoded><![CDATA[<p>I&#8217;m having issues with the ordering of a clustered index at present.</p>
<p>Assume you have a table with one column of type INT which is the primary key.  When you insert say 10000 rows in DESC order and then do a select * from that table with no order by clause, it returns the data in order from 1 to 10000.</p>
<p>Do the same thing, but on on a temporary/variable table and it&#8217;s out of order.</p>
<p>With smaller datasets, it works just fine on the temp/variable tables.</p>
<p>This is on SQL2000 SP4.  I tested the same code in 2005 and the ordering is correct in both instances.</p>
<p>Does anyone know why I would get a different sort order in this instance?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian Yates</title>
		<link>http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/#comment-33396</link>
		<dc:creator>Ian Yates</dc:creator>
		<pubDate>Thu, 31 Jan 2008 16:42:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/#comment-33396</guid>
		<description>Following on from Luciano, it&#039;s not a bug.  The reason why a TOP clause with a different value (eg 97 PERCENT or 1000 rows), even if the value exceeds the number of rows in your table AT THAT TIME, is because if you  have a table with, for example, 2000 rows, then selecting the top 95 percent with an order by, does actually mean something.  Same goes for selecting the top 1000 records.
The order by, in both cases, is not for ordering the result of the view, but is for instructing SQL Server which top records you want.  Unfortunately (it&#039;s been widely discussed on many sites) the order by clause technically serves two purposes - ordering of the resultset as well as instructing SQL Server how you want the records ordered for things such as a TOP clause.

Take the same logic and you can see why the OVER clause in SQL 2005 has the ability to have an ORDER BY.</description>
		<content:encoded><![CDATA[<p>Following on from Luciano, it&#8217;s not a bug.  The reason why a TOP clause with a different value (eg 97 PERCENT or 1000 rows), even if the value exceeds the number of rows in your table AT THAT TIME, is because if you  have a table with, for example, 2000 rows, then selecting the top 95 percent with an order by, does actually mean something.  Same goes for selecting the top 1000 records.<br />
The order by, in both cases, is not for ordering the result of the view, but is for instructing SQL Server which top records you want.  Unfortunately (it&#8217;s been widely discussed on many sites) the order by clause technically serves two purposes &#8211; ordering of the resultset as well as instructing SQL Server how you want the records ordered for things such as a TOP clause.</p>
<p>Take the same logic and you can see why the OVER clause in SQL 2005 has the ability to have an ORDER BY.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luciano Leston</title>
		<link>http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/#comment-16609</link>
		<dc:creator>Luciano Leston</dc:creator>
		<pubDate>Sat, 27 Oct 2007 21:05:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/#comment-16609</guid>
		<description>I agree with Pinal Dave.

In the most cases, the query resultset that don&#039;t include ORDER BY clause and the table is clustered indexed returns the expected sequence that index are created (ASC or DESC), but there are some cases that doesn’t occur. I have experiment a lot of cases like that. SQL Server returns the resultset more efficiently and speedily, except when forced by a hint or other customizations (at least according query optimizer). This is the general rules.
About clustered index, they don&#039;t physically reorders the data pages, they just do this in logical level reordering the sequence of b-tree pointers in the leaf nodes.
About the ORDER BY clause in views, there are a huge discussion between MCPs and MVPs to decide if this is a BUG or not. The facts are: This &quot;problem&quot; is caused by the SQL 2005 query optimizer, which ignores the TOP (100) PERCENT clause when returning the resultset. However, any other value of the TOP clause will work. Of course sorted views are inefficient and you should do the ORDER BY clause in the query that calls the view. Microsoft said that was a misconception about the ORDER BY clause in views and subqueries. In these cases, the ORDER BY and TOP clauses are not for ordering but only for select the rows that must be returned. In the early versions of SQL, the query optimizer is less efficiently as SQL 2005. But if this is right, why a TOP clause with a different value works fine? Even you test using a value greater than the number of rows in your table the results are ordered. Microsoft publishes a temporary hotfix for this supposed &quot;bug&quot; in http://support.microsoft.com/kb/926292 but this requires the compatible level 80.</description>
		<content:encoded><![CDATA[<p>I agree with Pinal Dave.</p>
<p>In the most cases, the query resultset that don&#8217;t include ORDER BY clause and the table is clustered indexed returns the expected sequence that index are created (ASC or DESC), but there are some cases that doesn’t occur. I have experiment a lot of cases like that. SQL Server returns the resultset more efficiently and speedily, except when forced by a hint or other customizations (at least according query optimizer). This is the general rules.<br />
About clustered index, they don&#8217;t physically reorders the data pages, they just do this in logical level reordering the sequence of b-tree pointers in the leaf nodes.<br />
About the ORDER BY clause in views, there are a huge discussion between MCPs and MVPs to decide if this is a BUG or not. The facts are: This &#8220;problem&#8221; is caused by the SQL 2005 query optimizer, which ignores the TOP (100) PERCENT clause when returning the resultset. However, any other value of the TOP clause will work. Of course sorted views are inefficient and you should do the ORDER BY clause in the query that calls the view. Microsoft said that was a misconception about the ORDER BY clause in views and subqueries. In these cases, the ORDER BY and TOP clauses are not for ordering but only for select the rows that must be returned. In the early versions of SQL, the query optimizer is less efficiently as SQL 2005. But if this is right, why a TOP clause with a different value works fine? Even you test using a value greater than the number of rows in your table the results are ordered. Microsoft publishes a temporary hotfix for this supposed &#8220;bug&#8221; in <a href="http://support.microsoft.com/kb/926292" rel="nofollow">http://support.microsoft.com/kb/926292</a> but this requires the compatible level 80.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abrar Ansari</title>
		<link>http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/#comment-16093</link>
		<dc:creator>Abrar Ansari</dc:creator>
		<pubDate>Mon, 22 Oct 2007 07:27:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/#comment-16093</guid>
		<description>One Response to “SQL SERVER - Order of Result Set of SELECT Statement on Clustered Indexed Table When ORDER BY is Not Used”

Reference to the above statement i disagree with  Pinal Dave  and i think more attention is needed  ,what i say, if you don&#039;t include order by clause and your table is clustered indexed on one or more columns although you  add disorder records, SQL will  display them in sequence because we have got a clustered index on column/aolumns. Moreover, Adding a clustered index to the table  physically reorders the data pages, putting them in physical order based on the indexed column/columns.</description>
		<content:encoded><![CDATA[<p>One Response to “SQL SERVER &#8211; Order of Result Set of SELECT Statement on Clustered Indexed Table When ORDER BY is Not Used”</p>
<p>Reference to the above statement i disagree with  Pinal Dave  and i think more attention is needed  ,what i say, if you don&#8217;t include order by clause and your table is clustered indexed on one or more columns although you  add disorder records, SQL will  display them in sequence because we have got a clustered index on column/aolumns. Moreover, Adding a clustered index to the table  physically reorders the data pages, putting them in physical order based on the indexed column/columns.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Heiko Hatzfeld</title>
		<link>http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/#comment-14055</link>
		<dc:creator>Heiko Hatzfeld</dc:creator>
		<pubDate>Tue, 25 Sep 2007 11:37:20 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/09/24/sql-server-order-of-result-set-of-select-statement-on-clustered-indexed-table-when-order-by-is-not-used/#comment-14055</guid>
		<description>Well the answer would be a lot simpler...

You didnt ask for an order, so you wont get one. The data in a relational DB is by definition not ordered. You want ordered data, then ASK for it ;)


You can even go 1 step futher..

Create view foobar as
select top 100 percent * from foo order by bar


select * from foobar


The above statement will not be a sorted resultset from foo. You did not ASK for an order, so the queryplan can actually drop the order request from the view. Even if this DID work on some older Versions of SQL Server, it is not an expected result. 

You didnt ask for an order in your final querry, that means there is no need to order the result before returning it to you...</description>
		<content:encoded><![CDATA[<p>Well the answer would be a lot simpler&#8230;</p>
<p>You didnt ask for an order, so you wont get one. The data in a relational DB is by definition not ordered. You want ordered data, then ASK for it ;)</p>
<p>You can even go 1 step futher..</p>
<p>Create view foobar as<br />
select top 100 percent * from foo order by bar</p>
<p>select * from foobar</p>
<p>The above statement will not be a sorted resultset from foo. You did not ASK for an order, so the queryplan can actually drop the order request from the view. Even if this DID work on some older Versions of SQL Server, it is not an expected result. </p>
<p>You didnt ask for an order in your final querry, that means there is no need to order the result before returning it to you&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
