<?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; Logical Query Processing Phases &#8211; Order of Statement Execution</title>
	<atom:link href="http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Fri, 17 May 2013 15:26:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Neeraj</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-474889</link>
		<dc:creator><![CDATA[Neeraj]]></dc:creator>
		<pubDate>Tue, 14 May 2013 05:45:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-474889</guid>
		<description><![CDATA[How to use on ,out, CUBE and  ROLLUP?
please give me one example]]></description>
		<content:encoded><![CDATA[<p>How to use on ,out, CUBE and  ROLLUP?<br />
please give me one example</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; UNION ALL and ORDER BY &#8211; How to Order Table Separately While Using UNION ALL &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-366346</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; UNION ALL and ORDER BY &#8211; How to Order Table Separately While Using UNION ALL &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Tue, 30 Oct 2012 01:31:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-366346</guid>
		<description><![CDATA[[...] It is not possible to use two different ORDER BY in the UNION statement. UNION returns single resultsetand as per the Logical Query Processing Phases. [...]]]></description>
		<content:encoded><![CDATA[<p>[...] It is not possible to use two different ORDER BY in the UNION statement. UNION returns single resultsetand as per the Logical Query Processing Phases. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emilian</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-322483</link>
		<dc:creator><![CDATA[Emilian]]></dc:creator>
		<pubDate>Tue, 31 Jul 2012 15:28:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-322483</guid>
		<description><![CDATA[declare @x table (i int)
insert into @x(i) values(1),(5),(10),(2)
select top(2) i as Z from @x
SELECT  * FROM    (select top(2) i as Z from @x) y order by y.Z

Why it applies order by before the top?:)]]></description>
		<content:encoded><![CDATA[<p>declare @x table (i int)<br />
insert into @x(i) values(1),(5),(10),(2)<br />
select top(2) i as Z from @x<br />
SELECT  * FROM    (select top(2) i as Z from @x) y order by y.Z</p>
<p>Why it applies order by before the top?:)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lakhan pariyani</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-319773</link>
		<dc:creator><![CDATA[lakhan pariyani]]></dc:creator>
		<pubDate>Fri, 27 Jul 2012 09:38:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-319773</guid>
		<description><![CDATA[thanks for this best post... can you please tell me which comes first in this two things
 
the increment(Identity) is incremented first 
or the constraints are checked first
As I know the answer that increments comes first but I need a reason for it..
because if the increment is done first and then constraints are checked if constraints are not matched then still the identity is increased which is wrong according to me..
but why these happens please reply as soon as possible]]></description>
		<content:encoded><![CDATA[<p>thanks for this best post&#8230; can you please tell me which comes first in this two things</p>
<p>the increment(Identity) is incremented first<br />
or the constraints are checked first<br />
As I know the answer that increments comes first but I need a reason for it..<br />
because if the increment is done first and then constraints are checked if constraints are not matched then still the identity is increased which is wrong according to me..<br />
but why these happens please reply as soon as possible</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; An Important Part of Most SELECT statement &#8211; WHERE clause &#8211; Quiz &#8211; Puzzle &#8211; 4 of 31 &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-231552</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; An Important Part of Most SELECT statement &#8211; WHERE clause &#8211; Quiz &#8211; Puzzle &#8211; 4 of 31 &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Thu, 05 Jan 2012 01:32:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-231552</guid>
		<description><![CDATA[[...] SQL Server Interview Questions and Answers ISBN: 1466405643 Page#38-40 Does Order of Column in WHERE clause Matter? Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup Logical Query Processing Phases – Order of Statement Execution [...]]]></description>
		<content:encoded><![CDATA[<p>[...] SQL Server Interview Questions and Answers ISBN: 1466405643 Page#38-40 Does Order of Column in WHERE clause Matter? Query Optimization – Remove Bookmark Lookup – Remove RID Lookup – Remove Key Lookup Logical Query Processing Phases – Order of Statement Execution [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zafar Iqbal</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-121186</link>
		<dc:creator><![CDATA[Zafar Iqbal]]></dc:creator>
		<pubDate>Mon, 28 Feb 2011 12:44:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-121186</guid>
		<description><![CDATA[Hi Pinal , In which sequence Where clause is executed ?]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal , In which sequence Where clause is executed ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pinaldave</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-57048</link>
		<dc:creator><![CDATA[pinaldave]]></dc:creator>
		<pubDate>Mon, 26 Oct 2009 20:07:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-57048</guid>
		<description><![CDATA[I just attended Itzik&#039;s session on this subject. 

One of the best! 

He also discussed that when TOP is there ORDER BY is executed along with it. If it does not exist, ORDER BY is executed separate. 

More detail I will post soon.

Great session.

Kind Regards,
Pinal]]></description>
		<content:encoded><![CDATA[<p>I just attended Itzik&#8217;s session on this subject. </p>
<p>One of the best! </p>
<p>He also discussed that when TOP is there ORDER BY is executed along with it. If it does not exist, ORDER BY is executed separate. </p>
<p>More detail I will post soon.</p>
<p>Great session.</p>
<p>Kind Regards,<br />
Pinal</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER – Download Logical Query Processing Poster Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-56572</link>
		<dc:creator><![CDATA[SQL SERVER – Download Logical Query Processing Poster Journey to SQL Authority with Pinal Dave]]></dc:creator>
		<pubDate>Sat, 10 Oct 2009 06:33:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-56572</guid>
		<description><![CDATA[[...] Brian Tkatch [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Brian Tkatch [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-56275</link>
		<dc:creator><![CDATA[Brian Tkatch]]></dc:creator>
		<pubDate>Wed, 30 Sep 2009 13:39:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-56275</guid>
		<description><![CDATA[@Bob

&quot;just having a clustered key present doesn’t mean that the TOP results will use that key.&quot;

That&#039;s something the help file states.

But if the CLUSTERED INDEX is used, that is &quot;probably&quot; the order. According to Denny Cherry &quot;If you don’t have an ORDER BY, then the SQL Server will probably sort by the clustered index and use it for filtering. If there is no clustered index it would then fall back to using the table itself and return the data in the order it comes across it.&quot; From: http://itknowledgeexchange.techtarget.com/sql-server/back-to-basics-whats-the-difference-between-a-scan-and-a-seek/

So, technically it doesn&#039;t have to use it. And sure, you probably shouldn&#039;t rely on it. But the fact remains, it is used. And blaming bugs in the code on &quot;techncially&quot;s is silly. Unless it really is the case, but my gut feeling is otherwise.]]></description>
		<content:encoded><![CDATA[<p>@Bob</p>
<p>&#8220;just having a clustered key present doesn’t mean that the TOP results will use that key.&#8221;</p>
<p>That&#8217;s something the help file states.</p>
<p>But if the CLUSTERED INDEX is used, that is &#8220;probably&#8221; the order. According to Denny Cherry &#8220;If you don’t have an ORDER BY, then the SQL Server will probably sort by the clustered index and use it for filtering. If there is no clustered index it would then fall back to using the table itself and return the data in the order it comes across it.&#8221; From: <a href="http://itknowledgeexchange.techtarget.com/sql-server/back-to-basics-whats-the-difference-between-a-scan-and-a-seek/" rel="nofollow">http://itknowledgeexchange.techtarget.com/sql-server/back-to-basics-whats-the-difference-between-a-scan-and-a-seek/</a></p>
<p>So, technically it doesn&#8217;t have to use it. And sure, you probably shouldn&#8217;t rely on it. But the fact remains, it is used. And blaming bugs in the code on &#8220;techncially&#8221;s is silly. Unless it really is the case, but my gut feeling is otherwise.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-56178</link>
		<dc:creator><![CDATA[Bob]]></dc:creator>
		<pubDate>Fri, 25 Sep 2009 21:42:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-56178</guid>
		<description><![CDATA[Brian,

I just reread the challenge.  To have the query use the clustered index and not return cluster key ordered rows?

No, I don&#039;t think I&#039;ll attempt that.

But the query above shows that just having a clustered key present doesn&#039;t mean that the TOP results will use that key.  In the case above the Non-clustered index is used even w/o an ORDER BY.]]></description>
		<content:encoded><![CDATA[<p>Brian,</p>
<p>I just reread the challenge.  To have the query use the clustered index and not return cluster key ordered rows?</p>
<p>No, I don&#8217;t think I&#8217;ll attempt that.</p>
<p>But the query above shows that just having a clustered key present doesn&#8217;t mean that the TOP results will use that key.  In the case above the Non-clustered index is used even w/o an ORDER BY.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-56177</link>
		<dc:creator><![CDATA[Bob]]></dc:creator>
		<pubDate>Fri, 25 Sep 2009 21:37:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-56177</guid>
		<description><![CDATA[Brian,

&gt;&gt; Can you give an example ...uses the CLUSTERED INDEX but the ordering is otherwise?

Something like this:
CREATE TABLE A (Col varchar(900) NOT NULL, Data varchar(4) NULL
	, CONSTRAINT [PK_A] PRIMARY KEY CLUSTERED ([Col] ASC)
)
GO
INSERT INTO A (Col, Data) VALUES (&#039;2kakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnl1akfvbsaib83y34nlnmhsojhg&#039;, &#039;B&#039;)
INSERT INTO A (Col, Data) VALUES (&#039;1kakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnmhsojhg&#039;, &#039;A&#039;)
GO
CREATE NONCLUSTERED INDEX [NCIX_Data_DESC] ON [dbo].[A] ([Data] DESC)
GO
SELECT TOP 2 * FROM A

The point is that when the BOL says that TOP returns arbitrary rows in the absence of ORDER BY, they are describing the expected behavior and not trying to give us insight into the implementation.  The implementation, confronted with a clustered index without an ORDER BY, may typically return an ordered set; but we are not supposed to perceive it as an ordered set because there may be at least one codepath through the optimizer that doesn&#039;t order as we would expect.  

pinaldave seems to have a knack for finding those code paths :)

I can also give an example where step #2 ON is evaluated after step #8 SELECT or STEP #4 WHERE.

Don&#039;t rely on implementation whenever and wherever you can get away with it.]]></description>
		<content:encoded><![CDATA[<p>Brian,</p>
<p>&gt;&gt; Can you give an example &#8230;uses the CLUSTERED INDEX but the ordering is otherwise?</p>
<p>Something like this:<br />
CREATE TABLE A (Col varchar(900) NOT NULL, Data varchar(4) NULL<br />
	, CONSTRAINT [PK_A] PRIMARY KEY CLUSTERED ([Col] ASC)<br />
)<br />
GO<br />
INSERT INTO A (Col, Data) VALUES (&#8217;2kakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnl1akfvbsaib83y34nlnmhsojhg&#8217;, &#8216;B&#8217;)<br />
INSERT INTO A (Col, Data) VALUES (&#8217;1kakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnlkakfvbsaib83y34nlnmhsojhg&#8217;, &#8216;A&#8217;)<br />
GO<br />
CREATE NONCLUSTERED INDEX [NCIX_Data_DESC] ON [dbo].[A] ([Data] DESC)<br />
GO<br />
SELECT TOP 2 * FROM A</p>
<p>The point is that when the BOL says that TOP returns arbitrary rows in the absence of ORDER BY, they are describing the expected behavior and not trying to give us insight into the implementation.  The implementation, confronted with a clustered index without an ORDER BY, may typically return an ordered set; but we are not supposed to perceive it as an ordered set because there may be at least one codepath through the optimizer that doesn&#8217;t order as we would expect.  </p>
<p>pinaldave seems to have a knack for finding those code paths :)</p>
<p>I can also give an example where step #2 ON is evaluated after step #8 SELECT or STEP #4 WHERE.</p>
<p>Don&#8217;t rely on implementation whenever and wherever you can get away with it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-56131</link>
		<dc:creator><![CDATA[Brian Tkatch]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 13:37:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-56131</guid>
		<description><![CDATA[@Bob

 Can you give an example where there is a CLUSTERED INDEX, no ORDER BY, and the query uses the CLUSTERED INDEX but the ordering is otherwise?]]></description>
		<content:encoded><![CDATA[<p>@Bob</p>
<p> Can you give an example where there is a CLUSTERED INDEX, no ORDER BY, and the query uses the CLUSTERED INDEX but the ordering is otherwise?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pinaldave</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-56118</link>
		<dc:creator><![CDATA[pinaldave]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 08:38:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-56118</guid>
		<description><![CDATA[Excellent note Bob. I appreciate it.

Kind Regards,
Pinal]]></description>
		<content:encoded><![CDATA[<p>Excellent note Bob. I appreciate it.</p>
<p>Kind Regards,<br />
Pinal</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-56101</link>
		<dc:creator><![CDATA[Bob]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 00:31:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-56101</guid>
		<description><![CDATA[The logical processing order that you repeat above from the March blog comments isn&#039;t correct in all cases, despite its being repeated in books, classes, and blogs.  It is useful for explaining to those new to SQL how one might envision the processing being accomplished, however, it is neither specified by any ANSI SQL standard, nor is it guaranteed by Microsoft, nor is it accurate with respect to Microsoft&#039;s current or prior implementations.  No ANSI SQL standard requires a specific processing order, but instead leaves that to the implementation.  The implementation&#039;s are therefore free to use that flexibility to optimize certain scenarios as they see fit and as long as the syntax and results conform to the standard.  Even the ORDER BY isn&#039;t as cut and dry as one might suspect, since the implementation can use the information it knows to be true about the data to best effect.  As a result, it would be unwise to declare statements in such a way as to depend on a specific processing order.

In the case of ORDER BY and TOP, Brian Tkatch gave you the only answer that you can hang your hat on; that is, that in the absence of an ORDER BY clause, TOP will return arbitrary rows.  Therefore, you can consider that ORDER BY precedes TOP when ORDER BY is specified, however, that may not be what actually happens procedurally, just what apparently happens when the results come back.  I can conceive of times where an optimizer could know enough about the data to select the TOP rows before even making a JOIN.  Like gravity, Microsoft&#039;s implementation is a black box to us: we all can make use of gravity everyday without having to know how it works.  In this case, we are better off not knowing how it works, because we don&#039;t want to be implementation-specific when we write our SQL.  I can produce sample code that shows these differences if you&#039;re interested.

P.S. It would be easy for someone to read Brian Tkatch&#039;s comment and think that an ORDER BY is not required in the case where you pull results from a single table with a clustered index and the cluster key order is the order you desire.  The fact is that result ordering is never guaranteed in the absence of an ORDER BY clause.   On a related note, rows are not physically order by cluster key.  The clustered index is a structure that is built from the data pages.]]></description>
		<content:encoded><![CDATA[<p>The logical processing order that you repeat above from the March blog comments isn&#8217;t correct in all cases, despite its being repeated in books, classes, and blogs.  It is useful for explaining to those new to SQL how one might envision the processing being accomplished, however, it is neither specified by any ANSI SQL standard, nor is it guaranteed by Microsoft, nor is it accurate with respect to Microsoft&#8217;s current or prior implementations.  No ANSI SQL standard requires a specific processing order, but instead leaves that to the implementation.  The implementation&#8217;s are therefore free to use that flexibility to optimize certain scenarios as they see fit and as long as the syntax and results conform to the standard.  Even the ORDER BY isn&#8217;t as cut and dry as one might suspect, since the implementation can use the information it knows to be true about the data to best effect.  As a result, it would be unwise to declare statements in such a way as to depend on a specific processing order.</p>
<p>In the case of ORDER BY and TOP, Brian Tkatch gave you the only answer that you can hang your hat on; that is, that in the absence of an ORDER BY clause, TOP will return arbitrary rows.  Therefore, you can consider that ORDER BY precedes TOP when ORDER BY is specified, however, that may not be what actually happens procedurally, just what apparently happens when the results come back.  I can conceive of times where an optimizer could know enough about the data to select the TOP rows before even making a JOIN.  Like gravity, Microsoft&#8217;s implementation is a black box to us: we all can make use of gravity everyday without having to know how it works.  In this case, we are better off not knowing how it works, because we don&#8217;t want to be implementation-specific when we write our SQL.  I can produce sample code that shows these differences if you&#8217;re interested.</p>
<p>P.S. It would be easy for someone to read Brian Tkatch&#8217;s comment and think that an ORDER BY is not required in the case where you pull results from a single table with a clustered index and the cluster key order is the order you desire.  The fact is that result ordering is never guaranteed in the absence of an ORDER BY clause.   On a related note, rows are not physically order by cluster key.  The clustered index is a structure that is built from the data pages.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yousuf Khan</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-54487</link>
		<dc:creator><![CDATA[Yousuf Khan]]></dc:creator>
		<pubDate>Wed, 05 Aug 2009 18:36:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-54487</guid>
		<description><![CDATA[Left Join is Quite Simple ! 

but turns out to be very dangerous
if used incorrectly that is the query may take hours to execute and the final result contains lakhs of unwanted rows.

Simple way to implement join
on tables all having one field in common
say ID,i.e. ID field links all tables to each other

Sample simple query could be used for n number of tables and n number of joins

select

a.ID,
a.field2,
a.field3,
b.field1,
b.field2,
c.field1,
c.field2,
d.field1,
d.field2,
d.field3,
d.field4


   from

(

(select * from table1) as a

left join

(select * from table2) as b

on a.ID=b.ID

left join

(select * from table3) as c

on a.ID=c.ID

left join 

(select * from table4) as d

on a.ID=d.ID

) 


I suggest never use left join !

If have to , create index on field like ID in above sample 
on all tables to reduce query time]]></description>
		<content:encoded><![CDATA[<p>Left Join is Quite Simple ! </p>
<p>but turns out to be very dangerous<br />
if used incorrectly that is the query may take hours to execute and the final result contains lakhs of unwanted rows.</p>
<p>Simple way to implement join<br />
on tables all having one field in common<br />
say ID,i.e. ID field links all tables to each other</p>
<p>Sample simple query could be used for n number of tables and n number of joins</p>
<p>select</p>
<p>a.ID,<br />
a.field2,<br />
a.field3,<br />
b.field1,<br />
b.field2,<br />
c.field1,<br />
c.field2,<br />
d.field1,<br />
d.field2,<br />
d.field3,<br />
d.field4</p>
<p>   from</p>
<p>(</p>
<p>(select * from table1) as a</p>
<p>left join</p>
<p>(select * from table2) as b</p>
<p>on a.ID=b.ID</p>
<p>left join</p>
<p>(select * from table3) as c</p>
<p>on a.ID=c.ID</p>
<p>left join </p>
<p>(select * from table4) as d</p>
<p>on a.ID=d.ID</p>
<p>) </p>
<p>I suggest never use left join !</p>
<p>If have to , create index on field like ID in above sample<br />
on all tables to reduce query time</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQLAuthority News - Gandhinagar SQL Server User Group Meeting April 24, 2009 Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-51445</link>
		<dc:creator><![CDATA[SQLAuthority News - Gandhinagar SQL Server User Group Meeting April 24, 2009 Journey to SQL Authority with Pinal Dave]]></dc:creator>
		<pubDate>Fri, 01 May 2009 01:32:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-51445</guid>
		<description><![CDATA[[...] We had a guest speaker this time - Jacob Sebastian, who is a Book Author and a SQL Server MVP. The meeting commenced with his discussion on Query Writing Style and Local Query Processing Phases where he enthusiastically shared his knowledge with us. Jacob clearly explicated on how query processes its statement. Previously, I had written about on the same subject SQL SERVER - Logical Query Processing Phases - Order of Statement Execution. [...]]]></description>
		<content:encoded><![CDATA[<p>[...] We had a guest speaker this time &#8211; Jacob Sebastian, who is a Book Author and a SQL Server MVP. The meeting commenced with his discussion on Query Writing Style and Local Query Processing Phases where he enthusiastically shared his knowledge with us. Jacob clearly explicated on how query processes its statement. Previously, I had written about on the same subject SQL SERVER &#8211; Logical Query Processing Phases &#8211; Order of Statement Execution. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jones</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-51337</link>
		<dc:creator><![CDATA[Jones]]></dc:creator>
		<pubDate>Mon, 27 Apr 2009 22:23:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-51337</guid>
		<description><![CDATA[Nice post...]]></description>
		<content:encoded><![CDATA[<p>Nice post&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Walsh</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-51317</link>
		<dc:creator><![CDATA[Mike Walsh]]></dc:creator>
		<pubDate>Mon, 27 Apr 2009 11:28:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-51317</guid>
		<description><![CDATA[So looking at what it does, how it works and looking at Itzik&#039;s poster in the link above and his other books that logical processing is described in. I would say the answer is &quot;Yes&quot;

It would appear as though to do the top it has to know the Order By clause and it needs to understand there is an Order By, and then filter only the top number/percent of rows according to that.

The actual ordering for display or passing on to the next query happens after the TOP filter is applied (even though it looked at the order by).

One important thing to remember is that we are talking about the logical query processing here. The physical order of events is not necessarily (not, actually) the same always.]]></description>
		<content:encoded><![CDATA[<p>So looking at what it does, how it works and looking at Itzik&#8217;s poster in the link above and his other books that logical processing is described in. I would say the answer is &#8220;Yes&#8221;</p>
<p>It would appear as though to do the top it has to know the Order By clause and it needs to understand there is an Order By, and then filter only the top number/percent of rows according to that.</p>
<p>The actual ordering for display or passing on to the next query happens after the TOP filter is applied (even though it looked at the order by).</p>
<p>One important thing to remember is that we are talking about the logical query processing here. The physical order of events is not necessarily (not, actually) the same always.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pinaldave</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-51311</link>
		<dc:creator><![CDATA[pinaldave]]></dc:creator>
		<pubDate>Mon, 27 Apr 2009 07:30:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-51311</guid>
		<description><![CDATA[Thank you guys for this wonderful contribution. I am still not clear what is first ORDER BY or TOP.

Any more ideas?

Regards,
Pinal]]></description>
		<content:encoded><![CDATA[<p>Thank you guys for this wonderful contribution. I am still not clear what is first ORDER BY or TOP.</p>
<p>Any more ideas?</p>
<p>Regards,<br />
Pinal</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vijay ahire</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-51277</link>
		<dc:creator><![CDATA[vijay ahire]]></dc:creator>
		<pubDate>Sat, 25 Apr 2009 07:19:04 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-51277</guid>
		<description><![CDATA[&quot;the breakpoint will not currently be hit.unable to bind sql breakpoint at this time&quot;.
Can anybody help me to solve this error while try to debug
SQL CLR procedure in VS2005.]]></description>
		<content:encoded><![CDATA[<p>&#8220;the breakpoint will not currently be hit.unable to bind sql breakpoint at this time&#8221;.<br />
Can anybody help me to solve this error while try to debug<br />
SQL CLR procedure in VS2005.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MarlonRibunal</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-50655</link>
		<dc:creator><![CDATA[MarlonRibunal]]></dc:creator>
		<pubDate>Wed, 08 Apr 2009 00:59:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-50655</guid>
		<description><![CDATA[Here, take a look at Itzik Ben-Gan&#039;s flow chart of Logical Query Processing (PDF download):

http://www.sql.co.il/books/insidetsql2008/Logical%20Query%20Processing%20Poster.pdf]]></description>
		<content:encoded><![CDATA[<p>Here, take a look at Itzik Ben-Gan&#8217;s flow chart of Logical Query Processing (PDF download):</p>
<p><a href="http://www.sql.co.il/books/insidetsql2008/Logical%20Query%20Processing%20Poster.pdf" rel="nofollow">http://www.sql.co.il/books/insidetsql2008/Logical%20Query%20Processing%20Poster.pdf</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Walsh</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-50592</link>
		<dc:creator><![CDATA[Mike Walsh]]></dc:creator>
		<pubDate>Tue, 07 Apr 2009 01:56:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-50592</guid>
		<description><![CDATA[Hi Pinal -

Those examples definitely help show that order by happens first. The query plan will definitely show it as well.

As for a simple way to think about it, just think about what happens with each operator.

The ORDER BY is ordering your results, it is forcing them to come back in a certain way (contrary to relational theory :) ). The TOP operator is telling SQL Server to restrict the rows returned to a certain number of rows (either explicitly or by percentage). If TOP happened first that would mean you would just be ordering by the number of rows specified in the top. If you were to query a table and ask for the TOP 1 ORDERED BY a certain value that would be silly for a couple reasons:

1.) Silly to order by one row
2.) Let&#039;s say you were querying a column that was not the clustered key, the TOP isn&#039;t going to guarantee you which row you get (as described above) so not doing the ORDER BY first means you are not going to be getting meaningful results.

Good post and always helpful to remind folks the order of logical query processing (physical processing doesn&#039;t always follow that order but it helps people think of ways to better limit their results earlier and improve performance.

Itzik&#039;s book INSIDE SQL Server 2005 T-SQL Querying describes each of these steps in detail (though I believe that is actually Lubar&#039;s chapter if memory serves correct)]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal -</p>
<p>Those examples definitely help show that order by happens first. The query plan will definitely show it as well.</p>
<p>As for a simple way to think about it, just think about what happens with each operator.</p>
<p>The ORDER BY is ordering your results, it is forcing them to come back in a certain way (contrary to relational theory :) ). The TOP operator is telling SQL Server to restrict the rows returned to a certain number of rows (either explicitly or by percentage). If TOP happened first that would mean you would just be ordering by the number of rows specified in the top. If you were to query a table and ask for the TOP 1 ORDERED BY a certain value that would be silly for a couple reasons:</p>
<p>1.) Silly to order by one row<br />
2.) Let&#8217;s say you were querying a column that was not the clustered key, the TOP isn&#8217;t going to guarantee you which row you get (as described above) so not doing the ORDER BY first means you are not going to be getting meaningful results.</p>
<p>Good post and always helpful to remind folks the order of logical query processing (physical processing doesn&#8217;t always follow that order but it helps people think of ways to better limit their results earlier and improve performance.</p>
<p>Itzik&#8217;s book INSIDE SQL Server 2005 T-SQL Querying describes each of these steps in detail (though I believe that is actually Lubar&#8217;s chapter if memory serves correct)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fan of SQLAuthority</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-50566</link>
		<dc:creator><![CDATA[fan of SQLAuthority]]></dc:creator>
		<pubDate>Mon, 06 Apr 2009 15:07:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-50566</guid>
		<description><![CDATA[Brian,

You are answering like Imran, who used to answer really nice.]]></description>
		<content:encoded><![CDATA[<p>Brian,</p>
<p>You are answering like Imran, who used to answer really nice.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pinaldave</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-50564</link>
		<dc:creator><![CDATA[pinaldave]]></dc:creator>
		<pubDate>Mon, 06 Apr 2009 15:00:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-50564</guid>
		<description><![CDATA[Hi Brian Tkatch,

Very nice!

Regards,
Pinal]]></description>
		<content:encoded><![CDATA[<p>Hi Brian Tkatch,</p>
<p>Very nice!</p>
<p>Regards,<br />
Pinal</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comment-50559</link>
		<dc:creator><![CDATA[Brian Tkatch]]></dc:creator>
		<pubDate>Mon, 06 Apr 2009 13:43:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218#comment-50559</guid>
		<description><![CDATA[The help file for TOP states &quot;If the query includes an ORDER BY clause, the first expression rows, or expression percent of rows, ordered by the ORDER BY clause are returned. If the query has no ORDER BY clause, the order of the rows is arbitrary.&quot;

Example:

First for an example, we want to get the query to read the TABLE in a different order than the ORDER BY. To do that, the data should be stored opposite the ORDER BY. That can be accomplished via a CLUSTERED INDEX:

CREATE TABLE #Data
(
 Id_Clustered			TINYINT,
 Id_Ordered				TINYINT,
 CONSTRAINT Data_Id_PK PRIMARY KEY CLUSTERED (Id_Clustered)
);
INSERT INTO #Data(Id_Clustered, Id_Ordered)
 SELECT 1, 2 UNION ALL
 SELECT 2, 1;

Id_Clustered is the COLUMN the TABLE will be CLUSTERED on, whereas Id_Ordered will be in the ORDER BY of the query. The values in the two records are opposites. This will force a query without an ORDER BY and with an ORDER BY to be different:

SELECT
		Id_Clustered,
		Id_Ordered
FROM
		#Data;
Yeilds:
1,2
2,1

Because that is how it is CLUSTERED. But, if we add an ORDER BY:

SELECT
		Id_Clustered,
		Id_Ordered
FROM
		#Data
ORDER BY
		Id_Ordered;

The result is reversed to:
2,1
1,2

because it is following the ORDER BY statement.

Add a TOP 1 for the test to see which one gets pulled:

SELECT TOP 1
		Id_Clustered,
		Id_Ordered
FROM
		#Data
ORDER BY
		Id_Ordered;

Which shows:

2,1

This is the same as the ORDER BY results.

The Query plan shows the Clustered Index Scan first and then a Sort (Top N Sort). It specifically reordered the results to follow the ORDER BY.

This example shows that ORDER BY is handled first, and then TOP.]]></description>
		<content:encoded><![CDATA[<p>The help file for TOP states &#8220;If the query includes an ORDER BY clause, the first expression rows, or expression percent of rows, ordered by the ORDER BY clause are returned. If the query has no ORDER BY clause, the order of the rows is arbitrary.&#8221;</p>
<p>Example:</p>
<p>First for an example, we want to get the query to read the TABLE in a different order than the ORDER BY. To do that, the data should be stored opposite the ORDER BY. That can be accomplished via a CLUSTERED INDEX:</p>
<p>CREATE TABLE #Data<br />
(<br />
 Id_Clustered			TINYINT,<br />
 Id_Ordered				TINYINT,<br />
 CONSTRAINT Data_Id_PK PRIMARY KEY CLUSTERED (Id_Clustered)<br />
);<br />
INSERT INTO #Data(Id_Clustered, Id_Ordered)<br />
 SELECT 1, 2 UNION ALL<br />
 SELECT 2, 1;</p>
<p>Id_Clustered is the COLUMN the TABLE will be CLUSTERED on, whereas Id_Ordered will be in the ORDER BY of the query. The values in the two records are opposites. This will force a query without an ORDER BY and with an ORDER BY to be different:</p>
<p>SELECT<br />
		Id_Clustered,<br />
		Id_Ordered<br />
FROM<br />
		#Data;<br />
Yeilds:<br />
1,2<br />
2,1</p>
<p>Because that is how it is CLUSTERED. But, if we add an ORDER BY:</p>
<p>SELECT<br />
		Id_Clustered,<br />
		Id_Ordered<br />
FROM<br />
		#Data<br />
ORDER BY<br />
		Id_Ordered;</p>
<p>The result is reversed to:<br />
2,1<br />
1,2</p>
<p>because it is following the ORDER BY statement.</p>
<p>Add a TOP 1 for the test to see which one gets pulled:</p>
<p>SELECT TOP 1<br />
		Id_Clustered,<br />
		Id_Ordered<br />
FROM<br />
		#Data<br />
ORDER BY<br />
		Id_Ordered;</p>
<p>Which shows:</p>
<p>2,1</p>
<p>This is the same as the ORDER BY results.</p>
<p>The Query plan shows the Clustered Index Scan first and then a Sort (Top N Sort). It specifically reordered the results to follow the ORDER BY.</p>
<p>This example shows that ORDER BY is handled first, and then TOP.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
