<?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>Journey to SQL Authority with Pinal Dave &#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>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 01:30:19 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='blog.sqlauthority.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/08e35387c05b61340e885b1763a69d9f?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Journey to SQL Authority with Pinal Dave &#187; SQL Joins</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
			<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 is a Hint?
Hints [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7113&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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;">
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/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&blog=668536&post=7113&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/11/19/sql-server-understanding-table-hints-with-examples/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/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&blog=668536&post=6916&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=6916&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/06/sql-server-interesting-observation-query-hint-force-order/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/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 White Papers]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL View]]></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 OUTER JOIN [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6910&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
Posted in Pinal Dave, SQL, SQL Authority, SQL Index, SQL Joins, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, SQL White Papers, SQLServer, T SQL, Technology Tagged: SQL View <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/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&blog=668536&post=6910&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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; 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 AS (SELECT 1 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6399&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=6399&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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; 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 a Hint?
Hints [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5645&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=5645&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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; 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&blog=668536&post=4307&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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/best-sql-server-download.cfm?download=EverythingJoin" target="_blank">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>
<p style="text-align:justify;">
<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 after 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>
<p style="text-align:justify;">
<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/best-sql-server-download.cfm?download=EverythingJoin" 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>
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/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&blog=668536&post=4307&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/feed/</wfw:commentRss>
		<slash:comments>50</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/inner join.jpg" medium="image" />

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

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

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

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

		<media:content url="http://www.pinaldave.com/bimg/march09ug/left join null.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/march09ug/right join null.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/march09ug/outer join null.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[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[Software Development]]></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[Download Logical Query Processing Poster
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 Craig. According to him this phenomenon happens due [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4218&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;"><strong><a href="http://www.pinaldave.com/best-sql-server-download.cfm?Download=Logical-Query-Processing-Poster" target="_blank"><strong>Download Logical Query Processing Poster</strong></a></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>
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/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&blog=668536&post=4218&subd=sqlauthority&ref=&feed=1" /></div>]]></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>22</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&blog=668536&post=4063&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=4063&subd=sqlauthority&ref=&feed=1" /></div>]]></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 Effects 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 records in a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3755&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
<div style="text-align:justify;"><span style="font-size:10pt;font-family:Tahoma;" lang="HE"><span style="color:#000080;"><span style="color:#800000;"><strong> </strong></span></span></span></div>
</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>
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/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&blog=668536&post=3755&subd=sqlauthority&ref=&feed=1" /></div>]]></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>16</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&blog=668536&post=1426&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=1426&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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/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 query,
1. Select column1,column2 From [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1380&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=1380&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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>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? with 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&blog=668536&post=1364&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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> with 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">
<p><strong>UNION</strong><br />
The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the UNION command all selected columns need to be of the same data type. With UNION, only distinct values are selected.</p>
<p><strong>UNION ALL</strong><br />
The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.</p>
<div class="snap_preview">Run following script to produce result for UNION and UNION ALL.</div>
<div class="snap_preview"><code style="font-size:12px;"><span style="color:green;">/* Create First Table */<br />
</span><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;">'Union'<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;">'UnionAll'<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></code></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/blogfolder/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/blogfolder/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>
</div>
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/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&blog=668536&post=1364&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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/blogfolder/unionunionall.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/unionplan.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Guidelines and Coding Standards Complete List Download</title>
		<link>http://blog.sqlauthority.com/2008/09/25/sql-server-guidelines-and-coding-standards/</link>
		<comments>http://blog.sqlauthority.com/2008/09/25/sql-server-guidelines-and-coding-standards/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 01:30:54 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Coding Standards]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Cursor]]></category>
		<category><![CDATA[SQL Data Storage]]></category>
		<category><![CDATA[SQL Documentation]]></category>
		<category><![CDATA[SQL Download]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Security]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Trigger]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[SQLAuthority]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1143</guid>
		<description><![CDATA[SQL SERVER &#8211; Guidelines and Coding Standards complete List Download
Coding standards and guidelines are very important for any developer on the path of successful career. A coding standard is a set of guidelines, rules and regulations on how to write code. Coding standards should be flexible enough or should take care of the situation where [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1143&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2 style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?download=SQL-SERVER-Database-Coding-Standards-and-Guidelines-Complete-List-Download"><strong>SQL SERVER &#8211; Guidelines and Coding Standards complete List Download</strong></a></h2>
<p style="text-align:justify;">Coding standards and guidelines are very important for any developer on the path of successful career. A coding standard is a set of guidelines, rules and regulations on how to write code. Coding standards should be flexible enough or should take care of the situation where they should not prevent best practices for coding. They are basically the guidelines that one should follow for better understanding.</p>
<p style="text-align:justify;">The concept behind implementing coding standards and guidelines, is that the consistency and uniformity in programming so that if multiple people are working on the same code, it becomes easier to communicate, share with or understand each other’s work.</p>
<p style="text-align:justify;">With the goal of promoting good coding standards and guidelines I have created document which can guide developers.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/23/sql-server-coding-standards-guidelines-part-1/" target="_blank"><strong>SQL SERVER &#8211; Guidelines and Coding Standards Part &#8211; 1</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/24/sql-server-coding-standards-guidelines-part-2/" target="_blank"><strong>SQL SERVER &#8211; Guidelines and Coding Standards Part &#8211; 2</strong></a></p>
<h2 style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?download=SQL-SERVER-Database-Coding-Standards-and-Guidelines-Complete-List-Download"><strong>SQL SERVER &#8211; Guidelines and Coding Standards complete List Download</strong></a></h2>
<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>
Posted in Best Practices, Data Warehousing, Database, DBA, Pinal Dave, SQL, SQL Authority, SQL Coding Standards, SQL Constraint and Keys, SQL Cursor, SQL Data Storage, SQL Documentation, SQL Download, SQL Function, SQL Index, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Security, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQL Trigger, SQL Utility, SQLAuthority, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1143/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1143&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/25/sql-server-guidelines-and-coding-standards/feed/</wfw:commentRss>
		<slash:comments>9</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; Guidelines and Coding Standards Part &#8211; 1</title>
		<link>http://blog.sqlauthority.com/2008/09/23/sql-server-coding-standards-guidelines-part-1/</link>
		<comments>http://blog.sqlauthority.com/2008/09/23/sql-server-coding-standards-guidelines-part-1/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 01:30:30 +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 Coding Standards]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Documentation]]></category>
		<category><![CDATA[SQL Index]]></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 Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1107</guid>
		<description><![CDATA[
 Use &#8220;Pascal&#8221; notation for SQL server Objects Like Tables, Views, Stored Procedures. Also tables and views should have ending &#8220;s&#8221;.
Example:
UserDetails
Emails

 If you have big subset of table group than it makes sense to give prefix for this table group. Prefix should be separated by _. 
Example:
Page_ UserDetails
Page_ Emails

 Use following naming convention for Stored [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1107&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Use &#8220;Pascal&#8221; notation for SQL server Objects Like Tables, Views, Stored Procedures. Also tables and views should have ending &#8220;s&#8221;.</strong></li>
<p>Example:</p>
<p>UserDetails</p>
<p>Emails</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>If you have big subset of table group than it makes sense to give prefix for this table group. Prefix should be separated by _. </strong></li>
<p>Example:</p>
<p>Page_ UserDetails</p>
<p>Page_ Emails</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Use following naming convention for Stored Procedure. sp&lt;Application Name&gt;_[&lt;group name &gt;_]&lt;action type&gt;&lt;table name or logical instance&gt; Where action is: Get, Delete, Update, Write, Archive, Insert&#8230; i.e. verb </strong></li>
<p>Example:</p>
<p>spApplicationName_GetUserDetails</p>
<p>spApplicationName_UpdateEmails</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Use following Naming pattern for triggers: TR_&lt;TableName&gt;_&lt;action&gt;&lt;description&gt; </strong></li>
<p>Example:</p>
<p>TR_Emails_LogEmailChanges</p>
<p>TR_UserDetails_UpdateUserName</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Indexes : IX_&lt;tablename&gt;_&lt;columns separated by_&gt; </strong></li>
<p>Example:</p>
<p>IX_UserDetails_UserID</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Primary Key : PK_&lt;tablename&gt; </strong></li>
<p>Example:</p>
<p>PK_UserDetails</p>
<p>PK_ Emails</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Foreign Key : FK_&lt;tablename_1&gt;_&lt;tablename_2&gt; </strong></li>
<p>Example:</p>
<p>FK_UserDetails_Emails</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Default: DF_&lt;table name&gt;_&lt;column name&gt; </strong></li>
<p>Example:</p>
<p>DF_ UserDetails _UserName</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Normalize Database structure based on <strong>3<sup>rd</sup> Normalization Form</strong>. Normalization is the process of designing a data model to efficiently store data in a database. (<a href="http://blog.sqlauthority.com/2007/11/26/sql-server-rules-of-third-normal-form-and-normalization-advantage-3nf/">Read More Here</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Avoid use of <strong>SELECT *</strong> in SQL queries. Instead practice writing required <strong>column</strong> names after <strong>SELECT</strong> statement.</li>
<p>Example:</p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">Username</span><span style="color:gray;">, </span><span style="color:black;">Password<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">UserDetails</span></code></ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Avoid using temporary tables and derived tables as it uses more disks I/O. Instead use <strong>CTE (Common Table Expression); </strong>its scope is limited to the next statement in SQL query. (<a href="http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/">Read More Here</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Use <strong>SET NOCOUNT ON</strong> at the beginning of SQL Batches, Stored Procedures and Triggers. This improves the performance of Stored Procedure. (<a href="http://blog.sqlauthority.com/2006/11/30/sql-server-cursor-to-process-tables-in-database-with-static-prefix-and-date-created/">Read More Here</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Properly <strong>format</strong> SQL queries using indents.</li>
<p>Example: <em><strong>Wrong</strong> Format</em></p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">Username</span><span style="color:gray;">, </span><span style="color:black;">Password </span><span style="color:blue;">FROM </span><span style="color:black;">UserDetails ud </span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Employee e </span><span style="color:blue;">ON </span><span style="color:black;">e.EmpID </span><span style="color:blue;">= </span><span style="color:black;">ud.UserID</span></code></p>
<p>Example: <em><strong>Correct</strong> Format</em></p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">Username</span><span style="color:gray;">, </span><span style="color:black;">Password</span></code><br />
<code style="font-size:12px;"><span style="color:blue;">FROM </span><span style="color:black;">UserDetails ud</span></code><br />
<code style="font-size:12px;"><span style="color:blue;">INNER JOIN </span><span style="color:black;">Employee e </span><span style="color:blue;">ON </span><span style="color:black;">e.EmpID </span><span style="color:blue;">= </span><span style="color:black;">ud.UserID</span></code></ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Practice writing Upper Case for all SQL keywords.</li>
<p>Example:</p>
<p>SELECT, UPDATE, INSERT, WHERE, INNER JOIN, AND, OR, LIKE.</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> There must be <strong>PRIMARY KEY</strong> in all the tables of database with column name ID. It is common practice to use Primary Key as <strong>IDENTITY</strong> column.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> If &#8220;One Table&#8221; references &#8220;Another Table&#8221; than the column name used in reference should use the following rule :</li>
<p><strong>Column of Another Table : &lt;OneTableName&gt; ID</strong></p>
<p>Example:</p>
<p>If User table references Employee table than the column name used in reference should be <strong>UserID</strong> where User is table name and ID primary column of User table and UserID is reference column of Employee table.</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Columns with <strong>Default value</strong> constraint should not allow NULLs.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Practice using <strong>PRIMARY</strong> key in <strong>WHERE</strong> condition of <strong>UPDATE</strong> or <strong>DELETE</strong> statements as this will avoid error possibilities.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Always create stored procedure in <strong>same database</strong> where its relevant table exists otherwise it will reduce network performance.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Avoid server-side Cursors</strong> as much as possible, instead use SELECT statement. If you need to use cursor then replace it with WHILE loop (or read next suggestion).</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Instead of using <strong>LOOP</strong> to insert data from Table B to Table A, try to use <strong>SELECT</strong> statement with <strong>INSERT</strong> statement. (<a href="http://blog.sqlauthority.com/2007/08/15/sql-server-insert-data-from-one-table-to-another-table-insert-into-select-select-into-table/">Read More Here</a>)</li>
<p><code style="font-size:12px;"><span style="color:blue;">INSERT INTO TABLE </span><span style="color:black;">A </span><span style="color:gray;">(</span><span style="color:black;">column1</span><span style="color:gray;">, </span><span style="color:black;">column2</span><span style="color:gray;">)</span></code><br />
<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</span></code><br />
<code style="font-size:12px;"><span style="color:blue;">FROM TABLE </span><span style="color:black;">B</span></code><br />
<code style="font-size:12px;"><span style="color:blue;">WHERE </span><span style="color:black;">.... </span></code></ul>
<ul class="unIndentedList" style="text-align:justify;">
<li><strong>Avoid using spaces within the name of database objects</strong>; this may create issues with front-end data access tools and applications. If you need spaces in your database object name then will accessing it surround the database object name with square brackets.</li>
<p>Example:</p>
<p>[Order Details]</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Do not use <strong>reserved words</strong> for naming database objects, as that can lead to some unpredictable situations. (<a href="http://blog.sqlauthority.com/2007/04/09/sql-server-2005-reserved-keywords/">Read More Here</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Practice writing comments</strong> in stored procedures, triggers and SQL batches, whenever something is not very obvious, as it won&#8217;t impact the performance.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Do not use <strong>wild card characters</strong> at the beginning of word while search using LIKE keyword as it results in Index scan.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Indent code</strong> for better readability. (<a href="http://blog.sqlauthority.com/2008/02/26/sql-server-select-1-vs-select-an-interesting-observation/">Example</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> While using <strong>JOINs</strong> in your SQL query always <strong>prefix column name</strong> with the table name. (<a href="http://blog.sqlauthority.com/2008/08/02/sql-server-effect-of-order-of-join-in-query/">Example</a>). If additionally require then prefix Table name with ServerName, DatabaseName, DatabaseOwner. (<a href="http://blog.sqlauthority.com/2007/06/26/sql-server-explanation-and-example-four-part-name/">Example</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Default constraint must be defined at the <strong>column level</strong>. All other constraints must be defined at the <strong>table level</strong>. (<a href="http://blog.sqlauthority.com/2008/09/08/sql-server-%e2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/">Read More Here</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Avoid using rules of database objects instead use <strong>constraints</strong>.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Do not use the <strong>RECOMPILE</strong> option for Stored Procedure as it reduces the performance.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Always put the <strong>DECLARE</strong> statements at the starting of the code in the stored procedure. This will make the query optimizer to reuse query plans. (<a href="http://blog.sqlauthority.com/2007/04/11/sql-server-udf-user-defined-function-to-extract-only-numbers-from-string/">Example</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Put the <strong>SET</strong> statements in beginning (after DECLARE) before executing code in the stored procedure. (<a href="http://blog.sqlauthority.com/2007/04/11/sql-server-udf-user-defined-function-to-extract-only-numbers-from-string/">Example</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Use <strong>BEGIN&#8230;END</strong> blocks only when multiple statements are present within a conditional code segment. (<a href="http://blog.sqlauthority.com/2007/11/28/sql-server-correct-syntax-for-stored-procedure-sp/">Read More Here</a>)</li>
</ul>
<p style="text-align:justify;">© Copyright 2000-2008<a title="Pinal Dave" href="http://www.pinaldave.com/"> Pinal Dave.</a> All Rights Reserved. <a href="http://blog.sqlauthority.com/">SQLAuthority.com</a></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>
Posted in Best Practices, Database, Pinal Dave, Software Development, SQL, SQL Authority, SQL Coding Standards, SQL Constraint and Keys, SQL Documentation, SQL Index, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1107/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1107&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/23/sql-server-coding-standards-guidelines-part-1/feed/</wfw:commentRss>
		<slash:comments>20</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; 2008 &#8211; Interview Questions and Answers Complete List Download</title>
		<link>http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/</link>
		<comments>http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 01:30:39 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Cursor]]></category>
		<category><![CDATA[SQL Data Storage]]></category>
		<category><![CDATA[SQL DateTime]]></category>
		<category><![CDATA[SQL Documentation]]></category>
		<category><![CDATA[SQL Download]]></category>
		<category><![CDATA[SQL Error Messages]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Index]]></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 Security]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBCC]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Trigger]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[SQLAuthority]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL XML]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1134</guid>
		<description><![CDATA[Download SQL Server 2008 Interview Questions and Answers Complete List
Interview is very important event for any person. A good interview leads to good career if candidate is willing to learn. I always enjoy interview questions and answers series. This is my very humble attempt to write SQL Server 2008 interview questions and answers. SQL Server [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1134&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2 style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?download=SQL-SERVER-2008-Download-Interview-Questions-and-Answers"><strong>Download SQL Server 2008 Interview Questions and Answers Complete List</strong></a></h2>
<p style="text-align:justify;">Interview is very important event for any person. A good interview leads to good career if candidate is willing to learn. I always enjoy interview questions and answers series. This is my very humble attempt to write SQL Server 2008 interview questions and answers. SQL Server is very large subject and not everything is usually asked in interview. In interview what matters the most is <strong>conceptual knowledge</strong> and <strong>learning attitude</strong>.</p>
<p style="text-align:justify;">I have listed all the series in this post so that it can be easily downloaded and used. All the questions are collected and listed in one PDF which is here to download. If you have any question or if you want to add to any of the question please send me mail or write a comment.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/12/sql-server-2008-interview-questions-and-answers-part-1/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 1</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/13/sql-server-2008-interview-questions-and-answers-part-2/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 2</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/14/sql-server-2008-interview-questions-and-answers-part-3/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 3</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/15/sql-server-2008-interview-questions-and-answers-part-4/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 4</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/16/sql-server-2008-interview-questions-and-answers-part-5/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 5</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/17/sql-server-2008-interview-questions-and-answers-part-6/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 6</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/18/sql-server-2008-interview-questions-and-answers-part-7/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 7</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/19/sql-server-2008-interview-questions-and-answers-part-8/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 8</strong></a></p>
<h2 style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?download=SQL-SERVER-2008-Download-Interview-Questions-and-Answers"><strong>Download SQL Server 2008 Interview Questions and Answers Complete List</strong></a></h2>
<p style="text-align:justify;">
<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>
Posted in Database, Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Cursor, SQL Data Storage, SQL DateTime, SQL Documentation, SQL Download, SQL Error Messages, SQL Function, SQL Index, SQL Interview Questions and Answers, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Security, SQL Server, SQL Server DBCC, SQL Stored Procedure, SQL Tips and Tricks, SQL Trigger, SQL Utility, SQLAuthority, T SQL, Technology Tagged: SQL XML <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1134/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1134&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/feed/</wfw:commentRss>
		<slash:comments>55</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; 2008 &#8211; Interview Questions and Answers &#8211; Part 2</title>
		<link>http://blog.sqlauthority.com/2008/09/13/sql-server-2008-interview-questions-and-answers-part-2/</link>
		<comments>http://blog.sqlauthority.com/2008/09/13/sql-server-2008-interview-questions-and-answers-part-2/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 01:30:09 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Cursor]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLAuthority]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=991</guid>
		<description><![CDATA[SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download
1) General Questions of SQL SERVER 
What is Cursor?
Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time.
In [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=991&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2 style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/" target="_blank">SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download</a></strong></h2>
<p style="text-align:justify;"><strong>1) </strong><strong>General Questions of SQL SERVER </strong></p>
<p style="text-align:justify;"><strong>What is Cursor?</strong></p>
<p style="text-align:justify;">Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time.</p>
<p style="text-align:justify;">In order to work with a cursor we need to perform some steps in the following order:</p>
<ul class="unIndentedList" style="text-align:justify;">
<li> Declare cursor</li>
<li> Open cursor</li>
<li> Fetch row from the cursor</li>
<li> Process fetched row</li>
<li> Close cursor</li>
<li> Deallocate cursor (<a href="http://blog.sqlauthority.com/2008/04/19/sql-server-is-cursor-database-object-or-datatype/" target="_blank">Read More Here</a>)</li>
</ul>
<p style="text-align:justify;"><strong>What is Collation?</strong></p>
<p style="text-align:justify;">Collation refers to a set of rules that determine how data is sorted and compared. Character data is sorted using rules that define the correct character sequence, with options for specifying case sensitivity, accent marks, kana character types and character width. (<a href="http://blog.sqlauthority.com/2007/04/24/sql-server-2005-collation-explanation-and-translation/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What is Difference between Function and Stored Procedure?</strong></p>
<p style="text-align:justify;">UDF can be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section where as Stored procedures cannot be. UDFs that return tables can be treated as another rowset. This can be used in JOINs with other tables. Inline UDF&#8217;s can be thought of as views that take parameters and can be used in JOINs and other Rowset operations.</p>
<p style="text-align:justify;"><strong>What is sub-query? Explain properties of sub-query?</strong></p>
<p style="text-align:justify;">Sub-queries are often referred to as sub-selects, as they allow a SELECT statement to be executed arbitrarily within the body of another SQL statement. A sub-query is executed by enclosing it in a set of parentheses. Sub-queries are generally used to return a single row as an atomic value, though they may be used to compare values against multiple rows with the IN keyword.</p>
<p style="text-align:justify;">A subquery is a SELECT statement that is nested within another T-SQL statement. A subquery SELECT statement if executed independently of the T-SQL statement, in which it is nested, will return a resultset. Meaning a subquery SELECT statement can standalone and is not depended on the statement in which it is nested. A subquery SELECT statement can return any number of values, and can be found in, the column list of a SELECT statement, a FROM, GROUP BY, HAVING, and/or ORDER BY clauses of a T-SQL statement. A Subquery can also be used as a parameter to a function call. Basically a subquery can be used anywhere an expression can be used. (<a href="http://blog.sqlauthority.com/2007/09/07/sql-server-correlated-and-noncorrelated-subquery-introduction-explanation-and-example/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What are different Types of Join?</strong></p>
<p style="text-align:justify;"><strong><em>Cross Join</em></strong></p>
<p style="text-align:justify;">A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. The common example is when company wants to combine each product with a pricing table to analyze each product at each price.</p>
<p style="text-align:justify;"><strong><em>Inner Join</em></strong></p>
<p style="text-align:justify;">A join that displays only the rows that have a match in both joined tables is known as inner Join.  This is the default type of join in the Query and View Designer.</p>
<p style="text-align:justify;"><strong><em>Outer Join</em></strong></p>
<p style="text-align:justify;">A join that includes rows even if they do not have related rows in the joined table is an Outer Join.  You can create three different outer join to specify the unmatched rows to be included:</p>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Left Outer Join:</strong> In Left Outer Join all rows in the first-named table i.e. &#8220;left&#8221; table, which appears leftmost in the JOIN clause are included. Unmatched rows in the right table do not appear.<strong> </strong><strong><em> </em></strong></li>
</ul>
<p style="text-align:justify;"><strong><em> </em></strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Right Outer Join:</strong> In Right Outer Join all rows in the second-named table i.e. &#8220;right&#8221; table, which appears rightmost in the JOIN clause are included. Unmatched rows in the left table are not included.<strong><em></em></strong></li>
</ul>
<p style="text-align:justify;"><strong><em> </em></strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Full Outer Join:</strong> In Full Outer Join all rows in all joined tables are included, whether they are matched or not.<strong></strong></li>
</ul>
<p style="text-align:justify;"><strong><em>Self Join</em></strong></p>
<p style="text-align:justify;">This is a particular case when one table joins to itself, with one or two aliases to avoid confusion. A self join can be of any type, as long as the joined tables are the same. A self join is rather unique in that it involves a relationship with only one table. The common example is when company has a hierarchal reporting structure whereby one member of staff reports to another. Self Join can be Outer Join or Inner Join. (<a href="http://blog.sqlauthority.com/2007/06/03/sql-server-2005-explanation-and-example-self-join/" target="_blank">Read More Here</a>)<strong></strong></p>
<p style="text-align:justify;"><strong>What are primary keys and foreign keys?</strong></p>
<p style="text-align:justify;">Primary keys are the unique identifiers for each row. They must contain unique values and cannot be null. Due to their importance in relational databases, Primary keys are the most fundamental of all keys and constraints. A table can have only one Primary key.</p>
<p style="text-align:justify;">Foreign keys are both a method of ensuring data integrity and a manifestation of the relationship between tables.</p>
<p style="text-align:justify;"><strong>What is User Defined Functions? What kind of User-Defined Functions can be created?</strong></p>
<p style="text-align:justify;">User-Defined Functions allow defining its own T-SQL functions that can accept 0 or more parameters and return a single scalar data value or a table data type.</p>
<p style="text-align:justify;"><em>Different Kinds of User-Defined Functions created are: </em></p>
<p style="text-align:justify;"><strong><em>Scalar User-Defined Function</em></strong></p>
<p style="text-align:justify;">A Scalar user-defined function returns one of the scalar data types. Text, ntext, image and timestamp data types are not supported. These are the type of user-defined functions that most developers are used to in other programming languages. You pass in 0 to many parameters and you get a return value.</p>
<p style="text-align:justify;"><strong><em>Inline Table-Value User-Defined Function</em></strong></p>
<p style="text-align:justify;">An Inline Table-Value user-defined function returns a table data type and is an exceptional alternative to a view as the user-defined function can pass parameters into a T-SQL select command and in essence provide us with a parameterized, non-updateable view of the underlying tables.<strong><em></em></strong></p>
<p style="text-align:justify;"><strong><em>Multi-statement Table-Value User-Defined Function</em></strong></p>
<p style="text-align:justify;">A Multi-Statement Table-Value user-defined function returns a table and is also an exceptional alternative to a view as the function can support multiple T-SQL statements to build the final result where the view is limited to a single SELECT statement. Also, the ability to pass parameters into a TSQL select command or a group of them gives us the capability to in essence create a parameterized, non-updateable view of the data in the underlying tables. Within the create function command you must define the table structure that is being returned. After creating this type of user-defined function, It can be used in the FROM clause of a T-SQL command unlike the behavior found when using a stored procedure which can also return record sets. (<a href="http://blog.sqlauthority.com/2007/09/08/sql-server-udf-user-defined-function-get-number-of-days-in-month/" target="_blank">Read Here For Example</a>)</p>
<p style="text-align:justify;"><strong>What is Identity?</strong></p>
<p style="text-align:justify;">Identity (or AutoNumber) is a column that automatically generates numeric values. A start and increment value can be set, but most DBA leave these at 1. A GUID column also generates numbers; the value of this cannot be controlled. Identity/GUID columns do not need to be indexed.</p>
<p style="text-align:justify;"><strong>What is DataWarehousing?</strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> Subject-oriented, meaning that the data in the database is organized so that all the data elements relating to the same real-world event or object are linked together;</li>
<li> Time-variant, meaning that the changes to the data in the database are tracked and recorded so that reports can be produced showing changes over time;</li>
<li> Non-volatile, meaning that data in the database is never over-written or deleted, once committed, the data is static, read-only, but retained for future reporting.</li>
<li> Integrated, meaning that the database contains data from most or all of an organization&#8217;s operational applications, and that this data is made consistent.</li>
</ul>
<p style="text-align:justify;">© Copyright 2000-2009<a title="Pinal Dave" href="http://www.pinaldave.com/" target="_blank"> Pinal Dave.</a> All Rights Reserved. <a href="http://blog.sqlauthority.com/" target="_blank">SQLAuthority.com</a></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>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/991/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/991/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/991/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=991&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/13/sql-server-2008-interview-questions-and-answers-part-2/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>SQL SERVER &#8211; 2008 &#8211; Introduction to Merge Statement &#8211; One Statement for INSERT, UPDATE, DELETE</title>
		<link>http://blog.sqlauthority.com/2008/08/28/sql-server-2008-introduction-to-merge-statement-one-statement-for-insert-update-delete/</link>
		<comments>http://blog.sqlauthority.com/2008/08/28/sql-server-2008-introduction-to-merge-statement-one-statement-for-insert-update-delete/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 01:30:27 +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://sqlauthority.wordpress.com/?p=881</guid>
		<description><![CDATA[MERGE is a new feature that provides an efficient way to perform multiple DML operations. In previous versions of SQL Server, we had to write separate statements to INSERT, UPDATE, or DELETE data based on certain conditions, but now, using MERGE statement we can include the logic of such data modifications in one statement that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=881&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">MERGE is a new feature that provides an efficient way to perform multiple DML operations. In previous versions of SQL Server, we had to write separate statements to INSERT, UPDATE, or DELETE data based on certain conditions, but now, using MERGE statement we can include the logic of such data modifications in one statement that even checks when the data is matched then just update it and when unmatched then insert it.</p>
<p style="text-align:justify;">One of the most important advantage of MERGE statement is all the data is read and processed only once. In previous versions three different statement has to be written to process three different activity (INSERT, UPDATE or DELETE), however using MERGE statement all update activity can be done in <strong>one pass of database table</strong>. This is quite an improvement in performance of database query.</p>
<p style="text-align:justify;"><strong>Syntax of MERGE statement is as following:</strong><br />
MERGE<br />
[ TOP ( expression ) [ PERCENT ] ]<br />
[ INTO ] target_table [ WITH ( &lt;merge_hint&gt; ) ] [ [ AS ] table_alias]<br />
USING &lt;table_source&gt;<br />
ON &lt;merge_search_condition&gt;<br />
[ WHEN MATCHED [ AND &lt;clause_search_condition&gt; ]<br />
THEN &lt;merge_matched&gt; ]<br />
[ WHEN NOT MATCHED [ BY TARGET ] [ AND &lt;clause_search_condition&gt; ]<br />
THEN &lt;merge_not_matched&gt; ]<br />
[ WHEN NOT MATCHED BY SOURCE [ AND &lt;clause_search_condition&gt; ]<br />
THEN &lt;merge_matched&gt; ]<br />
[ &lt;output_clause&gt; ]<br />
[ OPTION ( &lt;query_hint&gt; [ ,...n ] ) ]<br />
;<br />
<strong>Example:</strong>
</p>
<p style="text-align:justify;">Let&#8217;s create Student Details and StudentTotalMarks and inserted some records.</p>
<p style="text-align:justify;"><strong>Student Details:</strong></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;">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</span></code>
</p>
<p style="text-align:justify;"><strong>StudentTotalMarks:</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><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</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/InsertMarks.gif" alt="" width="414" height="468" /></p>
<p style="text-align:justify;"><strong>In our example we will consider three main conditions while we merge this two tables.</strong></p>
<ol style="text-align:justify;">
<li>Delete the records whose marks are more than 250.</li>
<li>Update marks and add 25 to each as internals if records exist.</li>
<li>Insert the records if record does not exists.</li>
</ol>
<p style="text-align:justify;">Now we will write MERGE process for tables created earlier. We will make sure that we will have our three conditions discussed above are satisfied.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">MERGE</span></code><code style="font-size:12px;"><span style="color:black;"> 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</span></code>
</p>
<p style="text-align:justify;"><strong>There are two very important points to remember while using MERGE statement.</strong></p>
<ul style="text-align:justify;">
<li>Semicolon is mandatory after the merge statement.</li>
<li>When there is a MATCH clause used along with some condition, it has to be specified first amongst all other WHEN MATCH clause.</li>
</ul>
<p style="text-align:justify;">After the MERGE statement has been executed, we should compare previous resultset and new resultset to verify if our three conditions are carried out.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/MergeStatement.gif" alt="" width="500" height="289" /></p>
<p style="text-align:justify;">AS we can see there are 5 rows updated.  StudentID 2 is deleted as it is more than 250, 25 marks have been added to all records that exists i.e StudentID 1,3 and the records that did not exists i.e. 4 and 5 are now inserted in StudentTotalMarks .</p>
<p style="text-align:justify;"><em><strong>MERGE statement is very handy improvement for T-SQL developers who have to update database tables with complicated logic. MERGE statement also improves the performance of database as it passes through data only once.</strong></em></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>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/881/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/881/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/881/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/881/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/881/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/881/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/881/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/881/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/881/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/881/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/881/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/881/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=881&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/08/28/sql-server-2008-introduction-to-merge-statement-one-statement-for-insert-update-delete/feed/</wfw:commentRss>
		<slash:comments>12</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/blogfolder/InsertMarks.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/MergeStatement.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; EXCEPT Clause in SQL Server is Similar to MINUS Clause in Oracle</title>
		<link>http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/</link>
		<comments>http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 01:30:45 +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://sqlauthority.wordpress.com/?p=791</guid>
		<description><![CDATA[One of the JR. Developer asked me a day ago, does SQL Server has similar operation like MINUS clause in Oracle.
Absolutely, EXCEPT clause in SQL Server is exactly similar to MINUS operation in Oracle. The EXCEPT query and MINUS query returns all rows in the first query that are not returned in the second query. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=791&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">One of the JR. Developer asked me a day ago, does SQL Server has similar operation like MINUS clause in Oracle.</p>
<p style="text-align:justify;">Absolutely, <em><strong>EXCEPT clause in SQL Server is exactly similar to MINUS operation in Oracle</strong></em>. The EXCEPT query and MINUS query returns all rows in the first query that are not returned in the second query. Each SQL statement within the EXCEPT query and MINUS query must have the same number of fields in the result sets with similar data types.</p>
<p style="text-align:justify;">Let us see that using example below. First create table in SQL Server and Oracle.</p>
<p><code style="font-size:12px;"><span style="color:blue;">CREATE TABLE </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:gray;">(</span><span style="color:black;">EmpNo </span><span style="color:blue;">INT </span><span style="color:gray;">NOT NULL, </span><span style="color:black;">EmpName </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:black;">EmpPost </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">9</span><span style="color:gray;">), </span><span style="color:black;">ManagerID </span><span style="color:blue;">INT</span><span style="color:gray;">,<br />
</span><span style="color:black;">Salery </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">COMM </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">DeptNO </span><span style="color:blue;">INT</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7369</span><span style="color:gray;">, </span><span style="color:red;">'SMITH'</span><span style="color:gray;">, </span><span style="color:red;">'CLERK'</span><span style="color:gray;">, </span><span style="color:black;">7902</span><span style="color:gray;">, </span><span style="color:black;">800</span><span style="color:gray;">, NULL, </span><span style="color:black;">20</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7499</span><span style="color:gray;">, </span><span style="color:red;">'ALLEN'</span><span style="color:gray;">, </span><span style="color:red;">'SALESMAN'</span><span style="color:gray;">, </span><span style="color:black;">7698</span><span style="color:gray;">, </span><span style="color:black;">1600</span><span style="color:gray;">, </span><span style="color:black;">300</span><span style="color:gray;">, </span><span style="color:black;">30</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7521</span><span style="color:gray;">, </span><span style="color:red;">'WARD'</span><span style="color:gray;">, </span><span style="color:red;">'SALESMAN'</span><span style="color:gray;">, </span><span style="color:black;">7698</span><span style="color:gray;">, </span><span style="color:black;">1250</span><span style="color:gray;">, </span><span style="color:black;">500</span><span style="color:gray;">, </span><span style="color:black;">30</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7566</span><span style="color:gray;">, </span><span style="color:red;">'JONES'</span><span style="color:gray;">, </span><span style="color:red;">'MANAGER'</span><span style="color:gray;">, </span><span style="color:black;">7839</span><span style="color:gray;">, </span><span style="color:black;">2975</span><span style="color:gray;">, NULL, </span><span style="color:black;">20</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7654</span><span style="color:gray;">, </span><span style="color:red;">'MARTIN'</span><span style="color:gray;">, </span><span style="color:red;">'SALESMAN'</span><span style="color:gray;">, </span><span style="color:black;">7698</span><span style="color:gray;">, </span><span style="color:black;">1250</span><span style="color:gray;">, </span><span style="color:black;">1400</span><span style="color:gray;">, </span><span style="color:black;">30</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7698</span><span style="color:gray;">, </span><span style="color:red;">'BLAKE'</span><span style="color:gray;">, </span><span style="color:red;">'MANAGER'</span><span style="color:gray;">, </span><span style="color:black;">7839</span><span style="color:gray;">, </span><span style="color:black;">2850</span><span style="color:gray;">, NULL, </span><span style="color:black;">30</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7782</span><span style="color:gray;">, </span><span style="color:red;">'CLARK'</span><span style="color:gray;">, </span><span style="color:red;">'MANAGER'</span><span style="color:gray;">, </span><span style="color:black;">7839</span><span style="color:gray;">, </span><span style="color:black;">2450</span><span style="color:gray;">, NULL, </span><span style="color:black;">10</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7788</span><span style="color:gray;">, </span><span style="color:red;">'SCOTT'</span><span style="color:gray;">, </span><span style="color:red;">'ANALYST'</span><span style="color:gray;">, </span><span style="color:black;">7566</span><span style="color:gray;">, </span><span style="color:black;">3000</span><span style="color:gray;">, NULL, </span><span style="color:black;">20</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7839</span><span style="color:gray;">, </span><span style="color:red;">'KING'</span><span style="color:gray;">, </span><span style="color:red;">'PRESIDENT'</span><span style="color:gray;">, NULL, </span><span style="color:black;">5000</span><span style="color:gray;">, NULL, </span><span style="color:black;">10</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7844</span><span style="color:gray;">, </span><span style="color:red;">'TURNER'</span><span style="color:gray;">, </span><span style="color:red;">'SALESMAN'</span><span style="color:gray;">, </span><span style="color:black;">7698</span><span style="color:gray;">, </span><span style="color:black;">1500</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">, </span><span style="color:black;">30</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7876</span><span style="color:gray;">, </span><span style="color:red;">'ADAMS'</span><span style="color:gray;">, </span><span style="color:red;">'CLERK'</span><span style="color:gray;">, </span><span style="color:black;">7788</span><span style="color:gray;">, </span><span style="color:black;">1100</span><span style="color:gray;">, NULL, </span><span style="color:black;">20</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7900</span><span style="color:gray;">, </span><span style="color:red;">'JAMES'</span><span style="color:gray;">, </span><span style="color:red;">'CLERK'</span><span style="color:gray;">, </span><span style="color:black;">7698</span><span style="color:gray;">, </span><span style="color:black;">950</span><span style="color:gray;">, NULL, </span><span style="color:black;">30</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7902</span><span style="color:gray;">, </span><span style="color:red;">'FORD'</span><span style="color:gray;">, </span><span style="color:red;">'ANALYST'</span><span style="color:gray;">, </span><span style="color:black;">7566</span><span style="color:gray;">, </span><span style="color:black;">3000</span><span style="color:gray;">, NULL, </span><span style="color:black;">20</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7934</span><span style="color:gray;">, </span><span style="color:red;">'MILLER'</span><span style="color:gray;">, </span><span style="color:red;">'CLERK'</span><span style="color:gray;">, </span><span style="color:black;">7782</span><span style="color:gray;">, </span><span style="color:black;">1300</span><span style="color:gray;">, NULL, </span><span style="color:black;">10</span><span style="color:gray;">);<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">EmployeeRecord</span><span style="color:gray;">; </span></code></p>
<p style="text-align:justify;">Now run following query in SQL Server:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">EmpNo</span><span style="color:gray;">, </span><span style="color:black;">EmpName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Salery </span><span style="color:gray;">&gt; </span><span style="color:black;">1000<br />
</span><span style="color:blue;">EXCEPT<br />
SELECT </span><span style="color:black;">EmpNo</span><span style="color:gray;">, </span><span style="color:black;">EmpName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Salery </span><span style="color:gray;">&gt; </span><span style="color:black;">2000<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">EmpName</span><span style="color:gray;">; </span></code>
</p>
<p style="text-align:justify;">Now run following query in Oracle:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">EmpNo</span><span style="color:gray;">, </span><span style="color:black;">EmpName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Salery </span><span style="color:gray;">&gt; </span><span style="color:black;">1000<br />
</span><span style="color:blue;">MINUS<br />
SELECT </span><span style="color:black;">EmpNo</span><span style="color:gray;">, </span><span style="color:black;">EmpName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">EmployeeRecord<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Salery </span><span style="color:gray;">&gt; </span><span style="color:black;">2000<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">EmpName</span><span style="color:gray;">; </span></code>
</p>
<p style="text-align:justify;">You will find that both the query will return you same results.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/ExceptMinus.jpg" alt="" width="484" height="383" /></p>
<p style="text-align:justify;">Drop the table in SQL Server and Oracle as we are done with example.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DROP TABLE </span><span style="color:black;">EmployeeRecord</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;">This proves that EXCEPT clause in SQL Server is same as MINUS clause in Oracle.</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>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/791/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/791/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/791/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=791&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/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/blogfolder/ExceptMinus.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2005 &#8211; Difference Between INTERSECT and INNER JOIN &#8211; INTERSECT vs. INNER JOIN</title>
		<link>http://blog.sqlauthority.com/2008/08/03/sql-server-2005-difference-between-intersect-and-inner-join-intersect-vs-inner-join/</link>
		<comments>http://blog.sqlauthority.com/2008/08/03/sql-server-2005-difference-between-intersect-and-inner-join-intersect-vs-inner-join/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 01:30:21 +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://sqlauthority.wordpress.com/?p=772</guid>
		<description><![CDATA[INTERSECT operator in SQL Server 2005 is used to retrieve the common records from both the left and the right query of the Intersect Operator. INTERSECT operator returns almost same results as INNER JOIN clause many times.
When using INTERSECT operator the number and the order of the columns must be the same in all queries [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=772&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">INTERSECT operator in SQL Server 2005 is used to retrieve the common records from both the left and the right query of the Intersect Operator. INTERSECT operator returns almost same results as INNER JOIN clause many times.</p>
<p style="text-align:justify;">When using INTERSECT operator the number and the order of the columns must be the same in all queries as well data type must be compatible.</p>
<p style="text-align:justify;">Let us see understand how INTERSECT and INNER JOIN are related.We will be using AdventureWorks database to demonstrate our example.</p>
<p style="text-align:justify;"><em><strong>Example 1: Simple Example of INTERSECT</strong></em></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">EmployeeID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">3</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INTERSECT<br />
SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">EmployeeID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">5</span><span style="color:gray;">)</span></code>
</p>
<p style="text-align:justify;"><em>ResultSet:</em></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/IntersectWithWhere.gif" alt="" width="500" height="261" /></p>
<p style="text-align:justify;"><em>Explanation:</em><br />
The ResultSet  shows  the  EmployeeID  which  are  common in both the Queries, i.e  2 and 3.
</p>
<p style="text-align:justify;"><em><strong>Example 2:  Using simple </strong></em><em><strong>INTERSECT</strong></em><em><strong>between two tables.</strong></em></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">VendorID</span><span style="color:gray;">,</span><span style="color:black;">ModifiedDate<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Purchasing.VendorContact<br />
</span><span style="color:blue;">INTERSECT<br />
SELECT </span><span style="color:black;">VendorID</span><span style="color:gray;">,</span><span style="color:black;">ModifiedDate<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Purchasing.VendorAddress</span></code>
</p>
<p style="text-align:justify;"><em>ResultSet:</em></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/SimpleIntersect.gif" alt="" width="427" height="401" /><br />
<em><br />
Explanation:</em><br />
The Resultset shows the records that are common in both the tables. It shows 104 common records between the tables.
</p>
<p style="text-align:justify;"><em><strong>Example 3:  Using INNER JOIN.</strong></em></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">va.VendorID</span><span style="color:gray;">,</span><span style="color:black;">va.ModifiedDate<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Purchasing.VendorContact vc<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Purchasing.VendorAddress va </span><span style="color:blue;">ON </span><span style="color:black;">vc.VendorID </span><span style="color:blue;">= </span><span style="color:black;">va.VendorID<br />
</span><span style="color:gray;">AND </span><span style="color:black;">vc.ModifiedDate </span><span style="color:blue;">= </span><span style="color:black;">va.ModifiedDate</span></code>
</p>
<p style="text-align:justify;"><em>ResultSet:</em></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/IntersectWithoutDistinct.gif" alt="" width="637" height="345" /><br />
<em></em></p>
<p style="text-align:justify;"><em>Exlanation :</em><br />
The resultset displays all the records which are common to both the tables. Additionally in example above INNER JOIN retrieves all the records from the left table and all the records from the right table. Carefully observing we can notice many of the records as duplicate records. When INNER JOIN is used it gives us duplicate records, but that is not in the case of INTERSECT operator.
</p>
<p style="text-align:justify;"><strong><em>Example 4:  Using INNER JOIN with Distinct.</em></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT DISTINCT </span><span style="color:black;">va.VendorID</span><span style="color:gray;">,</span><span style="color:black;">va.ModifiedDate<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Purchasing.VendorContact vc<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Purchasing.VendorAddress va </span><span style="color:blue;">ON </span><span style="color:black;">vc.VendorID </span><span style="color:blue;">= </span><span style="color:black;">va.VendorID<br />
</span><span style="color:gray;">AND </span><span style="color:black;">vc.ModifiedDate </span><span style="color:blue;">= </span><span style="color:black;">va.ModifiedDate</span></code>
</p>
<p style="text-align:justify;"><em>ResultSet:</em></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/IntersectWithDistinct.gif" alt="" width="645" height="349" /></p>
<p style="text-align:justify;"><em>Explanation:</em><br />
The resultset in this example does not contain any duplicate records as DISTINCT clause is used in SELECT statement. DISTINCT removes the duplicate rows and final result in this example is exactly same as example 2 described above. In this way, INNER JOIN can simulate with INTERSECT when used with DISTINCT.
</p>
<p style="text-align:justify;">Summary :</p>
<p style="text-align:justify;"><strong>INNER JOIN can simulate with INTERSECT when used with DISTINCT.</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>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/772/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/772/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/772/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=772&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/08/03/sql-server-2005-difference-between-intersect-and-inner-join-intersect-vs-inner-join/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/blogfolder/IntersectWithWhere.gif" medium="image" />

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

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

		<media:content url="http://www.pinaldave.com/blogfolder/IntersectWithDistinct.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Effect of Order of Join In Query</title>
		<link>http://blog.sqlauthority.com/2008/08/02/sql-server-effect-of-order-of-join-in-query/</link>
		<comments>http://blog.sqlauthority.com/2008/08/02/sql-server-effect-of-order-of-join-in-query/#comments</comments>
		<pubDate>Sat, 02 Aug 2008 01:30:27 +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>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=767</guid>
		<description><![CDATA[Let us try to understand this subject with example.
We will use Adventurworks database for this purpose. Table which we will be using are HumanResources.Employee (290 rows), HumanResources.EmployeeDepartmentHistory (296 rows) and HumanResources.Department (16 rows).
We will be running following two queries and observe the output. In the resultset the order of first column (EmployeeID) is different in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=767&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Let us try to understand this subject with example.</p>
<p style="text-align:justify;">We will use Adventurworks database for this purpose. Table which we will be using are HumanResources.Employee <strong>(290 rows)</strong>, HumanResources.EmployeeDepartmentHistory <strong>(296 rows)</strong> and HumanResources.Department <strong>(16 rows)</strong>.</p>
<p style="text-align:justify;">We will be running following two queries and observe the output. In the resultset the order of first column (EmployeeID) is different in both the cases when whole resultset is same. When compared both the results they are same but the order of rows is different in both the resultset.</p>
<p style="text-align:justify;">Query 1 :</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">he.EmployeeID</span><span style="color:gray;">, </span><span style="color:black;">he.Title</span><span style="color:gray;">, </span><span style="color:black;">hd.Name</span><span style="color:gray;">, </span><span style="color:black;">hd.GroupName</span><span style="color:gray;">, </span><span style="color:black;">hdh.StartDate<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee he<br />
</span><span style="color:magenta;">LEFT </span><span style="color:blue;">JOIN </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory hdh<br />
</span><span style="color:blue;">ON </span><span style="color:black;">he.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">hdh.EmployeeID<br />
</span><span style="color:magenta;">RIGHT </span><span style="color:blue;">JOIN </span><span style="color:black;">HumanResources.Department hd<br />
</span><span style="color:blue;">ON </span><span style="color:black;">hd.DepartmentID </span><span style="color:blue;">= </span><span style="color:black;">hdh.DepartmentID </span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/LeftJoinOrderQ1.gif" alt="" width="500" height="324" /></p>
<p style="text-align:justify;">Query 2:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">he.EmployeeID</span><span style="color:gray;">, </span><span style="color:black;">he.Title</span><span style="color:gray;">, </span><span style="color:black;">hd.Name</span><span style="color:gray;">, </span><span style="color:black;">hd.GroupName</span><span style="color:gray;">, </span><span style="color:black;">hdh.StartDate<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Employee he<br />
</span><span style="color:magenta;">INNER </span><span style="color:blue;">JOIN </span><span style="color:black;">HumanResources.EmployeeDepartmentHistory hdh<br />
</span><span style="color:blue;">ON </span><span style="color:black;">he.EmployeeID </span><span style="color:blue;">= </span><span style="color:black;">hdh.EmployeeID<br />
</span></code><code style="font-size:12px;"><span style="color:magenta;">INNER</span></code><code style="font-size:12px;"><span style="color:magenta;"> </span><span style="color:blue;">JOIN </span><span style="color:black;">HumanResources.Department hd<br />
</span><span style="color:blue;">ON </span><span style="color:black;">hd.DepartmentID </span><span style="color:blue;">= </span><span style="color:black;">hdh.DepartmentID </span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/InnerJoinOrderQ2.gif" alt="" width="500" height="309" /></p>
<p style="text-align:justify;">Now the interesting part of understand why the order is different.</p>
<p style="text-align:justify;">I would like my readers and participate here and give me their opinion. There are many reasons for this happening but what I think is <strong>&#8220;Effect of Index&#8221;</strong>.</p>
<p style="text-align:justify;">When we use Left or Right Join, We have a base table Employee and the records are order by the primary key i.e The EmployeeID of the base table by default. But when we use the Inner Join, then the table having smallest number of records are used to order by. Here in our above example, the HumanResources.Department has 16 Records. So  the records are sorted by the departmentId of the HumanResources.Department table.</p>
<p style="text-align:justify;">Let me know your opinion and also let me know if you like this kind of small articles.</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>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/767/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/767/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/767/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/767/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/767/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/767/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/767/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/767/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/767/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/767/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/767/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/767/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=767&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/08/02/sql-server-effect-of-order-of-join-in-query/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/blogfolder/LeftJoinOrderQ1.gif" medium="image" />

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