<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	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>SQL Server Journey with SQL Authority &#187; SQL Joins</title>
	<atom:link href="http://blog.sqlauthority.com/category/sql-joins/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:09:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.sqlauthority.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/08e35387c05b61340e885b1763a69d9f?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>SQL Server Journey with SQL Authority &#187; SQL Joins</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.sqlauthority.com/osd.xml" title="SQL Server Journey with SQL Authority" />
	<atom:link rel='hub' href='http://blog.sqlauthority.com/?pushpress=hub'/>
		<item>
		<title>SQL SERVER &#8211; Quick Note about JOIN &#8211; Common Questions and Simple Answers</title>
		<link>http://blog.sqlauthority.com/2011/10/04/sql-server-quick-note-about-join-common-questions-and-simple-answers/</link>
		<comments>http://blog.sqlauthority.com/2011/10/04/sql-server-quick-note-about-join-common-questions-and-simple-answers/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 01:30:38 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14816</guid>
		<description><![CDATA[This blog post is written in response to the T-SQL Tuesday post of JOIN. This is a very interesting subject. Years ago, I wrote my article about SQL SERVER – Introduction to JOINs – Basic of JOINs, ‑ till date, it is my most favorite article on the blog. Today we are going to talk [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14816&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><img class="alignleft" src="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" alt="" width="123" height="123" /></p>
<p style="text-align:justify;">This blog post is written in response to the T-SQL Tuesday post of <a href="http://codegumbo.com/index.php/2011/09/27/tsql2sday-t-sql-tuesday-23early-edition/" target="_blank">JOIN</a><strong>.</strong> This is a very interesting subject. Years ago, I wrote my article about <a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/"><strong>SQL SERVER – Introduction to JOINs – Basic of JOINs</strong></a>, ‑ till date, it is my most favorite article on the blog.</p>
<p style="text-align:justify;">Today we are going to talk about join and lots of things related to the JOIN. I recently started office hours to answer questions and issues of the community. I receive so many questions that are related to JOIN. I will share few of the same over here. Most of them are basic, but note that the basics are of great importance.</p>
<p style="text-align:justify;">Without further ado, let me continue with the question and answers.</p>
<p style="text-align:justify;"><strong>Q:</strong> Which one of the following is a better method?<br />
Method 1:<br />
SELECT t1.*, t2.*<br />
FROM t1,t2<br />
WHERE t1.col1 = t2.col1</p>
<p style="text-align:justify;">Method 2:<br />
SELECT t1.*, t2.*<br />
FROM t1 INNER JOIN t2 ON t1.col1 = t2.col1</p>
<p style="text-align:justify;">A: The answer to this question will bring some interesting conversation. I strongly prefer method 2 because it is much cleaner to understand and if I have to use table level hints or so on, it is much convenient to do the same. I would suggest going ahead with method 2. Currently with regard to performance and execution plan, both the methods show the same (most of the time). However, with respect to standard and future innovation, method 2 is the way to go.</p>
<p style="text-align:justify;">When I have to perform a performance tuning task, and if I see method 1, I usually ask the developer to convert it to method 2 as I feel much more comfortable with method 2. Additionally, when you have to work with OUTER JOIN, you will have to do so.</p>
<p style="text-align:justify;"><strong>Q:</strong> What is better ‑ subquery or JOIN?<br />
Subquery:<br />
SELECT t1.*<br />
FROM t1<br />
WHERE t1.col1 IN (SELECT t2.col1 FROM t2)</p>
<p style="text-align:justify;">Join:<br />
SELECT t1.*<br />
SELECT t1.*, t2.*<br />
FROM t1 INNER JOIN t2 ON t1.col1 = t2.col1</p>
<p style="text-align:justify;">In this case, there is no right answer. You should use the one that gives you optimal performance. I have seen cases when the subquery gives optimal performance as well join giving optimal performance when compared to each other. I have seen either of them performing so well that I think one has to test out both the methods before selecting one. If you are facing situation where you are not sure which method you should select, I suggest that you go with your intuition. I still prefer JOIN over any other method, but in this case, I will suggest you to test your options.</p>
<p style="text-align:justify;"><strong>Q:</strong> How to simulate Join?<br />
A: I get this question a lot of times, and I have no answer. Here, I want your help as I do not even understand this question.</p>
<p style="text-align:justify;"><strong>Q:</strong> How can I change my LEFT JOIN to RIGHT JOIN and get the same answer?<br />
A: Sure. Here is quick example of the same:</p>
<p style="text-align:justify;">Left Join:<br />
SELECT t1.col1, t2.col2<br />
FROM t1 LEFT JOIN t2 ON ON t1.col1 = t2.col1</p>
<p style="text-align:justify;">Right Join:<br />
SELECT t1.col1, t2.col2<br />
FROM t2 RIGHT JOIN t1 ON ON t1.col1 = t2.col1</p>
<p style="text-align:justify;">Both of the above options will give you same result. However, the real question is why you want to do that. What is the reason that you want to change the left join to right join?</p>
<p style="text-align:justify;"><strong>Q:</strong> Does it matter how I write tables in my join if I am using INNER JOIN only?<br />
A: No it does not matter in case of INNER JOIN as the result will be the same, and the SQL Server Engine will figure out the optimal execution plans for your query. As your question clearly suggests that for any other kind of join (i.e., OUTER JOIN, CROSS JOIN), it will matter for sure. Additionally, there are cases with INNER JOIN ‑ when order is forced on them, they have shown a little performance enhancement. Here is a <a href="http://blog.sqlauthority.com/2008/08/02/sql-server-effect-of-order-of-join-in-query/" target="_blank"><strong>quick example</strong></a> of the same.</p>
<p style="text-align:justify;">If you have attended my session of <a href="http://blog.sqlauthority.com/2011/09/28/sqlauthority-news-tomorrow-online-session-ancient-trade-of-performance-tuning-index-beyond-index-and-no-index/" target="_blank"><strong>Virtual Tech Days</strong></a> few days ago, you would have seen the example of the how forceorder hint works.</p>
<p style="text-align:justify;"><strong>Q:</strong> Is there a quick tutorial to Joins?<br />
A: I have written an article on this subject earlier, and as I said earlier in this article, I personally like the same a lot. Here you can read about the same: <a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/"><strong>Introduction to JOINs – Basic of JOINs</strong></a>.</p>
<p style="text-align:justify;"><strong>Q:</strong> Is there any book available to learn T-SQL, which explains various concepts like this easily?<br />
A: I am bit biased but you can read about <a href="http://blog.sqlauthority.com/sql-server-books/" target="_blank"><strong>my books</strong></a> over here.</p>
<p style="text-align:justify;"><strong>Q:</strong> Is SELF JOIN is a type of INNER JOIN or OUTER JOIN?<br />
A: In fact, it is both an inner as well as outer join. Self Join is a very interesting subject. Here is an interesting article that I have written earlier on this subject: <a href="http://blog.sqlauthority.com/2010/07/08/sql-server-the-self-join-inner-join-and-outer-join/"><strong>SQL SERVER – The Self Join – Inner Join and Outer Join </strong></a>.</p>
<p style="text-align:justify;"><strong>Q:</strong> In case of the OUTER JOIN, where should I put the condition?<br />
A: This question requires a detailed answer, I have written a detailed blog post on this subject over here: <a href="http://blog.sqlauthority.com/2009/03/15/sql-server-interesting-observation-of-on-clause-on-left-join-how-on-clause-effects-resultset-in-left-join/" target="_blank"><strong>How ON Clause Effects Resultset in LEFT JOIN </strong></a>.</p>
<p style="text-align:justify;"><strong>Q:</strong> What is Optimal LEFT JOIN or NOT IN?<br />
A: I personally prefer LEFT JOIN as I have seen LEFT JOIN doing better in many cases. Once again, I suggest you should test it with your query. Here is a quick example of the same: <a href="http://blog.sqlauthority.com/2008/04/22/sql-server-better-performance-left-join-or-not-in/" target="_blank"><strong>Differences Between Left Join and Left Outer Join</strong></a>.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14816/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14816/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14816/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14816/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14816/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14816/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14816/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14816/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14816/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14816/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14816/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14816/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14816/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14816/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14816&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/10/04/sql-server-quick-note-about-join-common-questions-and-simple-answers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER- Differences Between Left Join and Left Outer Join</title>
		<link>http://blog.sqlauthority.com/2011/01/22/sql-server-differences-between-left-join-and-left-outer-join/</link>
		<comments>http://blog.sqlauthority.com/2011/01/22/sql-server-differences-between-left-join-and-left-outer-join/#comments</comments>
		<pubDate>Sat, 22 Jan 2011 01:30:41 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=11737</guid>
		<description><![CDATA[There are a few questions that I had decided not to discuss on this blog because I think they are very simple and many of us know it. Many times, I even receive not-so positive notes from several readers when I am writing something simple. However, assuming that we know all and beginners should know [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11737&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">There are a few questions that I had decided not to discuss on this blog because I think they are very simple and many of us know it. Many times, I even receive not-so positive notes from several readers when I am writing something simple. However, assuming that we know all and beginners should know everything is not the right attitude.</p>
<p style="text-align:justify;">Since day 1, I have been keeping a small journal regarding questions that I receive in this blog. There are around 200+ questions I receive every day through emails, comments and occasional phone calls. Yesterday, I received a comment with the following question:</p>
<p style="text-align:justify;"><strong>What are the differences between Left Join and Left Outer Join?</strong> <a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/#comment-112416" target="_blank">Click here to read original comment</a>.</p>
<p style="text-align:justify;">This question has triggered the threshold of receiving the same question repeatedly. Here is the answer:</p>
<p style="text-align:justify;">There is absolutely <strong>no difference</strong> between LEFT JOIN and LEFT OUTER JOIN. The same is true for RIGHT JOIN and RIGHT OUTER JOIN. When you use LEFT JOIN keyword in SQL Server, it means LEFT OUTER JOIN only.</p>
<p style="text-align:justify;">I have already written in-depth visual diagram discussing the JOINs. I encourage all of you to read the article for further understanding of the JOINs:</p>
<p style="text-align:justify;"><span style="font-size:20px;font-weight:bold;"><a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/" target="_blank">Read Introduction to JOINs – Basic of JOINs</a></span></p>
<p style="text-align:justify;">Reference:<strong> Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/11737/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/11737/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/11737/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/11737/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/11737/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/11737/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/11737/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/11737/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/11737/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/11737/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/11737/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/11737/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/11737/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/11737/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=11737&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/01/22/sql-server-differences-between-left-join-and-left-outer-join/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQLAuthority News &#8211; Statistics and Best Practices &#8211; Virtual Tech Days &#8211; Nov 22, 2010</title>
		<link>http://blog.sqlauthority.com/2010/11/22/sqlauthority-news-statistics-and-best-practices-virtual-tech-days-nov-22-2010/</link>
		<comments>http://blog.sqlauthority.com/2010/11/22/sqlauthority-news-statistics-and-best-practices-virtual-tech-days-nov-22-2010/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 01:30:43 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[MVP]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLAuthority News]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Statistics]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10962</guid>
		<description><![CDATA[I am honored that I have been invited to speak at Virtual TechDays on Nov 22, 2010 by Microsoft. I will be speaking on my favorite subject of Statistics and Best Practices. This exclusive online event will have 80 deep technical sessions across 3 days – and, attendance is completely FREE. There are dedicated tracks [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10962&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I am honored that I have been invited to speak at Virtual TechDays on Nov 22, 2010 by Microsoft. I will be speaking on my favorite subject of Statistics and Best Practices.</p>
<p style="text-align:justify;">This exclusive online event will have 80 deep technical sessions across 3  days – and, attendance is completely FREE. There are dedicated tracks  for Architects, Software Developers/Project Managers, Infrastructure  Managers/Professionals and Enterprise Developers. So, REGISTER for this  exclusive online event TODAY.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/statistics.jpg" alt="" width="472" height="332" /></p>
<p style="text-align:justify;"><strong>Statistics and Best Practices</strong><br />
Timing: 11:45am-12:45pm<br />
Statistics are a key part of getting solid performance. In this session we will go over the basics of the statistics and various best practices related to Statistics. We will go over various frequently asked questions like a) when to update statistics, b) different between sync and async update of statistics c) best method to update statistics d) optimal interval of updating statistics. We will also discuss the pros and cons of the statistics update. This session is for all of you &#8211; whether you&#8217;re a DBA or developer!</p>
<p style="text-align:justify;">You can register for this event over <a href="http://virtualtechdays.com/agendaday1.aspx">here</a>.</p>
<p style="text-align:justify;">If you have never attended my session on  this subject I strongly suggest that you attend the event as this is  going to be very interesting conversation between us. If you have  attended this session earlier, this will contain few new information  which will for sure interesting to share with all.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/mvp/'>MVP</a>, <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlauthority/sqlauthority-news/'>SQLAuthority News</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/sql-statistics/'>SQL Statistics</a>, <a href='http://blog.sqlauthority.com/tag/statistics/'>Statistics</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10962/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10962/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10962/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10962/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10962/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10962/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10962/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10962/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10962/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10962/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10962/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10962/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10962/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10962/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10962&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/11/22/sqlauthority-news-statistics-and-best-practices-virtual-tech-days-nov-22-2010/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/statistics.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER – SELF JOIN Not Allowed in Indexed View – Limitation of the View 9</title>
		<link>http://blog.sqlauthority.com/2010/09/26/sql-server-self-join-not-allowed-in-indexed-view-limitation-of-the-view-9/</link>
		<comments>http://blog.sqlauthority.com/2010/09/26/sql-server-self-join-not-allowed-in-indexed-view-limitation-of-the-view-9/#comments</comments>
		<pubDate>Sun, 26 Sep 2010 01:30:32 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL View]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10322</guid>
		<description><![CDATA[Update: Please read the summary post of all the 11 Limitations of the view SQL SERVER – The Limitations of the Views – Eleven and more… Previously, I wrote an article about SQL SERVER – The Self Join – Inner Join and Outer Join, and that blog post seems very popular because of its interesting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10322&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#ff0000;">Update</span>: Please read the summary post of all the 11 Limitations of the view <strong><a rel="bookmark" href="http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/" target="_blank">SQL SERVER – The Limitations of the Views – Eleven and more…</a></strong><br />
Previously, I wrote an article about <a href="http://blog.sqlauthority.com/2010/07/08/sql-server-the-self-join-inner-join-and-outer-join/" target="_blank"><strong>SQL SERVER – The Self Join – Inner Join and Outer Join</strong></a>, and that blog post seems very popular because of its interesting points. It is quite common to think that Self Join is also only Inner Join, but the reality is that it can be anything. The concept of Self Join is very useful that we use it quite often in our coding. However, this is not allowed in the Index View. I will be using the same example  that I have created earlier for the said article.</p>
<p>Let us first create the same table for an employee. One of the columns in this table contains the ID of the manger, who is an employee of that company, at the same time. This way, all the employees and their managers are present in the same table. If we want to find the manager of a particular employee, we need to use Self Join.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">TempDb<br />
GO<br />
</span><span style="color:green;">-- Create a Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">Employee</span><span style="color:gray;">(<br />
</span><span style="color:black;">EmployeeID </span><span style="color:blue;">INT PRIMARY KEY</span><span style="color:gray;">,<br />
</span><span style="color:black;">Name </span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">50</span><span style="color:gray;">),<br />
</span><span style="color:black;">ManagerID </span><span style="color:blue;">INT<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert Sample Data<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">Employee<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:red;">'Mike'</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">, </span><span style="color:red;">'David'</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">, </span><span style="color:red;">'Roger'</span><span style="color:gray;">, NULL<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">, </span><span style="color:red;">'Marry'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">5</span><span style="color:gray;">, </span><span style="color:red;">'Joseph'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">7</span><span style="color:gray;">, </span><span style="color:red;">'Ben'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
GO<br />
</span><span style="color:green;">-- Check the data<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/selfjoini1.jpg" alt="" width="243" height="155" /></p>
<p>We will now utilize Inner Join to find the employees and their managers’ details.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Inner Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">e1.Name EmployeeName</span><span style="color:gray;">, </span><span style="color:black;">e2.name </span><span style="color:blue;">AS </span><span style="color:black;">ManagerName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee e1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Employee e2<br />
</span><span style="color:blue;">ON </span><span style="color:black;">e1.ManagerID </span><span style="color:blue;">= </span><span style="color:black;">e2.EmployeeID<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/selfjoini2.jpg" alt="" width="235" height="136" /></p>
<p>Now let us try to create View on the table. This will allow well construction of the View without any issues associated with it.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Create a View<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">myJoinView<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">SCHEMABINDING<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:black;">e1.Name EmployeeName</span><span style="color:gray;">, </span><span style="color:black;">e2.name </span><span style="color:blue;">AS </span><span style="color:black;">ManagerName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.Employee e1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">dbo.Employee e2<br />
</span><span style="color:blue;">ON </span><span style="color:black;">e1.ManagerID </span><span style="color:blue;">= </span><span style="color:black;">e2.EmployeeID<br />
GO<br />
</span></code></p>
<p>Now let us try to create a Clustered Index on the View.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Attempt to Create Index on View will thrown an error<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_MyJoinView] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[myJoinView]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[EmployeeName] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p>Unfortunately, the above attempt will not allow you to create the Clustered Index, as evidenced by an error message. It will throw following error suggesting that SELF JOIN is now allowed in the table.</p>
<p style="text-align:justify;"><span style="color:#ff0000;">Msg 1947, Level 16, State 1, Line 2<br />
Cannot create index on view &#8220;tempdb.dbo.myJoinView&#8221;. The view contains a self join on &#8220;tempdb.dbo.Employee&#8221;.</span></p>
<p>The generic reason provided is that it is very expensive to manage the view for SQL Server when SELF JOIN is implemented in the query.</p>
<p>If any of you has a better explanation of this subject, please post it here through your comments, and I will publish it with due credit.</p>
<p>The complete script for the example is given below:</p>
<p><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">TempDb<br />
GO<br />
</span><span style="color:green;">-- Create a Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">Employee</span><span style="color:gray;">(<br />
</span><span style="color:black;">EmployeeID </span><span style="color:blue;">INT PRIMARY KEY</span><span style="color:gray;">,<br />
</span><span style="color:black;">Name </span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">50</span><span style="color:gray;">),<br />
</span><span style="color:black;">ManagerID </span><span style="color:blue;">INT<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert Sample Data<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">Employee<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:red;">'Mike'</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">, </span><span style="color:red;">'David'</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">, </span><span style="color:red;">'Roger'</span><span style="color:gray;">, NULL<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">, </span><span style="color:red;">'Marry'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">5</span><span style="color:gray;">, </span><span style="color:red;">'Joseph'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">7</span><span style="color:gray;">, </span><span style="color:red;">'Ben'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
GO<br />
</span><span style="color:green;">-- Check the data<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee<br />
GO<br />
</span><span style="color:green;">-- Inner Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">e1.Name EmployeeName</span><span style="color:gray;">, </span><span style="color:black;">e2.name </span><span style="color:blue;">AS </span><span style="color:black;">ManagerName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee e1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Employee e2<br />
</span><span style="color:blue;">ON </span><span style="color:black;">e1.ManagerID </span><span style="color:blue;">= </span><span style="color:black;">e2.EmployeeID<br />
GO<br />
</span><span style="color:green;">-- Create a View<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">myJoinView<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">SCHEMABINDING<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:black;">e1.Name EmployeeName</span><span style="color:gray;">, </span><span style="color:black;">e2.name </span><span style="color:blue;">AS </span><span style="color:black;">ManagerName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.Employee e1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">dbo.Employee e2<br />
</span><span style="color:blue;">ON </span><span style="color:black;">e1.ManagerID </span><span style="color:blue;">= </span><span style="color:black;">e2.EmployeeID<br />
GO<br />
</span><span style="color:green;">-- Attempt to Create Index on View will thrown an error<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">[IX_MyJoinView] </span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[myJoinView]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[EmployeeName] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/*<br />
Msg 1947, Level 16, State 1, Line 2<br />
Cannot create index on view "tempdb.dbo.myJoinView". The view contains a self join on "tempdb.dbo.Employee".<br />
*/<br />
-- Clean up<br />
</span><span style="color:blue;">DROP VIEW </span><span style="color:black;">myJoinView<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">Employee<br />
GO</span></code></p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sql-view/'>SQL View</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10322/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10322&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/26/sql-server-self-join-not-allowed-in-indexed-view-limitation-of-the-view-9/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/selfjoini1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/selfjoini2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Outer Join Not Allowed in Indexed Views &#8211; Limitation of the View 8</title>
		<link>http://blog.sqlauthority.com/2010/09/24/sql-server-outer-join-not-allowed-in-indexed-views-limitation-of-the-view-8/</link>
		<comments>http://blog.sqlauthority.com/2010/09/24/sql-server-outer-join-not-allowed-in-indexed-views-limitation-of-the-view-8/#comments</comments>
		<pubDate>Fri, 24 Sep 2010 01:30:13 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL View]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10311</guid>
		<description><![CDATA[Update: Please read the summary post of all the 11 Limitations of the view SQL SERVER – The Limitations of the Views – Eleven and more… This blog post was previously published over here. I am republishing it in the series Limitation of the Views with a few modifications. While reading the white paper Improving [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10311&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#ff0000;">Update</span>: Please read the summary post of all the 11 Limitations of the view <strong><a href="http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/" rel="bookmark" target="_blank">SQL SERVER – The Limitations of the Views – Eleven and more…</a></strong></p>
<p style="text-align:justify;">This blog post was previously published over <a href="http://blog.sqlauthority.com/2010/08/23/sql-server-order-by-does-not-work-limitation-of-the-views-part-1/" target="_blank">here</a>. I am republishing it in the series <em>Limitation of the Views </em>with a few modifications.</p>
<p style="text-align:justify;">While reading the white paper <strong><a href="http://msdn.microsoft.com/en-us/library/dd171921%28SQL.100%29.aspx" target="_blank">Improving Performance with SQL Server 2008 Indexed Views</a></strong>, I noticed that it says outer joins are NOT allowed in the indexed views. Here, I have created an example to demonstrate why this is so.</p>
<p style="text-align:justify;">Rows can logically disappear from an Indexed View based on OUTER JOIN when you insert data into a base table. This makes the OUTER JOIN view to be increasingly updated, which is relatively difficult to implement. In addition, the performance of the implementation would be slower than for views based on standard (INNER) JOIN.</p>
<p style="text-align:justify;">The reader was confused with my answer and wanted me to explain it further. Here is the example that I have quickly put together to demonstrate the behavior described in the above statement:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">tempdb<br />
GO<br />
</span><span style="color:green;">-- Create Two Tables<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">BaseTable </span><span style="color:gray;">(</span><span style="color:black;">ID1 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">Col1 </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">JoinedTable </span><span style="color:gray;">(</span><span style="color:black;">ID2 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">Col2 </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert Values in Tables<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">BaseTable </span><span style="color:gray;">(</span><span style="color:black;">ID1</span><span style="color:gray;">,</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:red;">'First'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:red;">'Second'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">JoinedTable </span><span style="color:gray;">(</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">Col2</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:red;">'First'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:red;">'Second'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:red;">'Third'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:red;">'Fourth'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Use Outer Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">jt.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">BaseTable bt<br />
</span><span style="color:magenta;">RIGHT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">JoinedTable jt </span><span style="color:blue;">ON </span><span style="color:black;">bt.ID1 </span><span style="color:blue;">= </span><span style="color:black;">jt.ID2<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">bt.ID1 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">The script above will give us the following output:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/outerjoinmiss1.jpg" alt="" width="240" height="101" /></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Now Insert Rows in Base Table<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">BaseTable </span><span style="color:gray;">(</span><span style="color:black;">ID1</span><span style="color:gray;">,</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:red;">'Third'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- You will notice that one row less retrieved from Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">jt.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">BaseTable bt<br />
</span><span style="color:magenta;">RIGHT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">JoinedTable jt </span><span style="color:blue;">ON </span><span style="color:black;">bt.ID1 </span><span style="color:blue;">= </span><span style="color:black;">jt.ID2<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">bt.ID1 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Clean up<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">BaseTable<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">JoinedTable<br />
GO<br />
</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/outerjoinmiss2.jpg" alt="" width="255" height="95" /></p>
<p style="text-align:justify;">After running this script, you will notice that as the base table gains one row, the result loses one row. Going back to the white paper I mentioned earlier, I believe this is an expensive way to manage the same issue as to why it is not allowed in Indexed View.</p>
<p style="text-align:justify;">Additionally, SQL Server Expert <a href="http://www.sqlbiinfo.com/" target="_blank"><strong>Ramdas</strong></a> provided excellent explanations regarding NULL and why resultset maintenance is expensive, over <a href="http://blog.sqlauthority.com/2010/06/29/sql-server-outer-join-not-allowed-in-indexed-views/" target="_blank">here</a>.</p>
<p style="padding-left:30px;text-align:justify;"><em>&#8220;A disadvantage of outer joins in SQL is that they generate nulls in the result set. Those nulls are indistinguishable from other nulls that are not generated by the outer join operation. There is no “standard” semantics for nulls in SQL but in many common situations, the appearance of nulls in outer joins doesn’t really correspond to the way nulls are returned and used in other places. Therefore, the presence of nulls in outer joins creates a certain amount of ambiguity.&#8221;</em></p>
<p style="text-align:justify;">This series is indeed getting very interesting. What are your suggestions?</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sql-view/'>SQL View</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10311/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10311&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/24/sql-server-outer-join-not-allowed-in-indexed-views-limitation-of-the-view-8/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/outerjoinmiss1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/outerjoinmiss2.jpg" medium="image" />
	</item>
		<item>
		<title>SQLAuthority News &#8211; 2 Sessions at TechInsight 2010 &#8211; June 29 &#8211; July 1, 2010</title>
		<link>http://blog.sqlauthority.com/2010/07/28/sqlauthority-news-2-sessions-at-techinsight-2010-june-29-july-1-2010/</link>
		<comments>http://blog.sqlauthority.com/2010/07/28/sqlauthority-news-2-sessions-at-techinsight-2010-june-29-july-1-2010/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 01:30:04 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MVP]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLAuthority Author Visit]]></category>
		<category><![CDATA[SQLAuthority News]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Spatial Database]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9653</guid>
		<description><![CDATA[Earlier this month, I got the opportunity to visit Malaysia for community sessions on June 29 &#8211; July 1, 2010 at Kuala Lumpur, Malaysia, which I would consider as valuable experience. I presented two different sessions at the event. The event was extremely popular in local community, and I had great time meeting people in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9653&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Earlier this month, I got the opportunity to visit <strong>Malaysia </strong>for community sessions on <strong>June 29 &#8211; July 1, 2010</strong> at <strong>Kuala Lumpur</strong>, <strong>Malaysia, </strong>which I would consider as valuable experience. I presented two different sessions at the event. The event was extremely popular in local community, and I had great time meeting people in Malaysia. I must say that the best thing about Kuala Lumpur is the people and their response.</p>
<div class="mceTemp" style="text-align:justify;">
<dl class="wp-caption alignnone">
<dt class="wp-caption-dt"><img title="Malaysia Twin Towers" src="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia (2).jpg" alt="Malaysia Twin Towers" width="500" height="666" /></dt>
<dd class="wp-caption-dd">Malaysia Twin Towers</dd>
</dl>
</div>
<p style="text-align:justify;">Techinsights is a major technology conference to network with like-minded peers and also up-skill your knowledge on latest technologies. An event that offers opportunity to dabble in hardcore technologies with in-depth and hands-on demonstration by Microsoft MVPs and industry experts local and abroad. This three-day event will challenge what you think you already know. You&#8217;ll return to the office with cutting-edge insights and expertise that will make life easier for you (and everyone else) at work. This round, we have a special highlight on new technologies such as SharePoint 2010, Visual Studio 2010, SQL Server 2008 R2, Silverlight 4, Windows 7, Windows Server 2008 R2 and many more. TechInsight is an event created by techies for techies. There is no marketing involved. It is indeed an experience to rediscover the uber-geek within you. Sign up today to secure your seat.</p>
<div class="mceTemp" style="text-align:justify;">
<dl class="wp-caption alignnone">
<dt class="wp-caption-dt"><img title="Techinsight - 2 Sessions" src="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia (3).jpg" alt="Techinsight - 2 Sessions" width="500" height="666" /></dt>
<dd class="wp-caption-dd">Techinsight &#8211; 2 Sessions</dd>
</dl>
</div>
<p style="text-align:justify;">I presented two sessions there. <strong>Both of my sessions were in the TOP 5 sessions of Development track. Additionally, my session on Join got the highest ranking ever in Dev Track</strong>.</p>
<p style="text-align:justify;"><strong>1) My Join, Your Join and Our Joins – The Story of Joins</strong></p>
<p style="text-align:justify;">Joins are very mysterious; there are many myths and confusions. This session will address all of them and also tell the story of how they act when it is about performance. Does the order of table in Join matter? Does the right or left join any different to each other? Does the Join increase IO? When is an outer join not an outer join and inner join? All these questions are answered and many more stories of Joins are included. Learn the simple tricks to get the maximum out of this tool.</p>
<p style="text-align:justify;"><em>Session Evaluations</em></p>
<p style="text-align:justify;">Overall session rating							<strong>7.5</strong><br />
How valuable was the content presented							7.467741935<br />
How effectively did the presenter communicate the content							7.596774194</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia (4).jpg" alt="" width="500" height="375" /></p>
<p style="text-align:justify;"><strong>2) Spatial Database – The Indexing Story</strong></p>
<p style="text-align:justify;">The world was believed to be flat but no more. Now SQL Server supports the spatial datatypes and many more functions. This session addresses the most vital part of Spatial datatypes and talks about how to improve the performance for the application, which is already blazing fast. We will look at how indexes are behaving with different spatial datatypes and how they can help to improve the performance and also learn the pitfalls to avoid them affecting performance.</p>
<p style="text-align:justify;"><em>Session Evaluations</em></p>
<p style="text-align:justify;">Overall session rating							<strong>7.237288136</strong><br />
How valuable was the content presented							7.322033898<br />
How effectively did the presenter communicate the content							7.457627119</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia (5).jpg" alt="" width="500" height="375" /></p>
<p style="text-align:justify;">I must express my special thanks to all the organizers of the event &#8211; <strong>Ervin, Walter, Raymond, and Patrick</strong> (in no particular order). They did an excellent job, and all the attendees of the event had great time as well. The food was awesome, and the response was excellent. After one month, when I am writing this review, I am still thinking of the wonderful experience I had from this event. This makes me want to not miss this event any year.</p>
<div class="mceTemp" style="text-align:justify;">
<dl class="wp-caption alignnone">
<dt class="wp-caption-dt"><img class="  " title="Techinsight - Event Organizers" src="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia.jpg" alt="Techinsight - Event Organizers" width="500" height="375" /></dt>
<dd class="wp-caption-dd">Techinsight &#8211; Event Organizers</dd>
</dl>
</div>
<p style="text-align:justify;">This one event is truly TechEd quality event in Malaysia. Kudos to the organizers and Microsoft.</p>
<div class="mceTemp" style="text-align:justify;">
<dl class="wp-caption alignnone">
<dt class="wp-caption-dt"><img title="Techinsight - Kuala Lumpur, Malaysia" src="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia (6).jpg" alt="Techinsight - Kuala Lumpur, Malaysia" width="500" height="375" /></dt>
<dd class="wp-caption-dd">Techinsight &#8211; Kuala Lumpur, Malaysia</dd>
</dl>
</div>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/dba/'>DBA</a>, <a href='http://blog.sqlauthority.com/category/mvp/'>MVP</a>, <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlauthority/sqlauthority-author-visit/'>SQLAuthority Author Visit</a>, <a href='http://blog.sqlauthority.com/category/sqlauthority/sqlauthority-news/'>SQLAuthority News</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/spatial-database/'>Spatial Database</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9653/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9653/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9653/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9653/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9653/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9653/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9653/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9653/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9653/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9653/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9653/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9653/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9653/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9653/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9653&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/07/28/sqlauthority-news-2-sessions-at-techinsight-2010-june-29-july-1-2010/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia(2).jpg" medium="image">
			<media:title type="html">Malaysia Twin Towers</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia(3).jpg" medium="image">
			<media:title type="html">Techinsight - 2 Sessions</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia(4).jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia(5).jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia.jpg" medium="image">
			<media:title type="html">Techinsight - Event Organizers</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/Malaysia/KL_Malaysia(6).jpg" medium="image">
			<media:title type="html">Techinsight - Kuala Lumpur, Malaysia</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; The Self Join &#8211; Inner Join and Outer Join</title>
		<link>http://blog.sqlauthority.com/2010/07/08/sql-server-the-self-join-inner-join-and-outer-join/</link>
		<comments>http://blog.sqlauthority.com/2010/07/08/sql-server-the-self-join-inner-join-and-outer-join/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 01:30:54 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9488</guid>
		<description><![CDATA[Self Join has always been an note-worthy case. It is interesting to ask questions on self join in a room full of developers. I often ask &#8211; if there are three kind of joins, i.e.- Inner Join, Outer Join and Cross Join; what type of join is Self Join? The usual answer is that it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9488&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Self Join has always been an note-worthy case. It is interesting to ask questions on self join in a room full of developers. I often ask &#8211; if there are three kind of joins, i.e.- Inner Join, Outer Join and Cross Join; what type of join is Self Join? The usual answer is that it is an Inner Join. In fact, it can be classified under any type of join. I have previously written about this in my<a href="http://blog.sqlauthority.com/2008/09/13/sql-server-2008-interview-questions-and-answers-part-2/" target="_blank"><strong> interview questions and answers series</strong></a>. I have also mentioned this subject when I explained the joins in detail over <a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/" target="_blank"><strong>SQL SERVER – Introduction to JOINs – Basic of JOINs</strong></a>.</p>
<p style="text-align:justify;">When I mention that Self Join can be the outer join, I often get a request for an example for the same. I have created example using <a href="http://blog.sqlauthority.com/2007/06/03/sql-server-2005-explanation-and-example-self-join/" target="_blank"><strong>AdventureWorks Database of Self Join</strong></a> earlier, but that was meant for inner join as well. Let us create a new example today, where we will see how Self Join can be implemented as an Inner Join as well as Outer Join.</p>
<p style="text-align:justify;">Let us first create the same table for an employee. One of the columns in the same table contains the ID of manger, who is also an employee for the same company. This way, all the employees and their managers are present in the same table. If we want to find the manager of a particular employee, we need use self join.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">TempDb<br />
GO<br />
</span><span style="color:green;">-- Create a Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">Employee</span><span style="color:gray;">(<br />
</span><span style="color:black;">EmployeeID </span><span style="color:blue;">INT PRIMARY KEY</span><span style="color:gray;">,<br />
</span><span style="color:black;">Name </span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">50</span><span style="color:gray;">),<br />
</span><span style="color:black;">ManagerID </span><span style="color:blue;">INT<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert Sample Data<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">Employee<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:red;">'Mike'</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">, </span><span style="color:red;">'David'</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">, </span><span style="color:red;">'Roger'</span><span style="color:gray;">, NULL<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">, </span><span style="color:red;">'Marry'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">5</span><span style="color:gray;">, </span><span style="color:red;">'Joseph'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">7</span><span style="color:gray;">, </span><span style="color:red;">'Ben'</span><span style="color:gray;">,</span><span style="color:black;">2<br />
GO<br />
</span><span style="color:green;">-- Check the data<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/selfjoini1.jpg" alt="" width="243" height="155" /></p>
<p style="text-align:justify;">We will now use inner join to find the employees and their managers’ details.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Inner Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">e1.Name EmployeeName</span><span style="color:gray;">, </span><span style="color:black;">e2.name </span><span style="color:blue;">AS </span><span style="color:black;">ManagerName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee e1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Employee e2<br />
</span><span style="color:blue;">ON </span><span style="color:black;">e1.ManagerID </span><span style="color:blue;">= </span><span style="color:black;">e2.EmployeeID<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/selfjoini2.jpg" alt="" width="235" height="136" /></p>
<p style="text-align:justify;">From the result set, we can see that all the employees who have a manager are visible. However we are unable to find out the top manager of the company as he is not visible in our resultset. The reason for the same is that due to inner join, his name is filtered out. Inner join does not bring any result which does not have manager id. Let us convert Inner Join to Outer Join and then see the resultset.</p>
<p><code style="font-size:12px;"><span style="color:green;">-- Outer Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">e1.Name EmployeeName</span><span style="color:gray;">, </span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:black;">e2.name</span><span style="color:gray;">, </span><span style="color:red;">'Top Manager'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">ManagerName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Employee e1<br />
</span><span style="color:magenta;">LEFT </span><span style="color:blue;">JOIN </span><span style="color:black;">Employee e2<br />
</span><span style="color:blue;">ON </span><span style="color:black;">e1.ManagerID </span><span style="color:blue;">= </span><span style="color:black;">e2.EmployeeID<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/selfjoini3.jpg" alt="" width="242" height="156" /></p>
<p style="text-align:justify;">Once we convert Inner Join to Outer Join, we can see the Top Manager as well. Here we have seen how Self Join can behave as an inner join as well as an outer join.</p>
<p style="text-align:justify;">As I said earlier, many of you know these details, but there are many who are still confused about this concept. I hope that this concept is clear from this post.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/readers-question/'>Readers Question</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9488/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9488&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/07/08/sql-server-the-self-join-inner-join-and-outer-join/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/selfjoini1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/selfjoini2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/selfjoini3.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Outer Join Not Allowed in Indexed Views</title>
		<link>http://blog.sqlauthority.com/2010/06/29/sql-server-outer-join-not-allowed-in-indexed-views/</link>
		<comments>http://blog.sqlauthority.com/2010/06/29/sql-server-outer-join-not-allowed-in-indexed-views/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 01:30:11 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Statistics]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9372</guid>
		<description><![CDATA[I recently received an email that contains a question from one of my readers. I have already replied the answer to his email, but I would still like to bring it to your attention and ask if you think I could have done any better with the example I gave. The question was raised when [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9372&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I recently received an email that contains a question from one of my readers. I have already replied the answer to his email, but I would still like to bring it to your attention and ask if you think I could have done any better with the example I gave.</p>
<p style="text-align:justify;">The question was raised when the email sender read the white paper, <a href="http://msdn.microsoft.com/en-us/library/dd171921%28SQL.100%29.aspx" target="_blank">Improving Performance with SQL Server 2008 Indexed Views</a>. If you scroll all the way down through the said white paper, there are several questions and answers.</p>
<p style="text-align:justify;"><strong>Q: Why can&#8217;t I use OUTER JOIN in an Indexed view?</strong></p>
<p style="text-align:justify;"><strong>A:</strong> Rows can logically disappear from an Indexed view based on OUTER JOIN when you insert data into a base table. This makes the OUTER JOIN view to be increasingly updated, which is relatively difficult to implement. In addition, the performance of the implementation would be slower than for views based on standard (INNER) JOIN.</p>
<p style="text-align:justify;">The reader was confused with my answer and wanted me to explain it further. Here is the example which I have quickly put together to demonstrate the behavior described in the above statement.<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">tempdb<br />
GO<br />
</span><span style="color:green;">-- Create Two Tables<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">BaseTable </span><span style="color:gray;">(</span><span style="color:black;">ID1 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">Col1 </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">JoinedTable </span><span style="color:gray;">(</span><span style="color:black;">ID2 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">Col2 </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert Values in Tables<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">BaseTable </span><span style="color:gray;">(</span><span style="color:black;">ID1</span><span style="color:gray;">,</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:red;">'First'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:red;">'Second'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">JoinedTable </span><span style="color:gray;">(</span><span style="color:black;">ID2</span><span style="color:gray;">,</span><span style="color:black;">Col2</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:red;">'First'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:red;">'Second'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:red;">'Third'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:red;">'Fourth'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Use Outer Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">jt.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">BaseTable bt<br />
</span><span style="color:magenta;">RIGHT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">JoinedTable jt </span><span style="color:blue;">ON </span><span style="color:black;">bt.ID1 </span><span style="color:blue;">= </span><span style="color:black;">jt.ID2<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">bt.ID1 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">The script above will give us the following output:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/outerjoinmiss1.jpg" alt="" width="240" height="101" /></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Now Insert Rows in Base Table<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">BaseTable </span><span style="color:gray;">(</span><span style="color:black;">ID1</span><span style="color:gray;">,</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:red;">'Third'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- You will notice that one row less retrieved from Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">jt.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">BaseTable bt<br />
</span><span style="color:magenta;">RIGHT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">JoinedTable jt </span><span style="color:blue;">ON </span><span style="color:black;">bt.ID1 </span><span style="color:blue;">= </span><span style="color:black;">jt.ID2<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">bt.ID1 </span><span style="color:blue;">IS </span><span style="color:gray;">NULL<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Clean up<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">BaseTable<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">JoinedTable<br />
GO<br />
</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/outerjoinmiss2.jpg" alt="" width="255" height="95" /></p>
<p style="text-align:justify;">After running this script, you will notice that as the base table gains one row, the result loses one row. Going back to the white paper mentioned earlier, I believe this is expensive to manage for the same reason why it is not allowed in Indexed View.</p>
<p style="text-align:justify;">Let me know if you have a better example to demonstrate this behavior in the Outer Join.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/sql-statistics/'>SQL Statistics</a>, <a href='http://blog.sqlauthority.com/tag/statistics/'>Statistics</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9372/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9372&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/29/sql-server-outer-join-not-allowed-in-indexed-views/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/outerjoinmiss1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/outerjoinmiss2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Default Statistics on Column &#8211; Automatic Statistics on Column</title>
		<link>http://blog.sqlauthority.com/2010/06/28/sql-server-default-statistics-on-column-automatic-statistics-on-column/</link>
		<comments>http://blog.sqlauthority.com/2010/06/28/sql-server-default-statistics-on-column-automatic-statistics-on-column/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 01:30:33 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9377</guid>
		<description><![CDATA[During the SQL Server Training, I frequently noticed confusion in people in terms of Statistics. Many people have no idea on how Statistics works. There are so many misconceptions with respect to Statistics. I recently had an interesting conversation with one attendee who believed that Statistics only exists on Column if there is an Index [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9377&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">During the SQL Server Training, I frequently noticed confusion in people in terms of Statistics. Many people have no idea on how Statistics works. There are so many misconceptions with respect to Statistics. I recently had an interesting conversation with one attendee who believed that Statistics only exists on Column if there is an Index on the Column, or if we explicitly create Statistics on it.</p>
<p style="text-align:justify;">The truth is, Statistics can be in a table even though there is no Index in it. If you have the auto- create and/or auto-update Statistics feature turned on for SQL Server database, Statistics will be automatically created on the Column based on a few conditions. Please read my previously posted article, <a href="http://blog.sqlauthority.com/2010/04/21/sql-server-when-are-statistics-updated-what-triggers-statistics-to-update/" target="_blank"><strong>SQL SERVER – When are Statistics Updated – What triggers Statistics to Update</strong></a>, for the specific conditions when Statistics is updated.</p>
<p style="text-align:justify;">Let us see one example where we could observe how Statistics is created automatically.</p>
<p><code style="font-size:12px;"><span style="color:green;">/*<br />
In this example we will see effect of unused index on updating database<br />
We will create unused indexes on table and see the performance degradation for insert<br />
*/<br />
</span><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">ALTER DATABASE </span><span style="color:black;">AdventureWorks<br />
</span><span style="color:blue;">SET </span><span style="color:black;">AUTO_CREATE_STATISTICS </span><span style="color:blue;">ON</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Create Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">StatsTable </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">,<br />
</span><span style="color:black;">FirstName </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">),<br />
</span><span style="color:black;">LastName </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">),<br />
</span><span style="color:black;">City </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert One Hundred Thousand Records<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">StatsTable </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">,</span><span style="color:black;">FirstName</span><span style="color:gray;">,</span><span style="color:black;">LastName</span><span style="color:gray;">,</span><span style="color:black;">City</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">100000 ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">) </span><span style="color:black;">RowID</span><span style="color:gray;">,<br />
</span><span style="color:red;">'Bob'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">)%</span><span style="color:black;">2 </span><span style="color:blue;">= </span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'Smith'<br />
</span><span style="color:blue;">ELSE </span><span style="color:red;">'Brown' </span><span style="color:blue;">END</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">)%</span><span style="color:black;">10 </span><span style="color:blue;">= </span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'New York'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">)%</span><span style="color:black;">10 </span><span style="color:blue;">= </span><span style="color:black;">5 </span><span style="color:blue;">THEN </span><span style="color:red;">'San Marino'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">)%</span><span style="color:black;">10 </span><span style="color:blue;">= </span><span style="color:black;">3 </span><span style="color:blue;">THEN </span><span style="color:red;">'Los Angeles'<br />
</span><span style="color:blue;">ELSE </span><span style="color:red;">'Houston' </span><span style="color:blue;">END<br />
FROM </span><span style="color:black;">sys.all_objects a<br />
</span><span style="color:gray;">CROSS </span><span style="color:blue;">JOIN </span><span style="color:black;">sys.all_objects b<br />
GO<br />
</span><span style="color:green;">/* Now Check the statistics on the Table<br />
As the table is just created there should not be any statistics on it<br />
and will display "This object does not have any statistics or indexes."<br />
*/<br />
</span><span style="color:darkred;">sp_helpstats </span><span style="color:red;">'StatsTable'</span><span style="color:gray;">, </span><span style="color:red;">'ALL'<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">From the example above, it is very clear that if the auto-update Statistics setting is enabled in the database, it will create the necessary Statistics based on the columns where certain conditions applied.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/statdef1.jpg" alt="" width="474" height="88" /></p>
<p><code style="font-size:12px;"><span style="color:green;">-- Run following few queries on the table<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StatsTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID </span><span style="color:blue;">= </span><span style="color:black;">110<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StatsTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">City </span><span style="color:blue;">= </span><span style="color:red;">'Houston'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Now Check the statistics on the Table again<br />
You will see two different statistics created on respective columns<br />
used in WHERE clause.<br />
*/<br />
</span><span style="color:darkred;">sp_helpstats </span><span style="color:red;">'StatsTable'</span><span style="color:gray;">, </span><span style="color:red;">'ALL'<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/statdef2.jpg" alt="" width="363" height="90" /></p>
<p><code style="font-size:12px;"><span style="color:green;">/* Now let us try with multiple Column in WHERE clause */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StatsTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID </span><span style="color:blue;">= </span><span style="color:black;">110 </span><span style="color:gray;">AND </span><span style="color:black;">City </span><span style="color:blue;">= </span><span style="color:red;">'Houston' </span><span style="color:gray;">AND </span><span style="color:black;">FirstName </span><span style="color:blue;">= </span><span style="color:red;">'Bob'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Now Check the statistics on the Table again<br />
You will see it will create statistics for the column<br />
used in WHERE clause; if it was not created earlier.<br />
*/<br />
</span><span style="color:darkred;">sp_helpstats </span><span style="color:red;">'StatsTable'</span><span style="color:gray;">, </span><span style="color:red;">'ALL'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Clean up Database<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">StatsTable<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/statdef3.jpg" alt="" width="355" height="92" /></p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9377/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9377/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9377/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9377/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9377/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9377/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9377/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9377/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9377/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9377/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9377/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9377/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9377/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9377/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9377&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/28/sql-server-default-statistics-on-column-automatic-statistics-on-column/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/statdef1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/statdef2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/statdef3.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Merge Operations &#8211; Insert, Update, Delete in Single Execution</title>
		<link>http://blog.sqlauthority.com/2010/06/08/sql-server-merge-operations-insert-update-delete-in-single-execution/</link>
		<comments>http://blog.sqlauthority.com/2010/06/08/sql-server-merge-operations-insert-update-delete-in-single-execution/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 01:30:41 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Merge]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9195</guid>
		<description><![CDATA[This blog post is written in response to T-SQL Tuesday hosted by Jorge Segarra (aka SQLChicken). I have been very active using these Merge operations in my development. However, I have found out from my consultancy work and friends that these amazing operations are not utilized by them most of the time. Here is my [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9195&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">This blog post is written in response to <a href="http://sqlchicken.com/2010/06/t-sql-tuesday-007-summertime-in-the-sql/" target="_blank">T-SQL Tuesday</a> hosted by <strong>Jorge Segarra</strong> (aka <a href="http://twitter.com/sqlchicken" target="_blank">SQLChicken</a>).</p>
<p style="text-align:justify;"><a href="http://sqlchicken.com/2010/06/t-sql-tuesday-007-summertime-in-the-sql/"><img class="alignleft" style="margin:3px;" src="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" alt="" width="120" height="120" /></a></p>
<p style="text-align:justify;">I have been very active using these Merge operations in my development. However, I have found out from my consultancy work and friends that these amazing operations are not utilized by them most of the time. Here is my attempt to bring the necessity of using the Merge Operation to surface one more time.</p>
<p style="text-align:justify;">MERGE is a new feature that provides an efficient way to do multiple DML operations. In earlier versions of SQL Server, we had to write separate statements to INSERT, UPDATE, or DELETE data based on certain conditions; however, at present, by using the MERGE statement, we can include the logic of such data changes in one statement that even checks when the data is matched and then just update it, and similarly, when the data is unmatched, it is inserted.</p>
<p style="text-align:justify;">One of the most important advantages of MERGE statement is that the entire data are read and processed only once. In earlier versions, three different statements had to be written to process three different activities (INSERT, UPDATE or DELETE); however, by using MERGE statement, all the update activities can be done in <strong>one pass of database table</strong>.</p>
<p style="text-align:justify;">I have written about these Merge Operations earlier in my blog post over here SQL SERVER – 2008 – Introduction to Merge Statement – One Statement for INSERT, UPDATE, DELETE. I was asked by one of the readers that <strong>how do we know that this operator was doing everything in single pass and was not calling this Merge Operator multiple times</strong>.</p>
<p style="text-align:justify;">Let us run the same example which I have used earlier; I am listing the same here again for convenience.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">--Let’s create Student Details and StudentTotalMarks and inserted some records.<br />
</span><span style="color:blue;">USE </span><span style="color:black;">tempdb<br />
GO<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">StudentDetails<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">StudentID INTEGER </span><span style="color:blue;">PRIMARY KEY</span><span style="color:gray;">,<br />
</span><span style="color:black;">StudentName </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">15</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">StudentDetails<br />
</span><span style="color:blue;">VALUES</span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:red;">'SMITH'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">StudentDetails<br />
</span><span style="color:blue;">VALUES</span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:red;">'ALLEN'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">StudentDetails<br />
</span><span style="color:blue;">VALUES</span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:red;">'JONES'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">StudentDetails<br />
</span><span style="color:blue;">VALUES</span><span style="color:gray;">(</span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:red;">'MARTIN'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">StudentDetails<br />
</span><span style="color:blue;">VALUES</span><span style="color:gray;">(</span><span style="color:black;">5</span><span style="color:gray;">,</span><span style="color:red;">'JAMES'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">StudentTotalMarks<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">StudentID INTEGER </span><span style="color:blue;">REFERENCES </span><span style="color:black;">StudentDetails</span><span style="color:gray;">,<br />
</span><span style="color:black;">StudentMarks INTEGER<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">StudentTotalMarks<br />
</span><span style="color:blue;">VALUES</span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:black;">230</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">StudentTotalMarks<br />
</span><span style="color:blue;">VALUES</span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">255</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">StudentTotalMarks<br />
</span><span style="color:blue;">VALUES</span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:black;">200</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Select from Table<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StudentDetails<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StudentTotalMarks<br />
GO<br />
</span><span style="color:green;">-- Merge Statement<br />
</span><span style="color:black;">MERGE StudentTotalMarks </span><span style="color:blue;">AS </span><span style="color:black;">stm<br />
USING </span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:black;">StudentID</span><span style="color:gray;">,</span><span style="color:black;">StudentName </span><span style="color:blue;">FROM </span><span style="color:black;">StudentDetails</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">sd<br />
</span><span style="color:blue;">ON </span><span style="color:black;">stm.StudentID </span><span style="color:blue;">= </span><span style="color:black;">sd.StudentID<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">MATCHED </span><span style="color:gray;">AND </span><span style="color:black;">stm.StudentMarks </span><span style="color:gray;">&gt; </span><span style="color:black;">250 </span><span style="color:blue;">THEN DELETE<br />
WHEN </span><span style="color:black;">MATCHED </span><span style="color:blue;">THEN UPDATE SET </span><span style="color:black;">stm.StudentMarks </span><span style="color:blue;">= </span><span style="color:black;">stm.StudentMarks </span><span style="color:gray;">+ </span><span style="color:black;">25<br />
</span><span style="color:blue;">WHEN </span><span style="color:gray;">NOT </span><span style="color:black;">MATCHED </span><span style="color:blue;">THEN<br />
INSERT</span><span style="color:gray;">(</span><span style="color:black;">StudentID</span><span style="color:gray;">,</span><span style="color:black;">StudentMarks</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES</span><span style="color:gray;">(</span><span style="color:black;">sd.StudentID</span><span style="color:gray;">,</span><span style="color:black;">25</span><span style="color:gray;">);<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Select from Table<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StudentDetails<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StudentTotalMarks<br />
GO<br />
</span><span style="color:green;">-- Clean up<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">StudentDetails<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">StudentTotalMarks<br />
GO</span></code></p>
<p style="text-align:justify;">The Merge Join performs very well and the following result is obtained.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/MergeStatement.gif" alt="" width="500" height="289" /></p>
<p style="text-align:justify;">Let us check the execution plan for the merge operator. You can click on following image to enlarge it.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/merge1.jpg" alt="" width="500" height="195" /></p>
<p style="text-align:justify;">Let us evaluate the execution plan for the <strong>Table Merge</strong> Operator only.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/merge2.jpg" alt="" width="319" height="435" /></p>
<p style="text-align:justify;">We can clearly see that the Number of Executions property suggests value 1. Which is quite clear that in a single PASS, the Merge Operation completes the operations of Insert, Update and Delete.</p>
<p style="text-align:justify;">I strongly suggest you all to use this operation, if possible, in your development. I have seen this operation implemented in many data warehousing applications.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/merge/'>Merge</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9195/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9195&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/08/sql-server-merge-operations-insert-update-delete-in-single-execution/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/MergeStatement.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/merge1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/merge2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Subquery or Join &#8211; Various Options &#8211; SQL Server Engine Knows the Best &#8211; Part 2</title>
		<link>http://blog.sqlauthority.com/2010/06/07/sql-server-subquery-or-join-various-options-sql-server-engine-knows-the-best-part-2/</link>
		<comments>http://blog.sqlauthority.com/2010/06/07/sql-server-subquery-or-join-various-options-sql-server-engine-knows-the-best-part-2/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 01:30:05 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9190</guid>
		<description><![CDATA[This blog post is part 2 of the earlier written article SQL SERVER – Subquery or Join – Various Options – SQL Server Engine knows the Best by Paulo R. Pereira. Paulo has left excellent comment to earlier article once again proving the point that SQL Server Engine is smart enough to figure out the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9190&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">This blog post is part 2 of the earlier written article <strong><a href="http://blog.sqlauthority.com/2010/06/06/sql-server-subquery-or-join-various-options-sql-server-engine-knows-the-best/" target="_blank">SQL SERVER – Subquery or Join – Various Options – SQL Server Engine knows the Best</a></strong> by <cite></cite><strong><a rel="external nofollow" href="http://sqlfromhell.wordpress.com/">Paulo R. Pereira</a></strong>.</p>
<p style="text-align:justify;">Paulo has left excellent comment to earlier article once again proving the point that SQL Server Engine is smart enough to figure out the best plan itself and uses the same for the query. Let us go over his <strong><a href="http://blog.sqlauthority.com/2010/06/06/sql-server-subquery-or-join-various-options-sql-server-engine-knows-the-best/#comment-74916" target="_blank">comment </a></strong>as he has posted.</p>
<p style="text-align:justify;">&#8220;I think IN or EXISTS is the best choice, because there is a little difference between ‘Merge Join’ of query with JOIN (Inner Join) and the others options (Left Semi Join), and JOIN can give more results than IN or EXISTS if the relationship is 1:0..N and not 1:0..1.</p>
<p>And if I try use NOT IN and NOT EXISTS the query plan is different from LEFT JOIN too (Left Anti Semi Join vs. Left Outer Join + Filter)&#8221;</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">-- use of SOME<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee E<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">E.EmployeeID </span><span style="color:blue;">= </span><span style="color:gray;">SOME (<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">EA.EmployeeID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeAddress EA<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">EA.EmployeeID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory EA<br />
</span><span style="color:gray;">)<br />
</span><span style="color:green;">-- use of IN<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee E<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">E.EmployeeID </span><span style="color:blue;">IN </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">EA.EmployeeID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeAddress EA<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">EA.EmployeeID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory EA<br />
</span><span style="color:gray;">)<br />
</span><span style="color:green;">-- use of EXISTS<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee E<br />
</span><span style="color:blue;">WHERE </span><span style="color:gray;">EXISTS (<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">EA.EmployeeID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeAddress EA<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">EA.EmployeeID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory EA<br />
</span><span style="color:gray;">)<br />
</span></code></p>
<p style="text-align:justify;">When looked into execution plan of the queries listed above indeed we do get different plans for queries and SQL Server Engines creates the best (least cost) plan for each query. Click on image to see larger images.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/inexists2.jpg" alt="" width="500" height="485" /></p>
<p style="text-align:justify;">Thanks Paulo for your wonderful contribution.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/readers-contribution/'>Readers Contribution</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9190/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9190&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/07/sql-server-subquery-or-join-various-options-sql-server-engine-knows-the-best-part-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/inexists2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Subquery or Join &#8211; Various Options &#8211; SQL Server Engine knows the Best</title>
		<link>http://blog.sqlauthority.com/2010/06/06/sql-server-subquery-or-join-various-options-sql-server-engine-knows-the-best/</link>
		<comments>http://blog.sqlauthority.com/2010/06/06/sql-server-subquery-or-join-various-options-sql-server-engine-knows-the-best/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 01:30:41 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9186</guid>
		<description><![CDATA[This is followup post of my earlier article SQL SERVER – Convert IN to EXISTS – Performance Talk, after reading all the comments I have received I felt that I could write more on the same subject to clear few things out. First let us run following four queries, all of them are giving exactly [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9186&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">This is followup post of my earlier article <a href="http://blog.sqlauthority.com/2010/06/05/sql-server-convert-in-to-exists-performance-talk/" target="_blank">SQL SERVER – Convert IN to EXISTS – Performance Talk</a>, after reading all the comments I have received I felt that I could write more on the same subject to clear few things out.</p>
<p style="text-align:justify;">First let us run following four queries, all of them are giving exactly same resultset.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">-- use of =<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee E<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">E.EmployeeID </span><span style="color:blue;">= </span><span style="color:gray;">( </span><span style="color:blue;">SELECT </span><span style="color:black;">EA.EmployeeID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeAddress EA<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">EA.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">E.EmployeeID</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- use of in<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee E<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">E.EmployeeID </span><span style="color:blue;">IN </span><span style="color:gray;">( </span><span style="color:blue;">SELECT </span><span style="color:black;">EA.EmployeeID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeAddress EA<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">EA.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">E.EmployeeID</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- use of exists<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee E<br />
</span><span style="color:blue;">WHERE </span><span style="color:gray;">EXISTS ( </span><span style="color:blue;">SELECT </span><span style="color:black;">EA.EmployeeID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeAddress EA<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">EA.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">E.EmployeeID</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Use of Join<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee E<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">HumanResources.EmployeeAddress EA </span><span style="color:blue;">ON </span><span style="color:black;">E.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">EA.EmployeeID<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">Let us compare the execution plan of the queries listed above. Click on image to see larger image.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/inexists1.jpg" alt="" width="500" height="350" /></p>
<p style="text-align:justify;">It is quite clear from the execution plan that in case of IN, EXISTS and JOIN SQL Server Engines is smart enough to figure out what is the best optimal plan of Merge Join for the same query and execute the same. However, in the case of use of Equal (=) Operator, SQL Server is forced to use Nested Loop and test each result of the inner query and compare to outer query, leading to cut the performance. Please note that here I no mean suggesting that Nested Loop is bad or Merge Join is better. This can very well vary on your machine and amount of resources available on your computer.</p>
<p style="text-align:justify;">When I see Equal (=) operator used in query like above, I usually recommend to see if user can use IN or EXISTS or JOIN. As I said, this can very much vary on different system. <em><strong>What is your take in above query?</strong></em> I believe SQL Server Engines is usually pretty smart to figure out what is ideal execution plan and use it.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9186/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9186&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/06/sql-server-subquery-or-join-various-options-sql-server-engine-knows-the-best/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/inexists1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 Star Join Query Optimization</title>
		<link>http://blog.sqlauthority.com/2009/12/04/sql-server-2008-star-join-query-optimization/</link>
		<comments>http://blog.sqlauthority.com/2009/12/04/sql-server-2008-star-join-query-optimization/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 01:30:01 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Business Intelligence]]></category>
		<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7470</guid>
		<description><![CDATA[Business Intelligence (BI) plays a significant role in businesses nowadays. Moreover, the databases that deal with the queries related to BI are presently facing an increase in workload. At present, when queries are sent to very large databases, millions of rows are returned. Also the users have to go through extended query response times when [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=7470&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Business Intelligence (BI) plays a significant role in businesses nowadays. Moreover, the databases that deal with the queries related to BI are presently facing an increase in workload. At present, when queries are sent to very large databases, millions of rows are returned. Also the users have to go through extended query response times when joining multiple tables are involved with such queries. ‘Star Join Query Optimization’ is a new feature of SQL Server 2008 Enterprise Edition. This mechanism uses bitmap filtering for improving the performance of some types of queries by the effective retrieval of rows from fact tables.</p>
<p style="text-align:justify;"><strong>Improved Query Response Times</strong><br />
In general, data warehouses employ dimensionally modeled star or snowflake schemas. These schemas have one or more than one fact tables that contain transactional data and many dimension tables, which holds information such as product data, customer information, and times and dates – all these define the fact table data. Usually, foreign keys are employed for maintaining relationships between the rows in fact tables and also between the rows in the dimension tables. Databases that contain star schemas are recognized by SQL Server 2008 Enterprise. It uses the new Star Join Query logic for processing queries against such star schemas more efficiently. Typically, on an average, data warehouse queries run faster to approximately 20 percent.</p>
<p style="text-align:justify;"><strong>Automatically Implemented</strong><br />
Star Join Query Optimization is automatically implemented by the SQL Server. It does not require a special database or application configuration. The query processor will usually optimize queries with medium selectivity (this refers to the queries that retrieve approximately 10% to 75% of rows from a fact table). Such queries are usually handled using hash joins to join the dimension and fact tables by employing the foreign keys to identify the matching rows. A hash table is built for each dimension table referenced in the query in the case of hash joins; the optimization process uses these hash tables for deriving bitmap filters. The key values from each dimension table are identified by bitmap filters; these key values qualify for inclusion in the query. When the fact table is scanned, the bitmap filters are applied to it. These bitmap filters eliminate those rows of the fact table which are not qualified for inclusion in the result set. The most selective bitmap filter is applied first as it is found to eliminate the highest number of rows. Since the eliminated rows do not need further processing, the subsequent filters need not be applied to them – this way the process becomes more efficient.</p>
<p style="text-align:justify;"><strong>Query Selectivity</strong><br />
The performance is enhanced in the case of medium selectivity queries while using bitmap filtering because the rows are filtered before any joins are implemented. Hence, there is a decrease in the number of rows that are processed by each join. Bitmap filtering is not applied when queries are highly selective (i.e., those queries that return less than 10% of the rows in a fact table). In such case, a nested loop join is found to be generally more efficient. Similarly, when the queries are not very selective at all (queries which return more than 75% of the rows in a fact table), bitmap filtering is not applied as there are very few rows to be filtered, and hence, there is no requirement of enhancement in performance in this case.</p>
<p style="text-align:justify;"><strong>Integer Data Types</strong><br />
Star join optimization is found to give the highest efficiency when the data type of the columns used in the joins is integer. This feature enables the bitmap filter to be applied as part of the initial table or index scan rather than being used at a later stage in the query plan. Most of the queries are benefited from star join optimization since foreign key relationships are commonly implemented using integer-based alternate key columns.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<p style="text-align:justify;">
<br />Posted in Best Practices, Business Intelligence, Data Warehousing, Pinal Dave, SQL, SQL Authority, SQL Joins, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/7470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/7470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/7470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/7470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7470/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=7470&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/12/04/sql-server-2008-star-join-query-optimization/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Understanding Table Hints with Examples</title>
		<link>http://blog.sqlauthority.com/2009/11/19/sql-server-understanding-table-hints-with-examples/</link>
		<comments>http://blog.sqlauthority.com/2009/11/19/sql-server-understanding-table-hints-with-examples/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 01:30:41 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7113</guid>
		<description><![CDATA[Introduction Today we have a very interesting subject to look at. I tried to look for help online but have not found any other documentation besides what we have from the Book Online. Let us try to understand what are the different kinds of hints available in SQL Server and how they are helpful. What [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=7113&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3 style="text-align:justify;">Introduction</h3>
<p style="text-align:justify;">Today we have a very interesting subject to look at. I tried to look for help online but have not found any other documentation besides what we have from the Book Online.</p>
<p style="text-align:justify;">Let us try to understand what are the different kinds of hints available in SQL Server and how they are helpful.</p>
<h3 style="text-align:justify;">What is a Hint?</h3>
<p style="text-align:justify;"><em>Hints </em> are options and strong suggestions specified for enforcement by the SQL Server query processor on DML statements. The hints override any execution plan the query optimizer might select for a query.</p>
<p style="text-align:justify;">Before we continue to explore this subject, we need to consider one very important fact and say some words of caution. SQL Server Query optimizer is a very smart tool and it makes a best selection of execution plan. Suggesting hints to the Query Optimizer should be attempted when absolutely necessary and by experienced developers who know exactly what they are doing (or in development as a way to experiment and learn).</p>
<p style="text-align:justify;">There are three different kinds of hints. Let us understand the basics of each of them separately.</p>
<h4 style="text-align:justify;">Join Hint</h4>
<p style="text-align:justify;">This hint is used when more than one table is used in a query. Two or more tables can be joined using different kinds of joins. This hint forces the type of join algorithm that is used. Joins can be used in <code>SELECT</code>, <code>UPDATE </code> and <code>DELETE </code> statements.</p>
<h4 style="text-align:justify;">Query Hint</h4>
<p style="text-align:justify;">This hint is used when certain kind of logic has to be applied to a whole query. Any hint used in the query is applied to the complete query, as opposed to part of it. There is no way to specify that only a certain part of a query should be used with the hint. After any query, the <code>OPTION </code> clause is specified to apply the logic to this query. A query always has any of the following statements: <code>SELECT</code>, <code>UPDATE</code>, <code>DELETE</code>, <code>INSERT </code> or <code>MERGE </code> (SQL 2K8); and this hint can be applied to all of them.</p>
<h4 style="text-align:justify;">Table Hint</h4>
<p style="text-align:justify;">This hint is used when certain kind of locking mechanism of tables has to be controlled. SQL Server query optimizer always puts the appropriate kind of lock on tables, when any of the Transact SQL operations <code>SELECT</code>, <code>UPDATE</code>, <code>DELETE</code>, <code>INSERT </code> or <code>MERGE </code> are used. There are certain cases when the developer knows when and where to override the default behavior of the locking algorithm and these hints are useful in those scenarios.</p>
<p style="text-align:justify;">Let us run the following simple query with different kinds of query hints and observe the actual execution plan. The analysis of execution plan is not part of this article and will be covered in future.</p>
<div id="highlighter_563028" style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">/* No Query Hint */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product </span><span style="color:blue;">AS </span><span style="color:black;">p<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">sod </span><span style="color:blue;">ON </span><span style="color:black;">p.ProductID </span><span style="color:blue;">= </span><span style="color:black;">sod.ProductID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Weight </span><span style="color:blue;">= </span><span style="color:black;">20.77<br />
GO<br />
</span><span style="color:green;">/* Merge Join Query Hint */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product </span><span style="color:blue;">AS </span><span style="color:black;">p<br />
</span><span style="color:blue;">INNER </span><span style="color:black;">MERGE </span><span style="color:blue;">JOIN </span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">sod </span><span style="color:blue;">ON </span><span style="color:black;">p.ProductID </span><span style="color:blue;">= </span><span style="color:black;">sod.ProductID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Weight </span><span style="color:blue;">= </span><span style="color:black;">20.77<br />
GO<br />
</span><span style="color:green;">/* Hash Join Query Hint */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product </span><span style="color:blue;">AS </span><span style="color:black;">p<br />
</span><span style="color:blue;">INNER </span><span style="color:black;">HASH </span><span style="color:blue;">JOIN </span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">sod </span><span style="color:blue;">ON </span><span style="color:black;">p.ProductID </span><span style="color:blue;">= </span><span style="color:black;">sod.ProductID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Weight </span><span style="color:blue;">= </span><span style="color:black;">20.77<br />
GO<br />
</span><span style="color:green;">/* Loop Join Query Hint */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product </span><span style="color:blue;">AS </span><span style="color:black;">p<br />
</span><span style="color:blue;">INNER </span><span style="color:black;">LOOP </span><span style="color:blue;">JOIN </span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">sod </span><span style="color:blue;">ON </span><span style="color:black;">p.ProductID </span><span style="color:blue;">= </span><span style="color:black;">sod.ProductID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Weight </span><span style="color:blue;">= </span><span style="color:black;">20.77<br />
GO<br />
</span><span style="color:green;">/* Remote Join Query Hint */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product </span><span style="color:blue;">AS </span><span style="color:black;">p<br />
</span><span style="color:blue;">INNER </span><span style="color:black;">REMOTE </span><span style="color:blue;">JOIN </span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">sod </span><span style="color:blue;">ON </span><span style="color:black;">p.ProductID </span><span style="color:blue;">= </span><span style="color:black;">sod.ProductID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Weight </span><span style="color:blue;">= </span><span style="color:black;">20.77<br />
GO </span></code></div>
<p style="text-align:justify;">The above query will produce the following query execution plan.</p>
<p style="text-align:justify;"><img src="http://www.pinaldave.com/bimg/sql-join.png" alt="" width="550" height="557" /></p>
<p style="text-align:justify;">In this article we will focus mainly on Join Hints. We will discuss other kinds of hints and their usage in a different article. There are a total of four kinds of join hints available.</p>
<h3 style="text-align:justify;">Loop Join</h3>
<p style="text-align:justify;">This join is also commonly known as <em>nested iteration</em>. This kind of join is composed by an outer loop and an inner loop. When the query runs for each row of the outer loop, the inner loop is executed completely. This join is effective only when the outer loop query is small and the inner loop query has all the proper optimizations applied. This join method is very useful with small transactions.</p>
<h3 style="text-align:justify;">Merge Join</h3>
<p style="text-align:justify;">This join has the unique requirement for tables involved in the operation to be sorted. This join keeps both of the tables sorted in parallel and compares each table row by row simultaneously with each other. It compares one row of the first table with one row of the second table. If they are equal, that row qualifies; otherwise, this join determines which row of each table has the lower value. Once the lowest value of the table is figured out, it moves on to next row of that table and compares that to the original row. This operation keeps going on until all rows from each table are completely examined. This operation can be very expensive when tables are not sorted and it’s required to sort them before they are joined. If tables have non clustered indexes over them and joins are using the same conditions, there are pretty good chances that this join performs better than other kinds of joins.</p>
<h3 style="text-align:justify;">Hash Join</h3>
<p style="text-align:justify;">This is the most complex of all the other joins. There are two major components of this kind of join &#8211; build query and probe query. First, a smaller table is assigned as build query and a hash table for the same is created. This hash table is compared with the probe table. This comparison of input table and probe table is done one row at a time. One row of the probe table is hashed and compared against the other row, and qualifying rows are checked.</p>
<p style="text-align:justify;">There are three different kinds of hash joins:  in-memory hash join, grace hash join, and recursive hash join. Let us look in more detail at these three kinds of joins.</p>
<h4 style="text-align:justify;">In-Memory Hash Join</h4>
<p style="text-align:justify;">This kind of hash join is used when tables are very small and a complete table can be hashed and loaded in memory.</p>
<h4 style="text-align:justify;">Grace Hash Join</h4>
<p style="text-align:justify;">This kind of hash join is used when tables are comparatively large and don’t fit in memory. Tables will be partitioned at input levels and processed in steps.</p>
<h4 style="text-align:justify;">Recursive Hash Join</h4>
<p style="text-align:justify;">This kind of join is used for complex tables and for tables which are very large and require multilevel of table partitions.</p>
<p style="text-align:justify;">SQL Server query optimizer is a smart tool and it knows when to use the right kind of join. When it comes to hash join, the query optimizer starts conservatively with in-memory hash join. If join is larger than in-memory it moves up to Grace hash join or Recursive hash join. Sometimes the optimizer makes a mistake in identifying a smaller table and it reverses the role of the build and probe table: this is called <em>role reversal</em>.</p>
<h4 style="text-align:justify;">Remote Join</h4>
<p style="text-align:justify;">This is least-used tabled join ever. There is no example for it given in Book On Line. Every Join has two tables associated with it: Left Table and Right Table. The join usually happens on the Left Table. When Remote close is used, joins are performed on the site of Right Join. This join can only be performed on INNER JOIN as in case of OUTER join there may be NULL values on the right table which makes concept of joining on right table logically incorrect.</p>
<h3 style="text-align:justify;">Summary</h3>
<p style="text-align:justify;">We have closely observed different kinds of Join Hints in this article. We will cover the remaining concepts in another article.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>), </strong><a href="http://dotnetslackers.com/articles/sql/SQL-Server-Understanding-Table-Hints-with-examples.aspx" target="_blank">DNS</a></p>
<p style="text-align:justify;">
<br />Posted in Pinal Dave, SQL, SQL Authority, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/7113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/7113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/7113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/7113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7113/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=7113&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/11/19/sql-server-understanding-table-hints-with-examples/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/sql-join.png" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Interesting Observation &#8211; Query Hint &#8211; FORCE ORDER</title>
		<link>http://blog.sqlauthority.com/2009/10/06/sql-server-interesting-observation-query-hint-force-order/</link>
		<comments>http://blog.sqlauthority.com/2009/10/06/sql-server-interesting-observation-query-hint-force-order/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 01:30:17 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6916</guid>
		<description><![CDATA[SQL Server never stops to amaze me. As regular readers of this blog already know that besides conducting corporate training, I work on large-scale projects on query optimizations and server tuning projects. In one of the recent projects, I have noticed that a Junior Database Developer used the query hint Force Order; when I asked [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6916&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server never stops to amaze me. As regular readers of this blog already know that besides conducting corporate training, I work on large-scale projects on query optimizations and server tuning projects. In one of the recent projects, I have noticed that a Junior Database Developer used the query hint Force Order; when I asked for details, I found out that the basic concept was not properly understood by him.</p>
<p style="text-align:justify;">Today, let us try to understand its working and the effect of this hint. Further, we will see the extent of difference in performance created by this one query hint. I also have one interesting question for all of you as well; I will give the answer in one of my later posts.</p>
<p style="text-align:left;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:black;">CountEmployee </span><span style="color:green;">-- 290 Rows<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:black;">CountEmployeeAddress </span><span style="color:green;">-- 290 Rows<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeAddress<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:black;">CountEmployeeDepartmentHistory </span><span style="color:green;">-- 296 Rows<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee e<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">HumanResources.EmployeeAddress ea </span><span style="color:blue;">ON </span><span style="color:black;">ea.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">e.EmployeeID<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory edh </span><span style="color:blue;">ON </span><span style="color:black;">edh.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">e.EmployeeID<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee e<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">HumanResources.EmployeeAddress ea </span><span style="color:blue;">ON </span><span style="color:black;">ea.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">e.EmployeeID<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory edh </span><span style="color:blue;">ON </span><span style="color:black;">edh.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">e.EmployeeID<br />
</span><span style="color:blue;">OPTION </span><span style="color:gray;">(</span><span style="color:black;">FORCE </span><span style="color:blue;">ORDER</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">In above example, I have used three tables and their row count is listed as well. Employee and EmployeeAddress – both have same number rows, and EmployeeDepartmentHistory table has around 6 rows more than the other two tables. Now let us run the query without using OPTION (FORCE ORDER) and run it along with the query hint and check the execution plan. You will find a difference in the query cost.</p>
<p style="text-align:justify;">We all accept that the table with least number of rows should be listed as the base table, and the same is done here. We have two such tables with the least number of rows, which are listed as base tables. Now before we further explain this, let us see the execution plan for the same.</p>
<p style="text-align:justify;">
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/forceorder.jpg" alt="" width="500" height="404" /></p>
<p style="text-align:justify;">It is very clear from the above execution plan that when I order is forced the query cost goes high. This proves that the SQL Server has already made a good decision with regard to the optimized query plan. When plan is forced in the case of the joins more than 2 table the performance matters. Let us see the execution order of the table in both the cases.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/forceorder1.jpg" alt="" width="500" height="618" /></p>
<p style="text-align:justify;">Now, from the example, it is very clear when you force order the query, it evaluates the table Employee first and when it is not forced, it evaluates EmployeeAddress first. Even though both the tables have same number of rows, the query optimizer processes them differently and uses different types of join logic. When the order is not forced, it uses hash join; further, in case of forced order, it uses nested loop – this creates a significant difference in the query cost.</p>
<p style="text-align:justify;">The conclusion of this whole exercise is very simple.</p>
<ul style="text-align:justify;">
<li>SQL      Server Query Execution Engine is pretty smart to decide the best execution      plan with least query cost for any query.</li>
<li>Order      of the tables in any query can make a significant impact on the query.</li>
</ul>
<p style="text-align:justify;">Now the question for you: We have seen that using query hint of OPTION (FORCE ORDER) reduces the performance; give an example wherein we can use this hint to improve the performance?</p>
<p style="text-align:justify;">Please leave your comment here. I will publish the answer to this question with due credit and with my own example in a later post.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<br />Posted in Best Practices, Pinal Dave, SQL, SQL Authority, SQL Index, SQL Joins, SQL Optimization, SQL Performance, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/6916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/6916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6916/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6916&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/06/sql-server-interesting-observation-query-hint-force-order/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/forceorder.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/forceorder1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Outer Join in Indexed View &#8211; Question to Readers</title>
		<link>http://blog.sqlauthority.com/2009/09/25/sql-server-outer-join-in-indexed-view-question-to-readers/</link>
		<comments>http://blog.sqlauthority.com/2009/09/25/sql-server-outer-join-in-indexed-view-question-to-readers/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 01:30:46 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL View]]></category>
		<category><![CDATA[SQL White Papers]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6910</guid>
		<description><![CDATA[Today I have question for you. Just a day ago I was reading whitepaper Improving Performance with SQL Server 2008 Indexed Views. Following is question and answer I read in the white paper. Q. Why can&#8217;t I use OUTER JOIN in an indexed view? A. Rows can logically disappear from an indexed view based on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6910&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Today I have question for you. Just a day ago I was reading whitepaper <a href="http://msdn.microsoft.com/en-us/library/dd171921.aspx" target="_blank">Improving Performance with SQL Server 2008 Indexed Views</a>. Following is question and answer I read in the white paper.</p>
<p style="padding-left:30px;text-align:justify;"><em><strong>Q. Why can&#8217;t I use OUTER JOIN in an indexed view?</strong></em></p>
<p style="padding-left:30px;text-align:justify;"><em>A. Rows can logically disappear from an indexed view based on OUTER JOIN when you insert data into a base table. This makes incrementally updating OUTER JOIN views relatively complex to implement, and the performance of the implementation would be slower than for views based on standard (INNER) JOIN.</em></p>
<p style="text-align:justify;">Here I would like to ask you one question, do you have example for kind of OUTER JOIN where you insert data into base table, it will make a row disappear from query?</p>
<p style="text-align:justify;">Please post your answer as comment.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Posted in Pinal Dave, SQL, SQL Authority, SQL Index, SQL Joins, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, SQL View, SQL White Papers, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6910/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6910/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6910/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6910/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/6910/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/6910/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/6910/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/6910/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6910/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6910/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6910/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6910/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6910/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6910/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6910&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/25/sql-server-outer-join-in-indexed-view-question-to-readers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL Server &#8211; Multiple CTE in One SELECT Statement Query</title>
		<link>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/</link>
		<comments>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 01:30:47 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[CTE]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6399</guid>
		<description><![CDATA[I have previously written many articles on CTE. One question I get often is how to use multiple CTE in one query or multiple CTE in SELECT statement. Let us see quickly two examples for the same. I had done my best to take simplest examples in this subject. Option 1 : /* Method 1 */ ;WITH CTE1 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6399&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I have previously written many articles on CTE. One question I get often is how to use multiple CTE in one query or multiple CTE in SELECT statement. Let us see quickly two examples for the same. I had done my best to take simplest examples in this subject.</p>
<p style="text-align:justify;">Option 1 :</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Method 1 */<br />
</span><span style="color:gray;">;</span><span style="color:blue;">WITH </span><span style="color:black;">CTE1 </span><span style="color:blue;">AS </span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">Col1</span><span style="color:gray;">),<br />
</span><span style="color:black;">CTE2 </span><span style="color:blue;">AS </span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:black;">2 </span><span style="color:blue;">AS </span><span style="color:black;">Col2</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">CTE1.Col1</span><span style="color:gray;">,</span><span style="color:black;">CTE2.Col2<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">CTE1<br />
</span><span style="color:gray;">CROSS </span><span style="color:blue;">JOIN </span><span style="color:black;">CTE2<br />
GO</span></code>
</p>
<p style="text-align:justify;">Option 2:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Method 2 */<br />
</span><span style="color:gray;">;</span><span style="color:blue;">WITH </span><span style="color:black;">CTE1 </span><span style="color:blue;">AS </span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">Col1</span><span style="color:gray;">),<br />
</span><span style="color:black;">CTE2 </span><span style="color:blue;">AS </span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:black;">COL1</span><span style="color:gray;">+</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">Col2 </span><span style="color:blue;">FROM </span><span style="color:black;">CTE1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">CTE1.Col1</span><span style="color:gray;">,</span><span style="color:black;">CTE2.Col2<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">CTE1<br />
</span><span style="color:gray;">CROSS </span><span style="color:blue;">JOIN </span><span style="color:black;">CTE2<br />
GO</span></code>
</p>
<p style="text-align:justify;">Please note the difference between options. If there is any other option than above mentioned two options, please leave your comment here.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
<br />Posted in Pinal Dave, SQL, SQL Authority, SQL Joins, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: CTE <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/6399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/6399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/6399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/6399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6399/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6399&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL Server &#8211; Understanding Table Hints with Examples</title>
		<link>http://blog.sqlauthority.com/2009/06/20/sql-server-%e2%80%93understanding-table-hints-with-examples/</link>
		<comments>http://blog.sqlauthority.com/2009/06/20/sql-server-%e2%80%93understanding-table-hints-with-examples/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 01:30:44 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Query Hint]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5645</guid>
		<description><![CDATA[Today we have a very interesting subject to look at. I tried to look for help online but have not found any other documentation besides what we have from the Book Online. Let us try to understand what are the different kinds of hints available in SQL Server and how they are helpful. What is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=5645&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Today we have a very interesting subject to look at. I tried to look for help online but have not found any other documentation besides what we have from the Book Online.</p>
<p style="text-align:justify;">Let us try to understand what are the different kinds of hints available in SQL Server and how they are helpful.</p>
<p style="text-align:justify;"><strong>What is a Hint?</strong></p>
<p style="text-align:justify;"><em>Hints </em> are options and strong suggestions specified for enforcement by the SQL Server query processor on DML statements. The hints override any execution plan the query optimizer might select for a query.</p>
<p style="text-align:justify;">Before we continue to explore this subject, we need to consider one very important fact and say some words of caution. SQL Server Query optimizer is a very smart tool and it makes a best selection of execution plan. Suggesting hints to the Query Optimizer should be attempted when absolutely necessary and by experienced developers who know exactly what they are doing (or in development as a way to experiment and learn).</p>
<p style="text-align:justify;">There are three different kinds of hints. Let us understand the basics of each of them separately.</p>
<h3 style="text-align:justify;"><a href="http://dotnetslackers.com/articles/sql/SQL-Server-Understanding-Table-Hints-with-examples.aspx" target="_blank">Please continue reading article here.</a></h3>
<p>Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<br />Posted in Pinal Dave, SQL, SQL Authority, SQL Joins, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: Query Hint <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5645/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5645/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/5645/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/5645/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5645/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5645/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5645/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5645/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=5645&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/06/20/sql-server-%e2%80%93understanding-table-hints-with-examples/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to JOINs &#8211; Basic of JOINs</title>
		<link>http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/</link>
		<comments>http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 01:30:02 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL User Group]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4307</guid>
		<description><![CDATA[The launch of Gandhinagar SQL Server User Group was a tremendous, astonishing success! It was overwhelming to see a large gathering of enthusiasts looking up to me (I was the Key Speaker) eager to enhance their knowledge and participate in some brainstorming discussions. Some members of User Group had requested me to write a simple [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=4307&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">The launch of <strong><a href="http://blog.sqlauthority.com/2009/04/03/sqlauthority-news-launch-of-gandhinagar-sql-server-user-group/" target="_blank"><strong>Gandhinagar SQL Server User Group</strong></a></strong> was a tremendous, astonishing success! It was overwhelming to see a large gathering of enthusiasts looking up to me (I was the Key Speaker) eager to enhance their knowledge and participate in some brainstorming discussions. Some members of User Group had requested me to write a simple article on JOINS elucidating its different types.</p>
<p style="text-align:justify;"><strong><a href="http://www.pinaldave.com/sql-downloads/sql-downloads/script-downloads/sql-server-introduction-to-joins-basic-of-joins/">Download SQL Script</a></strong> used throughout in this article to practice along. Now, take a quick look at the following two tables I have created.</p>
<h3 style="text-align:justify;"><strong>INNER JOIN</strong></h3>
<p style="text-align:justify;">This join returns rows when there is at least one match in both the tables.<br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/March09UG/inner join.jpg" alt="" width="424" height="424" /></p>
<h3 style="text-align:justify;"><strong>OUTER JOIN</strong></h3>
<p style="text-align:justify;">There are three different Outer Join methods.</p>
<p style="text-align:justify;"><strong>LEFT OUTER JOIN</strong><br />
This join returns all the rows from the left table in conjunction with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values.<br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/March09UG/left join.jpg" alt="" width="424" height="424" /></p>
<p style="text-align:justify;"><strong>RIGHT OUTER JOIN</strong><br />
This join returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.<br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/March09UG/right join.jpg" alt="" width="424" height="424" /></p>
<p style="text-align:justify;"><strong>FULL OUTER JOIN</strong><br />
This join combines left outer join and right outer join. It returns row from either table when the conditions are met and returns null value when there is no match.<br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/March09UG/outer join.jpg" alt="" width="424" height="424" /></p>
<h3 style="text-align:justify;">CROSS JOIN</h3>
<p style="text-align:justify;">This join is a Cartesian join that does not necessitate any condition to join. The resultset contains records that are multiplication of record number from both the tables.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/March09UG/cross join - half.jpg" alt="" width="500" height="444" /></p>
<h3 style="text-align:justify;">Additional Notes related to JOIN:</h3>
<p style="text-align:justify;">The following are three classic examples to display where Outer Join is useful. You will notice several instances where developers write query as given below.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">t1.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">t1.ID </span><span style="color:gray;">NOT </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:black;">t2.ID </span><span style="color:blue;">FROM </span><span style="color:black;">Table2 t2</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">The query demonstrated above can be easily replaced by Outer Join. Indeed, replacing it by Outer Join is the best practice. The query that gives same result as above is displayed here using Outer Join and WHERE clause in join.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* LEFT JOIN - WHERE NULL */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">t1.</span><span style="color:gray;">*,</span><span style="color:black;">t2.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:magenta;">LEFT </span><span style="color:blue;">JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">t2.ID </span><span style="color:blue;">IS </span><span style="color:gray;">NULL</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/March09UG/left join null.jpg" alt="" width="424" height="424" /></p>
<p style="text-align:justify;">The above example can also be created using Right Outer Join.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/March09UG/right join null.jpg" alt="" width="424" height="424" /></p>
<p style="text-align:justify;"><strong>NOT INNER JOIN</strong><br />
Remember, the term Not Inner Join does not exist in database terminology. However, when full Outer Join is used along with WHERE condition, as explained in the above two examples, it will give you exclusive result to Inner Join. This join will give all the results that were not present in Inner Join.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/March09UG/outer join null.jpg" alt="" width="424" height="424" /></p>
<p style="text-align:justify;">You can download the complete <strong><a href="http://www.pinaldave.com/sql-downloads/sql-downloads/script-downloads/sql-server-introduction-to-joins-basic-of-joins/" target="_blank">SQL Script</a></strong> here, but for the sake of complicity I am including the same script here.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">table1<br />
</span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">Value </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">))<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">Table1 </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">Value</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:red;">'First'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:red;">'Second'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:red;">'Third'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:red;">'Fourth'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">5</span><span style="color:gray;">,</span><span style="color:red;">'Fifth'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">table2<br />
</span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">Value </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">))<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">Table2 </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">Value</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:red;">'First'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:red;">'Second'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:red;">'Third'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">6</span><span style="color:gray;">,</span><span style="color:red;">'Sixth'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">7</span><span style="color:gray;">,</span><span style="color:red;">'Seventh'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">8</span><span style="color:gray;">,</span><span style="color:red;">'Eighth'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table2<br />
GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">/* INNER JOIN */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">t1.</span><span style="color:gray;">*,</span><span style="color:black;">t2.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID<br />
GO<br />
</span><span style="color:green;">/* LEFT JOIN */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">t1.</span><span style="color:gray;">*,</span><span style="color:black;">t2.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:magenta;">LEFT </span><span style="color:blue;">JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID<br />
GO<br />
</span><span style="color:green;">/* RIGHT JOIN */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">t1.</span><span style="color:gray;">*,</span><span style="color:black;">t2.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:magenta;">RIGHT </span><span style="color:blue;">JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID<br />
GO<br />
</span><span style="color:green;">/* OUTER JOIN */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">t1.</span><span style="color:gray;">*,</span><span style="color:black;">t2.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:blue;">FULL </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID<br />
GO<br />
</span><span style="color:green;">/* LEFT JOIN - WHERE NULL */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">t1.</span><span style="color:gray;">*,</span><span style="color:black;">t2.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:magenta;">LEFT </span><span style="color:blue;">JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">t2.ID </span><span style="color:blue;">IS </span><span style="color:gray;">NULL<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* RIGHT JOIN - WHERE NULL */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">t1.</span><span style="color:gray;">*,</span><span style="color:black;">t2.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:magenta;">RIGHT </span><span style="color:blue;">JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">t1.ID </span><span style="color:blue;">IS </span><span style="color:gray;">NULL<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* OUTER JOIN - WHERE NULL */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">t1.</span><span style="color:gray;">*,</span><span style="color:black;">t2.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:blue;">FULL </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">t1.ID </span><span style="color:blue;">IS </span><span style="color:gray;">NULL OR </span><span style="color:black;">t2.ID </span><span style="color:blue;">IS </span><span style="color:gray;">NULL<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* CROSS JOIN */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">t1.</span><span style="color:gray;">*,</span><span style="color:black;">t2.</span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:gray;">CROSS </span><span style="color:blue;">JOIN </span><span style="color:black;">Table2 t2<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">table1<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">table2<br />
GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;">I hope this article fulfills its purpose. I would like to have feedback from my blog readers. Please suggest me where do you all want me to take this article next.<br />
</span></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Posted in Best Practices, Database, Pinal Dave, SQL, SQL Authority, SQL Joins, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL User Group, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/4307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/4307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/4307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/4307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/4307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/4307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/4307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/4307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/4307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/4307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/4307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/4307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/4307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/4307/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=4307&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/feed/</wfw:commentRss>
		<slash:comments>221</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/March09UG/innerjoin.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/March09UG/leftjoin.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/March09UG/rightjoin.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/March09UG/outerjoin.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/March09UG/crossjoin-half.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/March09UG/leftjoinnull.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/March09UG/rightjoinnull.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/March09UG/outerjoinnull.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Logical Query Processing Phases &#8211; Order of Statement Execution</title>
		<link>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/</link>
		<comments>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 01:30:20 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Logical Query Processing Phases]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4218</guid>
		<description><![CDATA[You can download the poster from Itzik Ben-Gan&#8217;s T-SQL Querying page over here. Of late, I penned down an article &#8211; SQL SERVER &#8211; Interesting Observation of ON Clause on LEFT JOIN &#8211; How ON Clause Effects Resultset in LEFT JOIN &#8211; which received a very intriguing comment from one of my regular blog readers [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=4218&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><strong></strong><strong>You can download the poster from Itzik Ben-Gan&#8217;s T-SQL Querying page over <a href="http://www.insidetsql.com/books/insidetsql2008" target="_blank">here</a>.<br />
</strong></p>
<p style="text-align:justify;">Of late, I penned down an article &#8211; <strong><a href="http://blog.sqlauthority.com/2009/03/15/sql-server-interesting-observation-of-on-clause-on-left-join-how-on-clause-effects-resultset-in-left-join/" target="_blank">SQL SERVER &#8211; Interesting Observation of ON Clause on LEFT JOIN &#8211; How ON Clause Effects Resultset in LEFT JOIN</a></strong> &#8211; which received a very intriguing <a href="http://blog.sqlauthority.com/2009/03/15/sql-server-interesting-observation-of-on-clause-on-left-join-how-on-clause-effects-resultset-in-left-join/#comment-49834" target="_blank">comment </a> from one of my regular blog readers <strong>Craig</strong>. According to him this phenomenon happens due to Logical Query Processing. His comment instigated a question in my mind. I have put forth this question to all my readers at the end of the article. Let me first give you an introduction to Logical Query Processing Phase.</p>
<p style="text-align:justify;">What actually sets SQL Server apart from other programming languages is the way SQL Server processes its code. Generally, most programming languages process statement from top to bottom. By contrast, SQL Server processes them in a unique order which is known as Logical Query Processing Phase. These phases generate a series of virtual tables with each virtual table feeding into the next phase (virtual tables not viewable). These phases and their orders are given as follows:</p>
<p style="text-align:justify;">1. FROM<br />
2. ON<br />
3. OUTER<br />
4. WHERE<br />
5. GROUP BY<br />
6. CUBE | ROLLUP<br />
7. HAVING<br />
8. SELECT<br />
9. DISTINCT<br />
10 ORDER BY<br />
11. TOP</p>
<p style="text-align:justify;">As OUTER join is applied subsequent to ON clause, all rows eliminated by the ON clause will still be included by the OUTER join as described in the article <strong><a href="http://blog.sqlauthority.com/2009/03/15/sql-server-interesting-observation-of-on-clause-on-left-join-how-on-clause-effects-resultset-in-left-join/" target="_blank">SQL SERVER &#8211; Interesting Observation of ON Clause on LEFT JOIN &#8211; How ON Clause Effects Resultset in LEFT JOIN</a>.</strong></p>
<p style="text-align:justify;">However, I am perplexed about the last two, ORDER BY and TOP. According to some people TOP comes first in logical query processing phase while others suggest that ORDER BY comes first. Now, here I’ve laid down my questions for you all to think about:</p>
<p style="text-align:justify;">1) What is the correct answer for order query processing phase &#8211; ORDER BY or TOP?<br />
2) How can we create an example to verify query processing phase for ORDER BY and TOP?</p>
<p style="text-align:justify;">I will soon publish the answers I receive to the above questions on this blog, with due credit given to my readers.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Posted in Database, Pinal Dave, Readers Contribution, Readers Question, Software Development, SQL, SQL Authority, SQL Joins, SQL Query, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology Tagged: Logical Query Processing Phases <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/4218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/4218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/4218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/4218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/4218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/4218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/4218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/4218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/4218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/4218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/4218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/4218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/4218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/4218/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=4218&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQLAuthority News &#8211; Announcement &#8211; Gandhinagar SQL Server User Group &#8211; March 27, 2009</title>
		<link>http://blog.sqlauthority.com/2009/03/25/sqlauthority-news-announcement-gandhinagar-sql-server-user-group-march-27-2009/</link>
		<comments>http://blog.sqlauthority.com/2009/03/25/sqlauthority-news-announcement-gandhinagar-sql-server-user-group-march-27-2009/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 01:30:48 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[MVP]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL User Group]]></category>
		<category><![CDATA[SQLAuthority Author Visit]]></category>
		<category><![CDATA[SQLAuthority News]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[DigiCorp]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4063</guid>
		<description><![CDATA[It is my pleasure to announce new SQL Server User Group &#8211; Gandhinagar SQL Server User Group. We will be meeting every 2nd and 4th Friday of the month. Here is the detail for this months meeting. We will be having one gift for best participant in the meeting. I request all the SQL enthusiast [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=4063&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">It is my pleasure to announce new SQL Server User Group &#8211; <strong>Gandhinagar SQL Server User Group</strong>. We will be meeting every 2nd and 4th Friday of the month. Here is the detail for this months meeting. We will be having one gift for best participant in the meeting. I request all the SQL enthusiast to attend this meeting and do not miss it. You can be member at sqlpass @ <strong><a href="http://gandhinagar.sqlpass.org/" target="_blank">http://gandhinagar.sqlpass.org/</a></strong>.</p>
<p style="text-align:justify;"><strong>Meeting Date Time:</strong><br />
March 27, 2009 6:30 PM -7:30 PM Friday</p>
<p style="text-align:justify;"><strong>Meeting Agenda:</strong><br />
6:30 PM &#8211; 7:00 PM &#8211; Introduction to Joins and Real Life Scenario<br />
7:00 PM &#8211; 7:10 PM &#8211; Tips to Improve SQL Performance<br />
7:10 PM &#8211; 7:15 PM &#8211; Simple SQL Quiz (3 Question)<br />
7:15 PM &#8211; 7:20 PM &#8211; Feedback<br />
7:20 PM &#8211; 7:30 PM &#8211; Price for &#8220;Best Participant&#8221; and Questions and Answers</p>
<p style="text-align:justify;"><strong>Meeting Location:</strong><br />
Digicorp House,<br />
Nr. Kashiram Agrawal Hall,<br />
Ambawadi,<br />
Ahmedabad-380015<br />
Gujarat, India<br />
Web &#8211; <a href="http://www.digi-corp.com/">Digicorp</a>
</p>
<p style="text-align:justify;">This is your chance to meet industry leaders as well checkout vibrant outsourcing company in India <strong><a href="http://www.digi-corp.com/" target="_blank">Digicorp</a></strong>.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Posted in MVP, Pinal Dave, SQL, SQL Authority, SQL Joins, SQL Query, SQL Server, SQL Tips and Tricks, SQL User Group, SQLAuthority Author Visit, SQLAuthority News, T SQL, Technology Tagged: DigiCorp <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/4063/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/4063/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/4063/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/4063/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/4063/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/4063/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/4063/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/4063/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/4063/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/4063/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/4063/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/4063/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/4063/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/4063/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=4063&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/03/25/sqlauthority-news-announcement-gandhinagar-sql-server-user-group-march-27-2009/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Interesting Observation of ON Clause on LEFT JOIN &#8211; How ON Clause affects Resultset in LEFT JOIN</title>
		<link>http://blog.sqlauthority.com/2009/03/15/sql-server-interesting-observation-of-on-clause-on-left-join-how-on-clause-effects-resultset-in-left-join/</link>
		<comments>http://blog.sqlauthority.com/2009/03/15/sql-server-interesting-observation-of-on-clause-on-left-join-how-on-clause-effects-resultset-in-left-join/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 01:30:09 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=3755</guid>
		<description><![CDATA[Today I received email from Yoel from Israel. He is one smart man always bringing up interesting questions. Let us see his latest email first. Hi Pinal, I am subscribed to your blog and enjoy reading it. I have a question which has been bothering me for some time now. When I want to filter [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=3755&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Today I received email from Yoel from Israel. He is one smart man always bringing up interesting questions. Let us see his latest email first.</p>
<p style="text-align:justify;"><em>Hi Pinal,</em></p>
<p style="text-align:justify;"><em>I am subscribed to your blog and enjoy reading it. I have a question which has been bothering me for some time now.</em></p>
<p style="text-align:justify;"><em>When I want to filter records in a query, I usually put the condition in the WHERE clause. When I make an inner join, I can put the condition in the ON clause instead, giving the same result. But with left joins this is not the case. Here is a quote from the SQL Server documentation:</em></p>
<p style="padding-left:30px;text-align:justify;"><strong><em>Although the placement of such predicates does not make a difference for INNER joins, they might cause a different result when OUTER joins are involved. This is because the predicates in the ON clause are applied to the table before the join, whereas the WHERE clause is semantically applied to the result of the join.</em></strong></p>
<p style="text-align:justify;"><em>I still can&#8217;t fully grasp the difference. Perhaps you could explain this better? I am sure that many of your readers could benefit from this too.<br />
Thank you.</em></p>
<p style="text-align:justify;">Yoel,</p>
<p style="text-align:justify;">You have asked very good question. Let us go through following example first. <a href="http://www.pinaldave.com/bimg/LeftJoinON.zip" target="_blank">Download the script for this example here and execute in SSMS</a>. I will explain the behavior as we go through the example. If any user does not want to read complete explanation, just go last paragraph of article which is in bold fonts.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">-- Create Table1<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">Table1<br />
</span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">Value </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">), </span><span style="color:black;">Flag </span></code><code style="font-size:12px;"><span style="color:blue;">INT</span></code><code style="font-size:12px;"><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Populate Table1<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">Table1 </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">Value</span><span style="color:gray;">, </span><span style="color:black;">Flag</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:red;">'First'</span><span style="color:gray;">, </span><span style="color:black;">1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">, </span><span style="color:red;">'Second'</span><span style="color:gray;">, </span><span style="color:black;">1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">, </span><span style="color:red;">'Third'</span><span style="color:gray;">, </span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">, </span><span style="color:red;">'Fourth'</span><span style="color:gray;">, </span><span style="color:black;">1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">5</span><span style="color:gray;">, </span><span style="color:red;">'Fifth'</span><span style="color:gray;">, </span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">6</span><span style="color:gray;">, </span><span style="color:red;">'Sixth'</span><span style="color:gray;">, </span><span style="color:black;">1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">7</span><span style="color:gray;">, </span><span style="color:red;">'Seventh'</span><span style="color:gray;">, </span><span style="color:black;">2<br />
GO<br />
</span><span style="color:green;">-- Create Table2<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">Table2<br />
</span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">Value </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">), </span><span style="color:black;">Flag </span></code><code style="font-size:12px;"><span style="color:blue;">INT</span></code><code style="font-size:12px;"><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Populate Table2<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">Table2 </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">Value</span><span style="color:gray;">, </span><span style="color:black;">Flag</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:red;">'First'</span><span style="color:gray;">, </span><span style="color:black;">1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">, </span><span style="color:red;">'Second'</span><span style="color:gray;">, </span><span style="color:black;">1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">, </span><span style="color:red;">'Third'</span><span style="color:gray;">, </span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">8</span><span style="color:gray;">, </span><span style="color:red;">'Eightth'</span><span style="color:gray;">, </span><span style="color:black;">1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">9</span><span style="color:gray;">, </span><span style="color:red;">'Nineth'</span><span style="color:gray;">, </span><span style="color:black;">2<br />
GO<br />
</span><span style="color:green;">-- Check the data in Table1 and Table2<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table2<br />
GO<br />
</span><span style="color:green;">-- INNER JOIN with WHERE Condition<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">t2.Flag </span><span style="color:blue;">= </span><span style="color:black;">1<br />
GO<br />
</span><span style="color:green;">-- INNER JOIN with Additional Condition on ON clause<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID </span><span style="color:gray;">AND </span><span style="color:black;">t2.Flag </span><span style="color:blue;">= </span><span style="color:black;">1<br />
GO<br />
</span><span style="color:green;">-- LEFT JOIN with WHERE Condition<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:magenta;">LEFT </span><span style="color:blue;">JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID </span><span style="color:gray;">AND </span><span style="color:black;">t2.Flag </span><span style="color:blue;">= </span><span style="color:black;">1<br />
GO<br />
</span><span style="color:green;">-- LEFT JOIN with Additional Condition on ON clause<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Table1 t1<br />
</span><span style="color:magenta;">LEFT </span><span style="color:blue;">JOIN </span><span style="color:black;">Table2 t2 </span><span style="color:blue;">ON </span><span style="color:black;">t1.ID </span><span style="color:blue;">= </span><span style="color:black;">t2.ID<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">t2.Flag </span><span style="color:blue;">= </span><span style="color:black;">1<br />
GO<br />
</span><span style="color:green;">-- Clean up tables<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">Table1<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">Table2<br />
GO</span></code></p>
<p style="text-align:justify;">Let us see the example in parts. Let us see the result of <strong>INNER JOIN</strong> clause. First of all we will run the query without WHERE clause and ON clause. Our result of join without any clause has to be same as they are essentially same query.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/LeftJoinON1.jpg" alt="" width="476" height="430" /></p>
<p style="text-align:justify;">Now we will run the same query with WHERE clause and ON clause and compare our result with earlier result and later resultset.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/LeftJoinON2.jpg" alt="" width="459" height="399" /></p>
<p style="text-align:justify;">As mentioned earlier it really does not matter for INNER JOIN if WHERE condition is moved to ON clause. In case of JOIN if ON clause (evaluated first) of WHERE clause (evaluated later) the result is same.</p>
<p style="text-align:justify;">Now let us see the example of <strong>LEFT JOIN</strong>. First we will run both the queries without WHERE clause and ON clause. Our result of join without any clause has to be same as they are essentially same query.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/LeftJoinON3.jpg" alt="" width="469" height="576" /></p>
<p style="text-align:justify;">Now we will run the same query with WHERE clause and ON clause and compare our result with earlier result and later resultset.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/LeftJoinON4.jpg" alt="" width="448" height="478" /></p>
<p style="text-align:justify;"><strong>Now let us understand ON clause it is apply before JOIN that is why it retrieves all the result of Table2 where there are Flag = 1 but it does not affect Table1 so it retrieves all the rows of table1. When WHERE clause is applied it applies to complete result so it removes all the rows from Table1 and Table2 where Flag is not equal to 1, essentially keeping flag = 1 rows from Table1 and Table2.</strong></p>
<p style="text-align:justify;">I hope now this explanation is clear to Yoel. I am really waiting for feedback from my readers about this article. If you think this is interesting article, please share it on your social network and leave your comment here.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Posted in Pinal Dave, Readers Question, SQL, SQL Authority, SQL Joins, SQL Performance, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/3755/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/3755/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/3755/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/3755/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/3755/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/3755/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/3755/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/3755/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/3755/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/3755/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/3755/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/3755/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/3755/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/3755/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=3755&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/03/15/sql-server-interesting-observation-of-on-clause-on-left-join-how-on-clause-effects-resultset-in-left-join/feed/</wfw:commentRss>
		<slash:comments>43</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/LeftJoinON1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/LeftJoinON2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/LeftJoinON3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/LeftJoinON4.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Simulate INNER JOIN using LEFT JOIN statement &#8211; Performance Analysis</title>
		<link>http://blog.sqlauthority.com/2008/10/25/sql-server-simulate-inner-join-using-left-join-statement-performance-analysis/</link>
		<comments>http://blog.sqlauthority.com/2008/10/25/sql-server-simulate-inner-join-using-left-join-statement-performance-analysis/#comments</comments>
		<pubDate>Sat, 25 Oct 2008 01:30:15 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1426</guid>
		<description><![CDATA[Just a day ago, while I was working with JOINs I find one interesting observation, which has prompted me to create following example. Before we continue further let me make very clear that INNER JOIN should be used where it can not be used and simulating INNER JOIN using any other JOINs will degrade the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1426&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Just a day ago, while I was working with JOINs I find one interesting observation, which has prompted me to create following example. Before we continue further let me make very clear that <em><strong>INNER JOIN should be used where it can not be used and simulating INNER JOIN using any other JOINs will degrade the performance</strong></em>. If there are scopes to convert any OUTER JOIN to INNER JOIN it should be done with priority.</p>
<p style="text-align:justify;">Run following two script and observe the resultset. Resultset will be identical.</p>
<p><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">/* Example of INNER JOIN */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">p.ProductID</span><span style="color:gray;">, </span><span style="color:black;">piy.ProductID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product p<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Production.ProductInventory piy </span><span style="color:blue;">ON </span><span style="color:black;">piy.ProductID </span><span style="color:blue;">= </span><span style="color:black;">p.ProductID<br />
GO<br />
</span><span style="color:green;">/* Example of LEFT JOIN simulating INNER JOIN */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">p.ProductID</span><span style="color:gray;">, </span><span style="color:black;">piy.ProductID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product p<br />
</span><span style="color:magenta;">LEFT </span><span style="color:blue;">JOIN </span><span style="color:black;">Production.ProductInventory piy </span><span style="color:blue;">ON </span><span style="color:black;">1 </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">piy.ProductID </span><span style="color:blue;">= </span><span style="color:black;">p.ProductID<br />
GO </span></code></p>
<p style="text-align:justify;">After looking at identical result the first thing came in to my mind was what is the behind the scene plan. When looking at the actual execution plan of the query it is very clear that even LEFT JOIN is used SQL Server Query Optimizer converts it to INNER JOIN as it determined that there is no need of OUTER LEFT JOIN and INNER JOIN will give better performance.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leftinner.gif" alt="" width="500" height="608" /></p>
<p style="text-align:justify;">Looking at above scenario it makes us think how smart Query Optimizer Engine is and how it might be saving lots of performance related issue for sub-optimal queries.</p>
<p style="text-align:justify;">Now let us understand why LEFT JOIN acts as  INNER JOIN. When 1= 1 is used in ON clause it is always true and converts LEFT JOIN to CROSS JOIN. However, when WHERE condition&#8217;s effect is applied to above CROSS JOIN it produces the result similar to INNER JOIN in our case. SQL Server Query Optimizer interprets this earlier and uses INNER JOIN right away.</p>
<p style="text-align:justify;">I think this is good interview questions to ask. Interview question which can be asked is <strong>&#8220;How to write OUTER JOIN which will give you exact same result, execution plan and performance as INNER JOIN?&#8221;</strong></p>
<p style="text-align:justify;">If there is any other explanation or if you know if there is any similar example please let me know and I will post on this blog.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Posted in Pinal Dave, SQL, SQL Authority, SQL Interview Questions and Answers, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/1426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/1426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/1426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/1426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1426/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1426&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/10/25/sql-server-simulate-inner-join-using-left-join-statement-performance-analysis/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/leftinner.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Get Common Records From Two Tables Without Using Join</title>
		<link>http://blog.sqlauthority.com/2008/10/17/sql-server-get-common-records-from-two-tables-without-using-join/</link>
		<comments>http://blog.sqlauthority.com/2008/10/17/sql-server-get-common-records-from-two-tables-without-using-join/#comments</comments>
		<pubDate>Fri, 17 Oct 2008 01:30:52 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1380</guid>
		<description><![CDATA[I really enjoy answering questions which I receive from either comments or Email. My passion is shared by SQL Server Expert Imran Mohammed. He frequently SQL community members by answering their questions frequently and promptly. Sachin Asked: Following is my scenario, Suppose Table 1 and Table 2 has same column e.g. Column1 Following is the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1380&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I really enjoy answering questions which I receive from either comments or Email. My passion is shared by SQL Server Expert <strong>Imran Mohammed</strong>. He frequently SQL community members by answering their questions frequently and promptly.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/contact-me-contact-pinaldave/#comment-43356" target="_blank">Sachin</a> Asked:</p>
<p style="padding-left:30px;text-align:justify;">Following is my scenario,<br />
Suppose Table 1 and Table 2 has same column e.g. Column1<br />
Following is the query,</p>
<p style="text-align:justify;">1. Select column1,column2 From Table1<br />
2. Select column1 From Table2
</p>
<p style="text-align:justify;">I want to find common records from these tables, but i don’t want to use Join clause bcoz for that i need to specify the column name for Join condition. <em><strong>Will you help me to get common records without using Join condition?</strong></em> I am using SQL Server 2005.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/contact-me-contact-pinaldave/#comment-43384" target="_blank">Imran Mohammed</a> Replied:</p>
<p style="padding-left:30px;text-align:justify;">If you are using SQL Server 2005, then you can use Intersect Key word, which gives you common records.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">column1<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">table1<br />
</span><span style="color:blue;">INTERSECT<br />
SELECT </span><span style="color:black;">column1<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">table2</span></code>
</p>
<p style="text-align:justify;">If you want in the output both column1 and column2 from table1 which has common columns1 in both tables.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">column1</span><span style="color:gray;">, </span><span style="color:black;">column2<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">table1<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">column1 </span><span style="color:blue;">IN<br />
</span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">column1<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">table1<br />
</span><span style="color:blue;">INTERSECT<br />
SELECT </span><span style="color:black;">column1<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">table2<br />
</span><span style="color:gray;">)</span></code>
</p>
<p style="text-align:justify;">To do this, make sure your column1 is unique and do not have duplicate records.</p>
<p style="text-align:justify;">This is good answer. INTERSECT is new operator in SQL Server which gives you similar answer without using JOIN. I have previously written article where I have compared INTERSECT with INNER JOIN I suggest that all user read that article for further clarity.</p>
<h3 style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2008/08/03/sql-server-2005-difference-between-intersect-and-inner-join-intersect-vs-inner-join/" target="_blank">SQL SERVER &#8211; 2005 &#8211; Difference Between INTERSECT and INNER JOIN &#8211; INTERSECT vs. INNER JOIN</a></strong></h3>
<p style="text-align:justify;">I would appreciate my readers input about this article and if you know any alternative method, it will be interesting to see them.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Posted in Pinal Dave, Readers Contribution, Readers Question, SQL, SQL Authority, SQL Joins, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/1380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/1380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/1380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/1380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1380/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1380&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/10/17/sql-server-get-common-records-from-two-tables-without-using-join/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction and Example of UNION and UNION ALL</title>
		<link>http://blog.sqlauthority.com/2008/10/15/sql-server-introduction-and-example-of-union-and-union-all/</link>
		<comments>http://blog.sqlauthority.com/2008/10/15/sql-server-introduction-and-example-of-union-and-union-all/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 01:30:32 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Union clause]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1364</guid>
		<description><![CDATA[It is very much interesting when I get request from blog reader to re-write my previous articles. I have received few request to rewrite my article SQL SERVER &#8211; Union vs. Union All &#8211; Which is better for performance? wi.th examples. I request you to read my previous article first to understand what is the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1364&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">It is very much interesting when I get request from blog reader to re-write my previous articles. I have received few request to rewrite my article <a href="http://blog.sqlauthority.com/2007/03/10/sql-server-union-vs-union-all-which-is-better-for-performance/" target="_blank">SQL SERVER &#8211; Union vs. Union All &#8211; Which is better for performance?</a> wi.th examples. I request you to read my previous article first to understand what is the concept and read this article to understand the same concept with example.</p>
<div class="entry" style="text-align:justify;">
<div class="snap_preview">
xe=&#8221;color:green;&#8221;&gt;/* Create First Table */<br />
<span style="color:blue;">DECLARE </span><span style="color:#434343;">@Table1 </span><span style="color:blue;">TABLE </span><span style="color:gray;">(</span><span style="color:black;">Col </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">5</p>
<p></span><span style="color:green;">/* Create Second Table */<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Table2 </span><span style="color:blue;">TABLE </span><span style="color:gray;">(</span><span style="color:black;">Col </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table2<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table2<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table2<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">6<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table2<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">7<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@Table2<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">8</p>
<p></span><span style="color:green;">/* Result of Union operation */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Col </span><span style="color:red;">&#8216;Union&#8217;<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:black;">Col<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@Table2</p>
<p></span><span style="color:green;">/* Result of Union All operation */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Col </span><span style="color:red;">&#8216;UnionAll&#8217;<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@Table1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Col<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@Table2<br />
</span><span style="color:black;">GO</span></div>
<div class="snap_preview">
<p>The difference between Union and Union all is that Union all will not eliminate duplicate rows, instead it just pulls all rows from all tables fitting your query specifics and combines them into a table.</p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg//unionunionall.gif" alt="" width="322" height="680" /></p>
<p><strong><em>A UNION statement effectively does a SELECT DISTINCT on the results set. If you know that all the records returned are unique from your union, use UNION ALL instead, it gives faster results.</em></strong></p>
<p>If you look at the resultset it is clear that UNION ALL gives result unsorted but in UNION result are sorted. Let us see the query plan to see what really happens when this operation are done.</p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg/unionplan.gif" alt="" width="500" height="391" /></p>
<p>From the plan it is very clear that in UNION clause there is an additional operation of DISTINCT SORT takes place where as in case of UNION ALL there is no such operation but simple concatenation happens. From our understanding of UNION and UNION ALL this makes sense.</p>
<p>There are three rules of UNION one should remember.</p>
<p><a href="http://blog.sqlauthority.com/2007/10/14/sql-server-three-rules-to-use-union/" target="_blank"><strong>UNION RULES</strong></a></p>
<ul>
<li>A UNION must be composed of two or more SELECT statements, each separated by the keyword UNION.</li>
<li>Each query in a UNION must contain the same columns, expressions, or aggregate functions, and they must be listed in the same order.</li>
<li>Column datatypes must be compatible: They need not be the same exact same type, but they must be of a type that SQL Server can implicitly convert.</li>
</ul>
</div>
<p>Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></div>
<br />Posted in Pinal Dave, SQL, SQL Authority, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: SQL Union clause <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1364/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1364&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/10/15/sql-server-introduction-and-example-of-union-and-union-all/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg//unionunionall.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/unionplan.gif" medium="image" />
	</item>
	</channel>
</rss>
