<?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 Puzzle</title>
	<atom:link href="http://blog.sqlauthority.com/category/sql-puzzle/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 Puzzle</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
			<item>
		<title>SQL SERVER &#8211; Size of Index Table &#8211; A Puzzle to Find Index Size for Each Index on Table</title>
		<link>http://blog.sqlauthority.com/2009/11/18/sql-server-size-of-index-table-a-puzzle-to-find-index-size-for-each-index-on-table/</link>
		<comments>http://blog.sqlauthority.com/2009/11/18/sql-server-size-of-index-table-a-puzzle-to-find-index-size-for-each-index-on-table/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 01:30:16 +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 Index]]></category>
		<category><![CDATA[SQL Puzzle]]></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[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7388</guid>
		<description><![CDATA[It is very easy to find out some basic details of any table using the following Stored Procedure.
USE AdventureWorks
GO
EXEC sp_spaceused [HumanResources.Shift]
GO
Above query will return following resultset

The above SP provides basic details such as rows, data size in table, and Index size of all the indexes on the table.
If we look at this carefully, a total [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7388&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">It is very easy to find out some basic details of any table using the following Stored Procedure.</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;">EXEC </span><span style="color:darkred;">sp_spaceused </span><span style="color:black;">[HumanResources.Shift]<br />
GO</span></code></p>
<p style="text-align:justify;">Above query will return following resultset</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/sp_spaceused1.jpg" alt="" width="369" height="153" /></p>
<p style="text-align:justify;">The above SP provides basic details such as rows, data size in table, and Index size of all the indexes on the table.</p>
<p style="text-align:justify;">If we look at this carefully, a total of three indexes can be found on the table HumanResources.Shift.</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;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.indexes<br />
</span><span style="color:blue;">WHERE </span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'HumanResources.Shift'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">The above query will give result with query listing all the index on the table.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexspace.jpg" alt="" width="500" height="248" /></p>
<p style="text-align:justify;">There is a small puzzle for all of you here. The puzzle is to write a query that will return the size for each index that is listed in above query. We need a query that will return an additional column in the above listed query and it should contain the size of the index. In our case, we will have three different sizes, which should add up to a total of 40 KB as shown in earlier query, where the total size is displayed.</p>
<p style="text-align:justify;">I will publish the solution with due credit 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, Readers Question, SQL, SQL Authority, SQL Index, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7388/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7388&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/11/18/sql-server-size-of-index-table-a-puzzle-to-find-index-size-for-each-index-on-table/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/sp_spaceused1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/indexspace.jpg" medium="image" />
	</item>
		<item>
		<title>SQLAuthority News &#8211; First Editorial &#8211; T-SQL Challenges Beginners</title>
		<link>http://blog.sqlauthority.com/2009/10/14/sqlauthority-news-first-editorial-t-sql-challenges-beginners/</link>
		<comments>http://blog.sqlauthority.com/2009/10/14/sqlauthority-news-first-editorial-t-sql-challenges-beginners/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 01:30:05 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[T-SQL Challenge]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7128</guid>
		<description><![CDATA[I would like to welcome all of you to very first editorial for T-SQL Challenges for Beginners. T-SQL Challenges began with the aim to help community to come out of regular mind set of just reading articles online. There is plenty of reading material available online, but there are very few that can make us [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7128&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I would like to welcome all of you to very first editorial for <em>T-SQL Challenges for Beginners</em>. <a href="http://www.tsqlchallenges.com/" target="_blank">T-SQL Challenges</a> began with the aim to help community to come out of regular mind set of just reading articles online. There is plenty of reading material available online, but there are very few that can make us use our brain cells.</p>
<p style="text-align:justify;"><a href="http://www.tsqlchallenges.com/" target="_blank">T-SQL Challenges</a> are very well received in community, and today, we are receiving more than 200 responses for every challenge in a very short time. The real challenge is how to keep everybody involved. <em>T-SQL Challenges</em> is focused and encourage experts to bring out the best from them. <em>T-SQL challenges for Beginners</em> share the same enthusiasm from everyone.</p>
<p style="text-align:justify;">Of course, this challenge can be attempted by everyone and just not beginners. However, just like we say “There is a child in everyone,” – in the exactly same way, I would like to say that “There is a beginner in everyone.” We may be expert in certain areas, but quite often we face a barrier to attempt something new and different. I believe that <em>T-SQL Challenges</em> can initiate the Beginners to break that very barrier. Note that the term beginner is used in a broader sense.</p>
<p style="text-align:justify;">Here, Beginners refer to the new innovators. A problem can be easily resolved by writing simple T-SQL; however, what is most important is that an innovative thought can resolve an age old issue. T-SQL has many dimensions, and each dimension is equally important. These dimensions are creativity, performance, data modeling techniques, readability and many more. A solution, which can be simply solved by cursor, can be re-written using a set-based solution; this will lead to the scoring of some points in the area of performance and best practices.</p>
<p style="text-align:justify;">I want to promise one more thing here that just like T-SQL Challenges, this challenge series for beginners will be focused on building a community, which helps each other. This is an open community, and everyone is welcome to stay as long as they wish. From my experience, I know that this is addictive, but I would let you decide the same.</p>
<p style="text-align:justify;">What can you do to help this community effort?</p>
<ul>
<li>Participate in challenge and solve them.</li>
<li>Read the solutions from others and learn new tricks.</li>
<li> Spread the word for the challenges.</li>
<li>Participate by submitting new challenges.</li>
<li>Last but not least, send your feedback.</li>
</ul>
<p style="text-align:justify;">One more very interesting point is that if you win the challenge and if you present a very innovative solution, you will receive a special certificate from <em>T-SQL challenge</em> founder <a href="http://beyondrelational.com/members/jacobbr/default.aspx" target="_blank">Jacob Sebastian</a> and <a href="http://beyondrelational.com/members/pinaldave/default.aspx" target="_blank">myself</a>. On my side, I promise to feature the same as an article on my blog <a href="http://blog.sqlauthority.com/" target="_blank">SQLAuthority.com</a>.</p>
<p style="text-align:justify;">Let me see what you have got? Over to <strong><a href="http://beyondrelational.com/blogs/tcb/archive/2009/09/22/first-editorial-t-sql-challenges-beginners.aspx" target="_blank">very first challenge now</a></strong>&#8230;</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 Puzzle, SQL Query, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: T-SQL Challenge <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7128/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7128&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/14/sqlauthority-news-first-editorial-t-sql-challenges-beginners/feed/</wfw:commentRss>
		<slash:comments>0</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 &#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; Interesting Observation &#8211; Execution Plan and Results of Aggregate Concatenation Queries</title>
		<link>http://blog.sqlauthority.com/2009/09/29/sql-server-interesting-observation-execution-plan-and-results-of-aggregate-concatenation-queries/</link>
		<comments>http://blog.sqlauthority.com/2009/09/29/sql-server-interesting-observation-execution-plan-and-results-of-aggregate-concatenation-queries/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 01:30:32 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL View]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6930</guid>
		<description><![CDATA[Working with SQL Server has never seems to be monotonous – no matter how long one has worked with it. Quite often, I come across some excellent comments that I feel like acknowledging them as blog posts. Recently, I wrote an article on SQL SERVER – Execution Plan and Results of Aggregate Concatenation Queries Depend [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6930&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Working with SQL Server has never seems to be monotonous – no matter how long one has worked with it. Quite often, I come across some excellent comments that I feel like acknowledging them as blog posts. Recently, I wrote an article on <a href="http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/" target="_blank"><strong>SQL SERVER – Execution Plan and Results of Aggregate Concatenation Queries Depend Upon Expression Location</strong></a>, which is well received in community.</p>
<p style="text-align:justify;">Before you read this article further, I request you to read <a href="http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/" target="_blank">original article</a>. I received very interesting comments from Bob on the blog, where he explained why this is happening. Further, he talked about a similar kind of example. Let us first read his comment, and then we will execute his example and see the resultset.</p>
<p style="text-align:justify;">Comment from <strong><a href="http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56103" target="_blank">Bob</a></strong></p>
<p style="padding-left:30px;text-align:justify;">pinaldave,</p>
<p style="padding-left:30px;text-align:justify;">If you want to see what’s going on here, I think you need to shift your point of view from an implementation-centric view to an ANSI point of view. ANSI does not guarantee processing order. Figure 2 is interesting, but it will be potentially misleading if you don’t understand the ANSI rule-set SQL Server operates under in most cases. Implementation thinking can certainly be useful at times when you really need that multi-million row query to finish before the backups fire off, but in this case, it’s counterproductive to understanding what is going on.</p>
<p style="padding-left:30px;text-align:justify;">First off, your statements all return a single row, which were properly sorted according to your ORDER BY expression; which is to say that there was really no sorting to be done because only a single row is returned. So let’s forget about the ORDER BY. Forget that the execution plan shows 2 rows being sorted — that’s implementation specific. A different vendor’s product could have an optimizer smart enough to see that only a single row can ever be returned from this query and therefore, avoid the unnecessary sort altogether.</p>
<p style="padding-left:30px;text-align:justify;">Secondly, you are not guaranteed that the string concatenation will occur in any particular order. The ORDER BY clause only pertains to the final results from an ANSI perspective (at least, for these queries). MS SQL Server is right in line with ANSI and will not guarantee you anything different. Don’t expect to get AB in every case.</p>
<p style="padding-left:30px;text-align:justify;">Finally, SQL works using relational sets, so the construct varchar = varchar + varchar forms a relational set that is evaluated accordingly. I have seen this type of construct used many times and it works sometimes – until it doesn’t. This is why you get only B in one example where I suspect you wanted AB. After thinking about it a bit, I built this example which I hope is just different enough to show you the same mechanism is at work here:<br />
DECLARE @sum INT<br />
SELECT @sum = 0<br />
SELECT @sum = @sum + num FROM (<br />
SELECT 1 [num] UNION SELECT 2 [num]<br />
) [a] ORDER BY (num * num)<br />
SELECT @sum [Is it 3]</p>
<p style="padding-left:30px;text-align:justify;">I tried to show two things here. The first is that the ORDER BY isn’t doing what you think it’s doing with respect to the order of concatenation in your query. The order here is nonsensical. The second thing I think the example shows is that 1+2 doesn’t equal 3 with this @ = @ + col construct in all cases. It’s a bad construct to use. Remove the ORDER BY and you get 3, leave it in and you get 2. Heck, someone else could get 1 in theory. There’s a case I see all the time where this is used, but I can’t quite recall it. I’ll post it if I think of it again.</p>
<p style="text-align:justify;">I must admit that he has made excellent point and explained the concept very well as well. Just for clarity, we will see the implementation of the example mentioned by him to understand how it works.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@sum </span><span style="color:blue;">INT<br />
SELECT </span><span style="color:#434343;">@sum </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@sum </span><span style="color:blue;">= </span><span style="color:#434343;">@sum </span><span style="color:gray;">+ </span><span style="color:black;">num </span><span style="color:blue;">FROM </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1 [num] </span><span style="color:blue;">UNION SELECT </span><span style="color:black;">2 [num]<br />
</span><span style="color:gray;">) </span><span style="color:black;">[a] </span><span style="color:blue;">ORDER BY </span><span style="color:gray;">(</span><span style="color:black;">num </span><span style="color:gray;">* </span><span style="color:black;">num</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@sum </span><span style="color:black;">WithOrderBy<br />
GO<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@sum </span><span style="color:blue;">INT<br />
SELECT </span><span style="color:#434343;">@sum </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@sum </span><span style="color:blue;">= </span><span style="color:#434343;">@sum </span><span style="color:gray;">+ </span><span style="color:black;">num </span><span style="color:blue;">FROM </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1 [num] </span><span style="color:blue;">UNION SELECT </span><span style="color:black;">2 [num]<br />
</span><span style="color:gray;">) </span><span style="color:black;">[a]<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@sum </span><span style="color:black;">WithoutOrderBy<br />
GO</span></code></p>
<p style="text-align:justify;">Let us see the result when we run the above two queries with and without ORDER BY clause.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/orderby.jpg" alt="" width="370" height="442" /></p>
<p style="text-align:justify;">Again, it is interesting to see how results are different from each other when there is only change of inclusion of ORDER BY clause. Bob has explained why this is happening in his comment earlier. He also explains further in another comment in the same article that.</p>
<p style="text-align:justify;">Comment from <strong><a href="http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56106" target="_blank">Bob</a></strong>:</p>
<p style="padding-left:30px;text-align:justify;">One more item to note is that if you built:</p>
<p style="padding-left:30px;text-align:justify;">SELECT @Str0 = @Str0 + C1 FROM T1</p>
<p style="padding-left:30px;text-align:justify;">into a table expression (i.e. VIEW, FUNCTION, FROM (SELECT)), you may get the results you want over and over until the one day where you call the view or function with the combination of syntax that triggers the unexpected behavoir.</p>
<p style="padding-left:30px;text-align:justify;"><em>pinaldave,</em></p>
<p style="padding-left:30px;text-align:justify;"><em><strong>On my final point</strong>, I think it’s unclear/weak. I’d like to add that the most clear reason as to why you can’t expect to get AB or BA, but might in fact get just A or just B, is that the construct @=@ +col is non-deterministic and therefore, for the same range of input values, is not guaranteed to produce the same result in every instance.</em></p>
<p style="text-align:justify;"><strong>Bob</strong>, Thank you very much for excellent explanation and your participation in the community.</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, SQL, SQL Authority, SQL Index, SQL Puzzle, SQL Query, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology Tagged: SQL View <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6930/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6930/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6930/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6930/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6930/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6930/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6930&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/29/sql-server-interesting-observation-execution-plan-and-results-of-aggregate-concatenation-queries/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/orderby.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Interesting Observation &#8211; Index on Index View Used in Similar Query</title>
		<link>http://blog.sqlauthority.com/2009/09/24/sql-server-interesting-observation-index-on-index-view-used-in-similar-query/</link>
		<comments>http://blog.sqlauthority.com/2009/09/24/sql-server-interesting-observation-index-on-index-view-used-in-similar-query/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 01:30:36 +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 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[SQL White Papers]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6895</guid>
		<description><![CDATA[Recently, I was working on an optimization project for one of the large organizations. While working on one of the queries, we came across a very interesting observation. We found that there was a query on the base table and when the query was run, it used the index, which did not exist in the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6895&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Recently, I was working on an optimization project for one of the large organizations. While working on one of the queries, we came across a very interesting observation. We found that there was a query on the base table and when the query was run, it used the index, which did not exist in the base table. On careful examination, we found that the query was using the index that was on another view. This was very interesting as I have personally never experienced a scenario like this. In simple words, &#8220;Query on the base table can use the index created on the indexed view of the same base table.&#8221;</p>
<p style="text-align:justify;">If you have skipped the first paragraph, I suggest you go over the first paragraph one more time and try to understand the intended meaning. According to me, this is a very interesting observation and we rarely come across such an instance. I have made very small attempt to recreate this particular behavior.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">tempdb<br />
GO<br />
</span><span style="color:green;">-- Create Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">IndexViewAggr </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Insert One Hundred Thousand Records<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">IndexViewAggr </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">100000 ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">a.name</span><span style="color:gray;">) </span><span style="color:black;">RowID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.all_objects a<br />
</span><span style="color:gray;">CROSS </span><span style="color:blue;">JOIN </span><span style="color:black;">sys.all_objects b<br />
GO<br />
</span><span style="color:green;">-- Create View on the table<br />
</span><span style="color:blue;">CREATE VIEW </span><span style="color:black;">ViewAggr </span><span style="color:blue;">WITH </span><span style="color:black;">SCHEMABINDING<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">COUNT_BIG</span><span style="color:gray;">(*) </span><span style="color:black;">CountID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.IndexViewAggr<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID </span><span style="color:gray;">BETWEEN </span><span style="color:black;">1 </span><span style="color:gray;">AND </span><span style="color:black;">1000<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ID<br />
GO<br />
</span><span style="color:green;">-- Following query is very identical to View<br />
-- Run the query on base table<br />
-- Please note that in execution plan there is Table Scan<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">COUNT_BIG</span><span style="color:gray;">(*) </span><span style="color:black;">CountID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.IndexViewAggr<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID </span><span style="color:gray;">BETWEEN </span><span style="color:black;">1 </span><span style="color:gray;">AND </span><span style="color:black;">1000<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ID<br />
GO<br />
</span><span style="color:green;">-- Create Index on the View<br />
-- Please note that this index is on View and not on table<br />
</span><span style="color:blue;">CREATE UNIQUE CLUSTERED INDEX </span><span style="color:black;">IndexView </span><span style="color:blue;">ON </span><span style="color:black;">ViewAggr</span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Run the query on base table again<br />
-- Please note that in execution plan there is Index Scan<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID</span><span style="color:gray;">, </span><span style="color:black;">COUNT_BIG</span><span style="color:gray;">(*) </span><span style="color:black;">CountID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.IndexViewAggr<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ID </span><span style="color:gray;">BETWEEN </span><span style="color:black;">1 </span><span style="color:gray;">AND </span><span style="color:black;">1000<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">ID<br />
GO<br />
</span><span style="color:green;">-- Please note that there are difference in "Query Cost"<br />
-- Clean up Database<br />
</span><span style="color:blue;">DROP VIEW </span><span style="color:black;">ViewAggr<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">IndexViewAggr<br />
GO</span></code>
</p>
<p style="text-align:justify;">Now let us quickly observe two performance comparison of two of the selects. One object is created before the creation of the index over the view, and the other object is created after the creation of the index over the view. Please note that we are talking about two different objects here. Let me explain this schematically.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/queryindex.jpg" alt="" width="500" height="513" /></p>
<p style="text-align:justify;">This is a very interesting behavior as the index created on view affects the query that runs on the base table. Let me show you the execution plan of the query before and after index creation.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexview1.jpg" alt="" width="500" height="285" /></p>
<p style="text-align:justify;">From this example, it is very clear that there is a very big difference between the query costs when the index is created on the table. In fact, there is a very simple explanation of this behavior in the white paper <a href="http://msdn.microsoft.com/en-us/library/dd171921.aspx" target="_blank">Improving Performance with SQL Server 2008 Indexed Views</a>.</p>
<p style="text-align:justify;">The reason for this behavior is that the query optimizer works when it checks for the index on view and finds that the execution plan of the query is similar to that of the view.</p>
<p style="text-align:justify;">Let me know your thoughts about this article. I do understand that this concept is slightly complicated. Further, this type of behavior is rarely encountered in real world. I hope that this article will give you a better insight to such a situation.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<p style="text-align:justify;">
Posted in Pinal Dave, SQL, SQL Authority, SQL Index, SQL Optimization, SQL Performance, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL White Papers, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6895/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6895&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/24/sql-server-interesting-observation-index-on-index-view-used-in-similar-query/feed/</wfw:commentRss>
		<slash:comments>14</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/queryindex.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/indexview1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Execution Plan and Results of Aggregate Concatenation Queries Depend Upon Expression Location</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/</link>
		<comments>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 01:30:11 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Documentation]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786</guid>
		<description><![CDATA[I was reading the blog of Ward Pond, and I came across another note of Microsoft. I really found it very interesting. The given explanation was very simple; however, I would like to rewrite it again.
Let us execute the following script. This script inserts two values &#8216;A&#8217; and &#8216;B&#8217; in the table and outputs a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6786&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I was reading the blog of <a href="http://blogs.technet.com/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx" target="_blank">Ward Pond</a>, and I came across <a href="http://support.microsoft.com/default.aspx?scid=287515" target="_blank">another note</a> of Microsoft. I really found it very interesting. The given explanation was very simple; however, I would like to rewrite it again.</p>
<p style="text-align:justify;">Let us execute the following script. This script inserts two values &#8216;A&#8217; and &#8216;B&#8217; in the table and outputs a simple code to concatenate each other to produce the result &#8216;AB&#8217;.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">IF </span><span style="color:gray;">EXISTS( </span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">sysobjects </span><span style="color:blue;">WHERE </span><span style="color:black;">name </span><span style="color:blue;">= </span><span style="color:red;">'T1' </span><span style="color:gray;">)<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">T1<br />
GO<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">T1</span><span style="color:gray;">( </span><span style="color:black;">C1 </span><span style="color:magenta;">NCHAR</span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">)  )<br />
</span><span style="color:blue;">INSERT </span><span style="color:black;">T1 </span><span style="color:blue;">VALUES</span><span style="color:gray;">( </span><span style="color:red;">'A' </span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT </span><span style="color:black;">T1 </span><span style="color:blue;">VALUES</span><span style="color:gray;">( </span><span style="color:red;">'B' </span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Str0 </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">4</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Str0 </span><span style="color:blue;">= </span><span style="color:red;">''<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@Str0 </span><span style="color:blue;">= </span><span style="color:#434343;">@Str0 </span><span style="color:gray;">+ </span><span style="color:black;">C1 </span><span style="color:blue;">FROM </span><span style="color:black;">T1 </span><span style="color:blue;">ORDER BY </span><span style="color:black;">C1<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@Str0 </span><span style="color:blue;">AS </span><span style="color:black;">Result<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">T1</span></code>
</p>
<p style="text-align:justify;">The code to concatenating any two string is very simple which is used three times in following example.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">IF </span><span style="color:gray;">EXISTS( </span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">sysobjects </span><span style="color:blue;">WHERE </span><span style="color:black;">name </span><span style="color:blue;">= </span><span style="color:red;">'T1' </span><span style="color:gray;">)<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">T1<br />
GO<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">T1</span><span style="color:gray;">( </span><span style="color:black;">C1 </span><span style="color:magenta;">NCHAR</span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">)  ) </span><span style="color:blue;"><br />
INSERT </span><span style="color:black;">T1 </span><span style="color:blue;">VALUES</span><span style="color:gray;">( </span><span style="color:red;">'A' </span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT </span><span style="color:black;">T1 </span><span style="color:blue;">VALUES</span><span style="color:gray;">( </span><span style="color:red;">'B' </span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Str0 </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">4</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Str1 </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">4</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Str2 </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">4</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Str0 </span><span style="color:blue;">= </span><span style="color:red;">''<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Str1 </span><span style="color:blue;">= </span><span style="color:red;">''<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Str2 </span><span style="color:blue;">= </span><span style="color:red;">''<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@Str0 </span><span style="color:blue;">= </span><span style="color:#434343;">@Str0 </span><span style="color:gray;">+ </span><span style="color:black;">C1 </span><span style="color:blue;">FROM </span><span style="color:black;">T1 </span><span style="color:blue;">ORDER BY </span><span style="color:black;">C1<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@Str1 </span><span style="color:blue;">= </span><span style="color:#434343;">@Str1 </span><span style="color:gray;">+ </span><span style="color:black;">C1 </span><span style="color:blue;">FROM </span><span style="color:black;">T1 </span><span style="color:blue;">ORDER BY </span><span style="color:magenta;">LTRIM</span><span style="color:gray;">( </span><span style="color:magenta;">RTRIM</span><span style="color:gray;">( </span><span style="color:black;">C1 </span><span style="color:gray;">) )<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@Str2 </span><span style="color:blue;">= </span><span style="color:#434343;">@Str2 </span><span style="color:gray;">+ </span><span style="color:magenta;">LTRIM</span><span style="color:gray;">( </span><span style="color:magenta;">RTRIM</span><span style="color:gray;">( </span><span style="color:black;">C1 </span><span style="color:gray;">) ) </span><span style="color:blue;">FROM </span><span style="color:black;">T1 </span><span style="color:blue;">ORDER BY </span><span style="color:black;">C1<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@Str0 </span><span style="color:red;">'No functions applied to column.'<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@Str1 </span><span style="color:red;">'LTRIM() and RTRIM() applied to ORDER BY clause.'<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@Str2 </span><span style="color:red;">'SELECT list with LTRIM(RTRIM()) (Workaround)'<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">T1 </span></code>
</p>
<p style="text-align:justify;">Resultset of the above query is as follows.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/orderbyfun.jpg" alt="" width="500" height="276" /></p>
<p style="text-align:justify;">From this result, it is evident that the resultset where function is applied in ORDER BY clause gives the wrong result. When the same function is moved to SELECT clause, it gives the correct result. That is in one way very strange; however, this is how it is defined in SQL Server standard. The behavior of the function in ORDER BY is not defined anywhere in SQL documentation.</p>
<p style="text-align:justify;">The best practice is to avoid the usage of function in ORDER BY clause when string concatenation operations are executed.</p>
<p style="text-align:justify;">The reason for this behavior is that the use of function in ORDER BY clause will change the order of query execution and create an unexpected output.</p>
<p style="text-align:justify;">
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/orderbyfun1.jpg" alt="" width="488" height="240" /></p>
<p style="text-align:justify;">Let me know if you have any other example for the same or a better explanation.</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 Documentation, SQL Performance, SQL Puzzle, SQL Query, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6786/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6786&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

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

		<media:content url="http://www.pinaldave.com/bimg/orderbyfun1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Discussion &#8211; Effect of Missing Identity on System &#8211; Real World Scenario</title>
		<link>http://blog.sqlauthority.com/2009/08/11/sql-server-discussion-effect-of-missing-identity-on-system-real-world-scenario/</link>
		<comments>http://blog.sqlauthority.com/2009/08/11/sql-server-discussion-effect-of-missing-identity-on-system-real-world-scenario/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 01:30:13 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[DBA]]></category>
		<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 Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6524</guid>
		<description><![CDATA[About a week ago, SQL Server Expert, Imran Mohammed, provided a script, which will list all the missing identity values of a table in a database. In this post, I asked my readers if any could write a similar or better script. The results were interesting. While no one provided a new script, my question [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6524&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">About a week ago, SQL Server Expert, <strong>Imran Mohammed,</strong> provided a script, which will list all the missing identity values of a table in a database. In this <a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/">post</a>, I asked my readers if any could write a similar or better script. The results were interesting. While no one provided a new script, my question sparked a very active <a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/">discussion</a> that is still ongoing.</p>
<p style="text-align:justify;">When providing the script, Imran asked me if I knew of any specific circumstances in which this kind of query could be useful, as he could not think of an instance where it would be necessary to find a missing identity. I was unable to think of a single reason for listing missing identities in a table. I posted Imran&#8217;s script on the assumption that someone would come up with an improved script, but as mentioned earlier, nobody did. Instead, we have been able to follow a very interesting discussion on subject of the need, if any, for listing Missing Identity values.</p>
<p style="text-align:justify;">So, the question is this: &#8220;<strong><em>Do you know a real-world scenario where a Missing Identity value in any table can create problems?</em></strong>&#8220;</p>
<p style="text-align:justify;">I have already received some extremely interesting comments from many experts, and all have posed the above question in one form or another. At this moment, I am still trying to think of an example from my own experience, but have yet to find one. Imran has since come up with one good example. Here is what he and other experts have suggested so far.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/#comment-54117" target="_blank"><strong>Jacob Sebastian</strong></a> &#8211; IDENTITY values are not expected to be sequential and there are all chances of having missing identity values, the most common cause is transaction rollbacks.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/#comment-54139" target="_blank"><strong>Simon Worth</strong></a> &#8211; The identity column is basically just a random number – even though they come sequentially. A developer making an assumption that the next record inserted will have an identity that is 1 more than the last inserted record. And if this is the case – then there are flaws in the logic of the developer.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/#comment-54158" target="_blank"><strong>Jacob Sebastian</strong></a> &#8211; What if the value in my table is 1, 2, 3, 5, 6 etc where “4″ is missing from the sequence. So what is the importance of knowing whether a table has missing identity values or not?</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/#comment-54184" target="_blank"><strong>Imran Mohammed</strong></a> &#8211; If you use Identity property as your most unique column and Transaction Identifier, then definitely you would want to know why few transaction did not completely, Is there any specific fashion these transaction fails (Can be found out looking at missing values of identity)&#8230; Could be helpful to debug.</p>
<p style="text-align:justify;">Now it is your turn. Let us have your thoughts.</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, DBA, Pinal Dave, Readers Contribution, Readers Question, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Puzzle, SQL Query, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6524/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6524/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6524/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6524/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6524/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6524/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6524/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6524/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6524/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6524/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6524&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/08/11/sql-server-discussion-effect-of-missing-identity-on-system-real-world-scenario/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Puzzle &#8211; Write Script to Generate Primary Key and Foreign Key</title>
		<link>http://blog.sqlauthority.com/2009/07/23/sql-server-puzzle-write-script-to-generate-primary-key-and-foreign-key/</link>
		<comments>http://blog.sqlauthority.com/2009/07/23/sql-server-puzzle-write-script-to-generate-primary-key-and-foreign-key/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 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 Constraint and Keys]]></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[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Challenge]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6032</guid>
		<description><![CDATA[In one of my recent projects, a large database migration project, I confronted a peculiar situation. SQL Server tables were already moved from Database_Old to Database_New. However, all the Primary Key and Foreign Keys were yet to be moved from the old server to the new server.
Please note that this puzzle is to be solved [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6032&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">In one of my recent projects, a large database migration project, I confronted a peculiar situation. SQL Server tables were already moved from Database_Old to Database_New. However, all the Primary Key and Foreign Keys were yet to be moved from the old server to the new server.</p>
<p style="text-align:justify;">Please note that this puzzle is to be solved for <strong>SQL Server 2005</strong> or <strong>SQL Server 2008</strong>. As noted by Kuldip it is possible to do this in SQL Server 2000.</p>
<p style="text-align:justify;">In SQL Server Management Studio (SSMS), there is no option to script all the keys. If one is required to script keys they will have to manually script each key one at a time. If database has many tables, generating one key at a time can be a very intricate task. I want to throw a question to all of you if any of you have script for the same purpose.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/pkgen.jpg" alt="" width="500" height="364" /></p>
<p style="text-align:justify;">As per my opinion, I think the challenge is to get orders of the column included in Primary Key as well on the filegroup they exist.</p>
<p style="text-align:justify;">Please note here that I am not looking for names of Primary Key or Foreign Key of database. I have already written an article for the same here : <a href="http://blog.sqlauthority.com/2009/07/17/sql-server-two-methods-to-retrieve-list-of-primary-keys-and-foreign-keys-of-database/" target="_blank">SQL SERVER – Two Methods to Retrieve List of Primary Keys and Foreign Keys of Database</a> . I am looking for T-SQL script that generates Primary Key from the existing table for all tables in database. There are already a couple of answers on my post here from two SQL Experts; you can refer to those for example <a href="http://blog.sqlauthority.com/2009/07/17/sql-server-two-methods-to-retrieve-list-of-primary-keys-and-foreign-keys-of-database/" target="_blank">here</a>.</p>
<p style="text-align:justify;">Following is an example of T-SQL that we need to generate. I<strong> am looking for script that will generate T-SQL script for all the Primary Key and Foreign Key for the entire database.</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">ALTER TABLE </span><span style="color:black;">[Person].[Address] </span><span style="color:blue;">ADD  CONSTRAINT </span><span style="color:black;">[PK_Address_AddressID] </span><span style="color:blue;">PRIMARY KEY CLUSTERED<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[AddressID] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code>
</p>
<p style="text-align:justify;">If you have a solution for the same, please post here or email me at pinal &#8216;at&#8217; sqlauthority.com and I will post on this blog with due credit. Again, please spread the word and help community become stronger by your active participation.</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>
<p style="text-align:justify;">
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology Tagged: SQL Challenge <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6032/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6032/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6032/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6032/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6032/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6032/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6032/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6032/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6032/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6032/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6032&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/07/23/sql-server-puzzle-write-script-to-generate-primary-key-and-foreign-key/feed/</wfw:commentRss>
		<slash:comments>30</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/pkgen.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Interesting Observation of Logon Trigger On All Servers &#8211; Solution</title>
		<link>http://blog.sqlauthority.com/2009/06/26/sql-server-interesting-observation-of-logon-trigger-on-all-servers-solution/</link>
		<comments>http://blog.sqlauthority.com/2009/06/26/sql-server-interesting-observation-of-logon-trigger-on-all-servers-solution/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 01:30:14 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL 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[SQL Trigger]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5718</guid>
		<description><![CDATA[Does the title of this post trigger your mind? If you all remember, a few days back I had written an article on my interesting observation regarding logon triggers. I would advise you to first read SQL SERVER – Interesting Observation of Logon Trigger On All Servers before continuing with this article further to have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5718&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Does the title of this post trigger your mind? If you all remember, a few days back I had written an article on my interesting observation regarding logon triggers. I would advise you to first read <strong><a href="http://blog.sqlauthority.com/2009/05/27/sql-server-interesting-observation-of-logon-trigger-on-all-servers/" target="_blank">SQL SERVER – Interesting Observation of Logon Trigger On All Servers</a></strong> before continuing with this article further to have a complete idea of the subject.</p>
<p style="text-align:justify;">The question I put forth in my previous article was &#8211; In single login why the trigger fires multiple times; it should be fired only once. I received numerous answers in thread as well as in my MVP private news group. Now, let us discuss the answer for the same.</p>
<p style="text-align:justify;">The answer is – <strong>It happens because multiple SQL Server services are running as well as intellisense is turned on. </strong></p>
<p style="text-align:justify;">Let us verify the above answer.</p>
<p style="text-align:justify;">First, run the following script to create database and logon Audit table.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create Audit Database */<br />
</span><span style="color:blue;">CREATE DATABASE </span><span style="color:black;">AuditDb<br />
GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">AuditDb<br />
GO<br />
</span><span style="color:green;">/* Create Audit Table */<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">ServerLogonHistory<br />
</span><span style="color:gray;">(</span><span style="color:black;">SystemUser </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">512</span><span style="color:gray;">),<br />
</span><span style="color:black;">DBUser </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">512</span><span style="color:gray;">),<br />
</span><span style="color:black;">SPID </span><span style="color:blue;">INT</span><span style="color:gray;">,<br />
</span><span style="color:black;">LogonTime DATETIME</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Create Logon Trigger */<br />
</span><span style="color:blue;">CREATE TRIGGER </span><span style="color:black;">Tr_ServerLogon<br />
</span><span style="color:blue;">ON </span><span style="color:gray;">ALL </span><span style="color:black;">SERVER </span><span style="color:blue;">FOR </span><span style="color:black;">LOGON<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
INSERT INTO </span><span style="color:black;">AuditDb.dbo.ServerLogonHistory<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">SYSTEM_USER</span><span style="color:gray;">,</span><span style="color:magenta;">USER</span><span style="color:gray;">,</span><span style="color:#434343;">@@SPID</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span></code>
</p>
<p style="text-align:justify;">After that, disable all the other SQL Server Services as delineated in the image below using SQL Server Configuration Manager.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/disableservices.png" alt="" width="456" height="277" /></p>
<p style="text-align:justify;">Next, try to logon to the system only one more time.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/loginscreen.png" alt="" width="426" height="319" /></p>
<p style="text-align:justify;">Check the audit table again to verify if there is a single entry for single login.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/singlelogin.png" alt="" width="428" height="594" /></p>
<p style="text-align:justify;">I would once again like to thank all of you for active participation and coming up with wonderful suggestions and answers. Let me have your opinion on this observation.</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 Contribution, SQL, SQL Authority, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL Trigger, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5718/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5718/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5718/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5718/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5718/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5718/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5718/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5718/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5718/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5718/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5718&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/06/26/sql-server-interesting-observation-of-logon-trigger-on-all-servers-solution/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

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

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

		<media:content url="http://www.pinaldave.com/bimg/singlelogin.png" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Solution to Puzzle &#8211; Shortest Code to Perform SSN Validation</title>
		<link>http://blog.sqlauthority.com/2009/04/29/sql-server-solution-to-puzzle-shortest-code-to-perform-ssn-validation/</link>
		<comments>http://blog.sqlauthority.com/2009/04/29/sql-server-solution-to-puzzle-shortest-code-to-perform-ssn-validation/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 01:30:16 +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 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=4647</guid>
		<description><![CDATA[One of my friends &#8211; a SQL Server MVP- Jacob Sebastian has a knack for coming up with interesting ideas and stuffs, the latest example being SQL Server Puzzles on his blog. Jacob is a regular blogger and a talented writer. I enjoy reading his blogs and books. He has recently published his new book [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4647&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">One of my friends &#8211; a SQL Server MVP- <strong>Jacob Sebastian</strong> has a knack for coming up with interesting ideas and stuffs, the latest example being <a href="http://beyondrelational.com/blogs/tc/archive/2009/04/23/tsql-challenge-4.aspx" target="_blank">SQL Server Puzzles</a> on his blog. Jacob is a regular blogger and a talented writer. I enjoy reading his blogs and books. He has recently published his new book &#8211; <a href="http://beyondrelational.com/blogs/jacob/archive/2009/04/26/my-latest-book-the-art-of-xsd-sql-server-xml-schema-collections-available-for-free-download.aspx" target="_blank">The Art of XSD &#8211; SQL Server XML Schema Collections</a>.</p>
<p style="text-align:justify;">I have based my present article on his most recent brainteaser – <strong>Write the shortest T-SQL Code that removes invalid SSN values and returns a result set with only valid SSN values</strong>. There are few additional validation rules that have been listed <a href="http://beyondrelational.com/blogs/tc/archive/2009/04/23/tsql-challenge-4.aspx" target="_blank">here</a>.</p>
<p style="text-align:justify;">I’m sure after reading the puzzle you all are bitten by the bug to solve it, and I’m certainly not an exception! I have attempted to solve the puzzle and have so far come up with the following shortest code.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT DISTINCT </span><span style="color:black;">ssn </span><span style="color:blue;">FROM </span><span style="color:#434343;">@t </span><span style="color:blue;">JOIN</span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:magenta;">LEFT</span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">,</span><span style="color:black;">3</span><span style="color:gray;">)</span><span style="color:black;">A</span><span style="color:gray;">,</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">,</span><span style="color:black;">5</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">)</span><span style="color:black;">B</span><span style="color:gray;">,</span><span style="color:magenta;">RIGHT</span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">,</span><span style="color:black;">4</span><span style="color:gray;">)</span><span style="color:black;">C </span><span style="color:blue;">FROM </span><span style="color:#434343;">@t</span><span style="color:gray;">)</span><span style="color:black;">D </span><span style="color:blue;">ON </span><span style="color:black;">A</span><span style="color:gray;">+</span><span style="color:red;">'-'</span><span style="color:gray;">+</span><span style="color:black;">B</span><span style="color:gray;">+</span><span style="color:red;">'-'</span><span style="color:gray;">+</span><span style="color:black;">C</span><span style="color:blue;">=</span><span style="color:black;">SSN </span><span style="color:gray;">AND(</span><span style="color:black;">A</span><span style="color:gray;">&gt;</span><span style="color:red;">'000'</span><span style="color:gray;">AND </span><span style="color:black;">A</span><span style="color:gray;">&lt;</span><span style="color:red;">'734'</span><span style="color:gray;">OR </span><span style="color:black;">A</span><span style="color:gray;">&gt;</span><span style="color:red;">'749'</span><span style="color:gray;">AND </span><span style="color:black;">A</span><span style="color:gray;">&lt;</span><span style="color:red;">'773'</span><span style="color:gray;">)AND </span><span style="color:black;">A</span><span style="color:gray;">&lt;&gt;</span><span style="color:red;">'666'</span><span style="color:gray;">AND </span><span style="color:black;">B</span><span style="color:gray;">&gt;</span><span style="color:red;">'00'</span><span style="color:gray;">AND </span><span style="color:black;">B</span><span style="color:gray;">&lt;=</span><span style="color:red;">'99'</span><span style="color:gray;">AND </span><span style="color:black;">C</span><span style="color:gray;">&gt;</span><span style="color:red;">'0000'</span><span style="color:gray;">AND </span><span style="color:black;">C</span><span style="color:gray;">&lt;=</span><span style="color:red;">'9999'</span></code></p>
<p style="text-align:justify;">The above code has been adjusted below for better readability:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT DISTINCT </span><span style="color:black;">ssn<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@t<br />
</span><span style="color:blue;">JOIN<br />
</span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:magenta;">LEFT</span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">,</span><span style="color:black;">3</span><span style="color:gray;">) </span><span style="color:black;">A</span><span style="color:gray;">, </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">,</span><span style="color:black;">5</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:black;">B</span><span style="color:gray;">, </span><span style="color:magenta;">RIGHT</span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">,</span><span style="color:black;">4</span><span style="color:gray;">) </span><span style="color:black;">C<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@t</span><span style="color:gray;">) </span><span style="color:black;">D<br />
</span><span style="color:blue;">ON </span><span style="color:black;">A</span><span style="color:gray;">+</span><span style="color:red;">'-'</span><span style="color:gray;">+</span><span style="color:black;">B</span><span style="color:gray;">+</span><span style="color:red;">'-'</span><span style="color:gray;">+</span><span style="color:black;">C </span><span style="color:blue;">= </span><span style="color:black;">SSN </span><span style="color:gray;">AND (</span><span style="color:black;">A </span><span style="color:gray;">&gt; </span><span style="color:red;">'000' </span><span style="color:gray;">AND </span><span style="color:black;">A </span><span style="color:gray;">&lt; </span><span style="color:red;">'734' </span><span style="color:gray;">OR </span><span style="color:black;">A </span><span style="color:gray;">&gt; </span><span style="color:red;">'749' </span><span style="color:gray;">AND </span><span style="color:black;">A </span><span style="color:gray;">&lt; </span><span style="color:red;">'773'</span><span style="color:gray;">)<br />
AND </span><span style="color:black;">A </span><span style="color:gray;">&lt;&gt; </span><span style="color:red;">'666' </span><span style="color:gray;">AND </span><span style="color:black;">B </span><span style="color:gray;">&gt; </span><span style="color:red;">'00' </span><span style="color:gray;">AND </span><span style="color:black;">B </span><span style="color:gray;">&lt;= </span><span style="color:red;">'99' </span><span style="color:gray;">AND </span><span style="color:black;">C </span><span style="color:gray;">&gt; </span><span style="color:red;">'0000' </span><span style="color:gray;">AND </span><span style="color:black;">C </span><span style="color:gray;">&lt;= </span><span style="color:red;">'9999'</span></code>
</p>
<p style="text-align:justify;">I think my proposed solution is very simple to understand; however, I would still like to give some explanation for your better understanding.</p>
<p style="text-align:justify;">I took the original SSN Number that should be in the format of &#8216;xxx-xx-xxxx&#8217;. I separated all the three parts using CTE (common table expression). After they were separated, I once again attempted to build the original SSN appending &#8216;-&#8217; at its appropriate place (<code style="font-size:12px;"><span style="color:black;">A</span><span style="color:gray;">+</span><span style="color:red;">'-'</span><span style="color:gray;">+</span><span style="color:black;">B</span><span style="color:gray;">+</span><span style="color:red;">'-'</span><span style="color:gray;">+</span><span style="color:black;">C</span></code>) i.e. 4th and 7th place. Once I recreated SSN, I compared it with the original SSN (<code style="font-size:12px;"><span style="color:black;">A</span><span style="color:gray;">+</span><span style="color:red;">'-'</span><span style="color:gray;">+</span><span style="color:black;">B</span><span style="color:gray;">+</span><span style="color:red;">'-'</span><span style="color:gray;">+</span><span style="color:black;">C </span><span style="color:blue;">= </span><span style="color:black;">SSN</span></code>); this comparison validates the format of the SSN. I must say, this was the most difficult part of solving the puzzle.</p>
<p style="text-align:justify;">The next part is comparatively simple. I  made sure that each section of SSN was falling within a range of integer and does not accept any other character. Now, instead of comparting all the digits to INT, I casted them as VARCHAR with single quotes around them. I used these VARCHAR casted variables with operator BETWEEN and compared INT range. This was the shortest way to validate INT range.</p>
<p style="text-align:justify;">I would like my readers to participate in this brainstorming session and come up the shortest code.</p>
<p style="text-align:justify;">The following is sample code to generate test table.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@t </span><span style="color:blue;">TABLE </span><span style="color:gray;">(</span><span style="color:black;">SSN </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">15</span><span style="color:gray;">))<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'123-45-6789'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'123-45-67.89'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'ABC-12-3455'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'123-45-67890'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'123-456789'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">' 123-45-6789'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">' 23-45-6789'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'12345-6789'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'123456789'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'123-12-1234'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'666-12-1234'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'123-12-0000'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'000-12-1234'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'735-12-1234'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'987-12-1234'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'123-65-1234'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'987-65-4321'<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@t </span><span style="color:gray;">(</span><span style="color:black;">SSN</span><span style="color:gray;">) </span><span style="color:blue;">SELECT </span><span style="color:red;">'987-65-4351'</span></code>
</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Database, Pinal Dave, SQL, SQL Authority, 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/4647/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/4647/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/4647/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/4647/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/4647/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/4647/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/4647/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/4647/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/4647/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/4647/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4647&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/04/29/sql-server-solution-to-puzzle-shortest-code-to-perform-ssn-validation/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; 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; Observation &#8211; Effect of Clustered Index over Nonclustered Index</title>
		<link>http://blog.sqlauthority.com/2009/02/04/sql-server-observation-effect-of-clustered-index-over-nonclustered-index/</link>
		<comments>http://blog.sqlauthority.com/2009/02/04/sql-server-observation-effect-of-clustered-index-over-nonclustered-index/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 01:30:51 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></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=2255</guid>
		<description><![CDATA[Today I came across very interesting observation while I was working on query optimization. Let us run the example first. Make sure to to enable Execution Plan (Using CTRL + M) before running comparison queries.
USE [AdventureWorks]
GO
/* */
CREATE TABLE [dbo].[MyTable](
[ID] [int] NOT NULL,
[First] [nchar](10) NULL,
[Second] [nchar](10) NULL
) ON [PRIMARY]
GO
/* Create Sample Table */
INSERT INTO [AdventureWorks].[dbo].[MyTable]
([ID],[First],[Second])
SELECT 1,'First1','Second1'
UNION ALL
SELECT 2,'First2','Second2'
UNION ALL
SELECT 3,'First3','Second3'
UNION ALL
SELECT 4,'First4','Second4'
UNION ALL
SELECT 5,'First5','Second5'
GO 

Now let us create nonclustered [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2255&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Today I came across very interesting observation while I was working on query optimization. Let us run the example first. Make sure to to enable Execution Plan (Using CTRL + M) before running comparison queries.</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;">/* */<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">[dbo].[MyTable]</span><span style="color:gray;">(<br />
</span><span style="color:black;">[ID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[First] [nchar]</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">) NULL,<br />
</span><span style="color:black;">[Second] [nchar]</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">) NULL<br />
) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO<br />
</span><span style="color:green;">/* Create Sample Table */<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">[AdventureWorks].[dbo].[MyTable]<br />
</span><span style="color:gray;">(</span><span style="color:black;">[ID]</span><span style="color:gray;">,</span><span style="color:black;">[First]</span><span style="color:gray;">,</span><span style="color:black;">[Second]</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;">'First1'</span><span style="color:gray;">,</span><span style="color:red;">'Second1'<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;">'First2'</span><span style="color:gray;">,</span><span style="color:red;">'Second2'<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;">'First3'</span><span style="color:gray;">,</span><span style="color:red;">'Second3'<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;">'First4'</span><span style="color:gray;">,</span><span style="color:red;">'Second4'<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;">'First5'</span><span style="color:gray;">,</span><span style="color:red;">'Second5'<br />
</span><span style="color:black;">GO </span></code>
</p>
<p style="text-align:justify;">Now let us create nonclustered index over this table.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create Nonclustered Index over Table */<br />
</span><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_MyTable_NonClustered]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MyTable]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[First] </span><span style="color:blue;">ASC</span><span style="color:gray;">,<br />
</span><span style="color:black;">[Second] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code>
</p>
<p style="text-align:justify;">Run following two queries together.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Run following two queries together and observe the<br />
result in by Enabling Actual Execution Plan (CTRL + M)<br />
1st Query will use Table Scan<br />
2nd Query will use Index Seek<br />
*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Second<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/nic.gif" alt="" width="500" height="502" /></p>
<p style="text-align:justify;">It is clear from query that index applies to columns on which it is created. In our case as in WHERE condition we have same columns which are used in Index.</p>
<p style="text-align:justify;">Now create Clustered Index over the same table.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create Clustered Index over Table */<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_MyTable_Clustered]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MyTable]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[ID] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code>
</p>
<p style="text-align:justify;">Once again run above two same query and see the execution plan.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Run following two queries together and observe the<br />
result in 1st Query will use Index Seek<br />
2nd Query will use Index Seek<br />
*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Second<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/nic1.gif" alt="" width="500" height="522" /></p>
<p style="text-align:justify;">Clean up the database by running following script.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Clean up */<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">[dbo].[MyTable]<br />
GO</span></code>
</p>
<p style="text-align:justify;">Interesting part of above execution plan is now both queries are using nonclustered index scan. Logically first query should have not used index which is for second query as it was retrieving the column which was not in the nonclustered index. However, it did used the nonclustered index and only difference between our first execution and second execution is that we have created clustered index over the column which is retrieved in the first query.</p>
<p style="text-align:justify;">The question is : <strong>The question is why this has happened?</strong></p>
<p style="text-align:justify;">In summary : A query which is not using nonclustered index to retrieve results used nonclustered index when clustered index created on the column which is retrieved.</p>
<p style="text-align:justify;">The reason for this happening is that every nonclustered index refers to clustered index internally. When clustered index is created on table it reorganizes the table in the physical order of the clustered index. When there is no clustered index created on table at that time all nonclustered index points to data in the table to retrieve the data, however once clustered index is created all the nonclustered indexes are reorganized and they point to clustered index. This effect is creating index seek operation on nonclustered index in our case as column on which clustered index is created is in SELECT clause and WHERE clause contains columns which are used in nonclustered index.</p>
<p style="text-align:justify;">Let me know what do you think about this article. <em>It may be possible I have not explained this problem properly so I suggest if my readers can rewrite this part of problem and send it to me and I can include their documentation here.</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><em><br />
</em></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, 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/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2255/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2255&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/02/04/sql-server-observation-effect-of-clustered-index-over-nonclustered-index/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/nic.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/nic1.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; T-SQL Script for FizzBuzz Logic</title>
		<link>http://blog.sqlauthority.com/2009/02/02/sql-server-t-sql-script-for-fizzbuzz-logic/</link>
		<comments>http://blog.sqlauthority.com/2009/02/02/sql-server-t-sql-script-for-fizzbuzz-logic/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 01:30:08 +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 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=2233</guid>
		<description><![CDATA[Following is quite common Interview Question asked in many interview questions. FizzBuzz is popular but very simple puzzle and have been very popular to solve. FizzBuzz problem can be attempted in any programming language. Let us attempt it in T-SQL.
Definition of FizzBuzz Puzzle : Write a program that prints the numbers from 1 to 100. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2233&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Following is quite common Interview Question asked in many interview questions. FizzBuzz is popular but very simple puzzle and have been very popular to solve. FizzBuzz problem can be attempted in any programming language. Let us attempt it in T-SQL.</p>
<p style="text-align:justify;"><strong>Definition of FizzBuzz Puzzle</strong> : Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@counter </span><span style="color:blue;">INT<br />
DECLARE </span><span style="color:#434343;">@output </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">8</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@counter </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:blue;">WHILE </span><span style="color:#434343;">@counter </span><span style="color:gray;">&lt; </span><span style="color:black;">101<br />
</span><span style="color:blue;">BEGIN<br />
SET </span><span style="color:#434343;">@output </span><span style="color:blue;">= </span><span style="color:red;">''<br />
</span><span style="color:blue;">IF </span><span style="color:#434343;">@counter </span><span style="color:gray;">% </span><span style="color:black;">3 </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@output </span><span style="color:blue;">= </span><span style="color:red;">'Fizz'<br />
</span><span style="color:blue;">IF </span><span style="color:#434343;">@counter </span><span style="color:gray;">% </span><span style="color:black;">5 </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@output </span><span style="color:blue;">= </span><span style="color:#434343;">@output </span><span style="color:gray;">+ </span><span style="color:red;">'Buzz'<br />
</span><span style="color:blue;">IF </span><span style="color:#434343;">@output </span><span style="color:blue;">= </span><span style="color:red;">''<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@output </span><span style="color:blue;">= </span><span style="color:#434343;">@counter<br />
</span><span style="color:blue;">PRINT </span><span style="color:#434343;">@output<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@counter </span><span style="color:blue;">= </span><span style="color:#434343;">@counter </span><span style="color:gray;">+ </span><span style="color:black;">1<br />
</span><span style="color:blue;">END</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/fizzbuzz.jpg" alt="" width="274" height="571" /></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 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/2233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2233/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2233&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/02/02/sql-server-t-sql-script-for-fizzbuzz-logic/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/bimg/fizzbuzz.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Interesting Interview Questions &#8211; Revisited</title>
		<link>http://blog.sqlauthority.com/2008/12/17/sql-server-interesting-interview-questions-revisited/</link>
		<comments>http://blog.sqlauthority.com/2008/12/17/sql-server-interesting-interview-questions-revisited/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 01:30:00 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></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[Software Development]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1754</guid>
		<description><![CDATA[I really enjoyed users participation in my previous question. Read SQL SERVER &#8211; Interesting Interview Questions before continuing reading this article. This interview question was about user participation and about how good and how different you can come with your T-SQL script. What I really liked is that many users took this test seriously and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1754&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I really enjoyed users participation in my previous question. Read <strong><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/" target="_blank">SQL SERVER &#8211; Interesting Interview Questions</a></strong> before continuing reading this article. This interview question was about user participation and about how good and how different you can come with your T-SQL script. What I really liked is that many users took this test seriously and did their best to answer. I really want to congratulate all the readers who have attempted to answer this question.</p>
<p style="text-align:justify;">As I have said earlier it did not matter what is the database structure, but it mattered what should be the good database architecture design. Here it only mattered if you can write T-SQL based on question.</p>
<p style="text-align:justify;">Following readers have got correct answer.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44580" target="_blank">Zod</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44584" target="_blank">Eric</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44598" target="_blank">Imran Mohammed</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44605" target="_blank">bhadeliaimran</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44607" target="_blank">fly</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44629" target="_blank">pom</a></p>
<p style="text-align:justify;">I really want to thank and congratulate readers who have answered this question correct. I would suggest that you book mark this article as well original article for your personal reference. In future if you need ever recommendation regarding any interview I will be happy to list this articles for you in reference.</p>
<p style="text-align:justify;">Now let us see their solution in detail.Please go over <strong><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/" target="_blank">interview question</a></strong> again before continuing reading following comments.</p>
<p style="text-align:justify;"><strong>Solution by</strong><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44580" target="_blank"><strong>Zod </strong><br />
</a></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Class1 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:#434343;">@Class2 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:#434343;">@Class3 </span><span style="color:blue;">INT</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@Class1Total </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:#434343;">@Class2Total </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:#434343;">@Class3Total </span><span style="color:blue;">INT<br />
SET </span><span style="color:#434343;">@Class1Total </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Class2Total </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Class3Total </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">DECLARE </span><span style="color:black;">student_CURSOR </span><span style="color:blue;">CURSOR FOR<br />
SELECT </span><span style="color:black;">Class1</span><span style="color:gray;">, </span><span style="color:black;">Class2</span><span style="color:gray;">, </span><span style="color:black;">Class3<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StudentsEnroll<br />
</span><span style="color:blue;">OPEN </span><span style="color:black;">student_CURSOR<br />
</span><span style="color:blue;">FETCH </span><span style="color:black;">next </span><span style="color:blue;">FROM </span><span style="color:black;">student_CURSOR<br />
</span><span style="color:blue;">INTO </span><span style="color:#434343;">@Class1</span><span style="color:gray;">, </span><span style="color:#434343;">@Class2</span><span style="color:gray;">, </span><span style="color:#434343;">@Class3<br />
</span><span style="color:blue;">WHILE </span><span style="color:#434343;">@@FETCH_STATUS </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">BEGIN<br />
SET </span><span style="color:#434343;">@Class1Total </span><span style="color:blue;">= </span><span style="color:#434343;">@Class1Total </span><span style="color:gray;">+ </span><span style="color:#434343;">@Class1<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Class2Total </span><span style="color:blue;">= </span><span style="color:#434343;">@Class2Total </span><span style="color:gray;">+ </span><span style="color:#434343;">@Class2<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Class3Total </span><span style="color:blue;">= </span><span style="color:#434343;">@Class3Total </span><span style="color:gray;">+ </span><span style="color:#434343;">@Class3<br />
</span><span style="color:blue;">FETCH </span><span style="color:black;">next </span><span style="color:blue;">FROM </span><span style="color:black;">student_CURSOR<br />
</span><span style="color:blue;">INTO </span><span style="color:#434343;">@Class1</span><span style="color:gray;">, </span><span style="color:#434343;">@Class2</span><span style="color:gray;">, </span><span style="color:#434343;">@Class3<br />
</span><span style="color:blue;">END<br />
CLOSE </span><span style="color:black;">student_CURSOR<br />
</span><span style="color:blue;">DEALLOCATE </span><span style="color:black;">student_CURSOR<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'Class1 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:#434343;">@Class1Total </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)) + </span><span style="color:red;">' students'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'Class2 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:#434343;">@Class2Total </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)) + </span><span style="color:red;">' students'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'Class3 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:#434343;">@Class3Total </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)) + </span><span style="color:red;">' students'<br />
</span><span style="color:black;">GO<br />
</span></code>
</p>
<p style="text-align:justify;"><strong>Solution by </strong><strong><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44584" target="_blank">Eric</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT<br />
</span><span style="color:red;">'Class1 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">[Class1] </span><span style="color:blue;">AS INT</span><span style="color:gray;">)) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)) + </span><span style="color:red;">' students.\n'<br />
</span><span style="color:gray;">+ </span><span style="color:red;">'Class2 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">[Class2] </span><span style="color:blue;">AS INT</span><span style="color:gray;">)) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)) + </span><span style="color:red;">' students.\n'<br />
</span><span style="color:gray;">+ </span><span style="color:red;">'Class3 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">[Class3] </span><span style="color:blue;">AS INT</span><span style="color:gray;">)) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)) + </span><span style="color:red;">' students.\n'<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[StudentsEnroll]</span><span style="color:gray;">;</span></code>
</p>
<p style="text-align:justify;"><strong>Solution by </strong><strong><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44598" target="_blank">Imran Mohammed</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">CLASS</span><span style="color:gray;">+ </span><span style="color:red;">' has '</span><span style="color:gray;">+</span><span style="color:magenta;">CONVERT(</span><span>VARCHAR</span><span>(</span><span>10</span><span style="color:gray;">),</span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:black;">orders </span><span style="color:gray;">))+</span><span style="color:red;">' Students ' 'Output' </span><span style="color:blue;">FROM </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">students</span><span style="color:gray;">, </span><span style="color:black;">Class</span><span style="color:gray;">, </span><span style="color:black;">Orders<br />
</span><span style="color:blue;">FROM<br />
</span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:black;">students</span><span style="color:gray;">, </span><span style="color:black;">class1</span><span style="color:gray;">, </span><span style="color:black;">class2</span><span style="color:gray;">,</span><span style="color:black;">class3<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">studentsenroll </span><span style="color:gray;">) </span><span style="color:black;">p<br />
UNPIVOT<br />
</span><span style="color:gray;">(</span><span style="color:black;">Orders </span><span style="color:blue;">FOR </span><span style="color:black;">Class </span><span style="color:blue;">IN<br />
</span><span style="color:gray;">(</span><span style="color:black;">class1</span><span style="color:gray;">, </span><span style="color:black;">class2</span><span style="color:gray;">, </span><span style="color:black;">class3 </span><span style="color:gray;">)<br />
)</span><span style="color:blue;">AS </span><span style="color:black;">unpvt</span><span style="color:gray;">) </span><span style="color:black;">X </span><span style="color:blue;">WHERE </span><span style="color:black;">orders </span><span style="color:blue;">= </span><span style="color:black;">1 </span><span style="color:blue;">GROUP BY </span><span style="color:black;">class</span></code>
</p>
<p style="text-align:justify;"><strong>Solution by </strong><strong><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44605" target="_blank">bhadeliaimran</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@opXml </span><span style="color:blue;">AS </span><span style="color:black;">XML<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@opXml </span><span style="color:blue;">= </span><span style="color:red;">'&lt;ClassStud value=''' </span><span style="color:gray;">+ </span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(<br />
(</span><span style="color:blue;">SELECT<br />
</span><span style="color:red;">'Class1 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">Class1 </span><span style="color:blue;">AS </span><span style="color:black;">SMALLINT</span><span style="color:gray;">)) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">4</span><span style="color:gray;">)) + </span><span style="color:red;">' students' </span><span style="color:gray;">,<br />
</span><span style="color:red;">',Class2 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">Class2 </span><span style="color:blue;">AS </span><span style="color:black;">SMALLINT</span><span style="color:gray;">)) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">4</span><span style="color:gray;">)) + </span><span style="color:red;">' students' </span><span style="color:gray;">,<br />
</span><span style="color:red;">',Class3 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">Class3 </span><span style="color:blue;">AS </span><span style="color:black;">SMALLINT</span><span style="color:gray;">)) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">4</span><span style="color:gray;">)) + </span><span style="color:red;">' students'<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[StudentsEnroll]<br />
</span><span style="color:blue;">FOR </span><span style="color:black;">XML PATH</span><span style="color:gray;">(</span><span style="color:red;">''</span><span style="color:gray;">) ), </span><span style="color:red;">','</span><span style="color:gray;">, </span><span style="color:red;">''' /&gt;&lt;ClassStud value='''</span><span style="color:gray;">) + </span><span style="color:red;">''' /&gt;'<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">x.value</span><span style="color:gray;">(</span><span style="color:red;">'@value'</span><span style="color:gray;">, </span><span style="color:red;">'varchar(100)'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">[output]<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@opXml</span><span style="color:black;">.nodes</span><span style="color:gray;">(</span><span style="color:red;">'/ClassStud'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">p</span><span style="color:gray;">(</span><span style="color:black;">x</span><span style="color:gray;">)</span></code>
</p>
<p style="text-align:justify;"><strong>Solution by </strong><strong><a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44607" target="_blank">fly</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@tbl </span><span style="color:blue;">TABLE </span><span style="color:gray;">(</span><span style="color:black;">i </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@tbl </span><span style="color:gray;">(</span><span style="color:black;">i</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:black;">3<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">i </span><span style="color:blue;">= </span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'Class 1 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:magenta;">CAST </span><span style="color:gray;">(</span><span style="color:black;">class1 </span><span style="color:blue;">AS INT</span><span style="color:gray;">)) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">)) + </span><span style="color:red;">' Students' </span><span style="color:blue;">ELSE<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">i </span><span style="color:blue;">= </span><span style="color:black;">2 </span><span style="color:blue;">THEN </span><span style="color:red;">'Class 2 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:magenta;">CAST </span><span style="color:gray;">(</span><span style="color:black;">class2 </span><span style="color:blue;">AS INT</span><span style="color:gray;">)) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">)) + </span><span style="color:red;">' Students' </span><span style="color:blue;">ELSE<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">i </span><span style="color:blue;">= </span><span style="color:black;">3 </span><span style="color:blue;">THEN </span><span style="color:red;">'Class 3 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:magenta;">CAST </span><span style="color:gray;">(</span><span style="color:black;">class3 </span><span style="color:blue;">AS INT</span><span style="color:gray;">)) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">)) + </span><span style="color:red;">' Students' </span><span style="color:blue;">ELSE </span><span style="color:red;">'' </span><span style="color:blue;">END<br />
END<br />
END<br />
FROM </span><span style="color:black;">[StudentsEnroll]<br />
</span><span style="color:blue;">JOIN </span><span style="color:#434343;">@tbl </span><span style="color:black;">t </span><span style="color:blue;">ON </span><span style="color:black;">i </span><span style="color:gray;">&lt;= </span><span style="color:black;">3<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">i</span></code>
</p>
<p style="text-align:justify;"><strong>Solution by<a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/#comment-44629" target="_blank"> pom</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:red;">'class1 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">((</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:blue;">FROM </span><span style="color:black;">StudentsEnroll </span><span style="color:blue;">WHERE </span><span style="color:black;">class1</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">GROUP BY </span><span style="color:black;">class1</span><span style="color:gray;">) </span><span style="color:blue;">AS CHAR</span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">)) + </span><span style="color:red;">'Students'<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:red;">'class2 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">((</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:blue;">FROM </span><span style="color:black;">StudentsEnroll </span><span style="color:blue;">WHERE </span><span style="color:black;">class2</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">GROUP BY </span><span style="color:black;">class2</span><span style="color:gray;">) </span><span style="color:blue;">AS CHAR</span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">)) + </span><span style="color:red;">'Students'<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:red;">'class3 has ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">((</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:blue;">FROM </span><span style="color:black;">StudentsEnroll </span><span style="color:blue;">WHERE </span><span style="color:black;">class3</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">GROUP BY </span><span style="color:black;">class3</span><span style="color:gray;">) </span><span style="color:blue;">AS CHAR</span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">)) + </span><span style="color:red;">'Students'</span></code>
</p>
<p style="text-align:justify;">Other two solution proposed earlier in<a href="http://blog.sqlauthority.com/2008/12/07/sql-server-interesting-interview-questions/" target="_blank"> original article</a>.</p>
<p style="text-align:justify;"><strong>Solution 1 &#8211; Using only SELECT statement</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Col </span><span style="color:blue;">INT<br />
SET </span><span style="color:#434343;">@Col </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:blue;">WHILE </span><span style="color:gray;">(</span><span style="color:#434343;">@Col </span><span style="color:gray;">&lt; </span><span style="color:black;">4</span><span style="color:gray;">)<br />
</span><span style="color:blue;">BEGIN<br />
EXEC</span><span style="color:gray;">(</span><span style="color:red;">‘SELECT     ”Class’</span><span style="color:gray;">+</span><span style="color:#434343;">@Col</span><span style="color:gray;">+</span><span style="color:red;">‘ Has ” + CAST(COUNT(Students) AS VARCHAR(100)) + ” Students” Results<br />
FROM         studentsenroll<br />
WHERE         Class’</span><span style="color:gray;">+</span><span style="color:#434343;">@Col</span><span style="color:gray;">+</span><span style="color:red;">‘ = 1<br />
GROUP BY      Class’</span><span style="color:gray;">+</span><span style="color:#434343;">@Col</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Col </span><span style="color:blue;">= </span><span style="color:#434343;">@Col </span><span style="color:gray;">+ </span><span style="color:black;">1<br />
</span><span style="color:blue;">CONTINUE<br />
END</span></code>
</p>
<p style="text-align:justify;"><strong>Solution 2 &#8211; Getting results in one resultset</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:red;">‘Class1 has ’ </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)) +</span><span style="color:red;">‘ Students’<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StudentsEnroll<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Class1 </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">Class1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">‘Class2 has ’ </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)) +</span><span style="color:red;">‘ Students’<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StudentsEnroll<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Class2 </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">Class2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">‘Class3 has ’ </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)) +</span><span style="color:red;">‘ Students’<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">StudentsEnroll<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Class3 </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">Class3</span></code>
</p>
<p style="text-align:justify;"><span style="color:black;">Now the real question is which of this articles are favorite solution of yours. Please leave your choice along with reason in comment. </span></p>
<p style="text-align:justify;"><span style="color:black;">I promise if you can not write solution but if you understand the proposed solution in this article you will usually pass any database T-SQL problem in interviews. Additionally, if you are looking for job in SQL Server related area please find your <strong><a href="http://jobs.sqlauthority.com/" target="_blank">right job</a> </strong>here. Make sure to read all the <strong><a href="http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/" target="_blank">interview questions and answers for SQL Server 2008</a></strong> before you appear for interview. </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, Pinal Dave, Readers Contribution, Software Development, SQL, SQL Authority, SQL Interview Questions and Answers, 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/1754/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1754/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1754/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1754/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1754/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1754/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1754/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1754/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1754/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1754/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1754&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/12/17/sql-server-interesting-interview-questions-revisited/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; Email from Blog Reader &#8211; Not a Potential Bug in SQL &#8211; Puzzle</title>
		<link>http://blog.sqlauthority.com/2008/12/13/sql-server-interesting-email-from-blog-reader-puzzle/</link>
		<comments>http://blog.sqlauthority.com/2008/12/13/sql-server-interesting-email-from-blog-reader-puzzle/#comments</comments>
		<pubDate>Sat, 13 Dec 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 Error Messages]]></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>
		<category><![CDATA[SQL Sub Query]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1721</guid>
		<description><![CDATA[Few days ago, I received wonderful email from blog reader and it was like good puzzle. I enjoyed solving this puzzle. I did not write the name of the blog reader because I am not sure if he wants his name here or not. Please read this article and run the script described in email. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1721&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Few days ago, I received wonderful email from blog reader and it was like good puzzle. I enjoyed solving this puzzle. I did not write the name of the blog reader because I am not sure if he wants his name here or not. Please read this article and run the script described in email. You can download the <a href="http://www.pinaldave.com/bimg/BuggyScript.zip" target="_blank">SQL Script here</a>. Also can any of you help this reader why SQL Server is behaving like this. I have already replied him with correct answer where I suggest that it is not bug and have explained him the reason for the same.</p>
<p style="text-align:justify;"><strong>Email Details are like this:</strong></p>
<p style="text-align:justify;">Few days ago, I found a very strange thing with SQL SERVER and I strongly feel that THIS IS A SEVERE BUG in SQL SERVER which actually can create lots of data discrepancy.</p>
<p style="text-align:justify;">To prove my point I have attached a SQL Script with this mail. I want you to run the script in SQL SERVER.</p>
<p style="text-align:justify;">Details of the Script</p>
<p style="text-align:justify;">Create new database.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE DATABASE </span><span style="color:black;">DB_TEST</span></code></p>
<p style="text-align:justify;">Use this Database.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">DB_TEST</span></code></p>
<p style="text-align:justify;">Create a table named tblENQUIRY</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE TABLE </span><span style="color:black;">tblENQUIRY<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">ENQUIRYID </span><span style="color:blue;">INT PRIMARY KEY</span><span style="color:gray;">,<br />
</span><span style="color:black;">ENQUIRYNAME </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">50</span><span style="color:gray;">)<br />
)</span></code>
</p>
<p style="text-align:justify;">Create another table named tblAPPLICANTS</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE TABLE </span><span style="color:black;">tblAPPLICANTS<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">APPLICANTID </span><span style="color:blue;">INT PRIMARY KEY</span><span style="color:gray;">,<br />
</span><span style="color:black;">ENQUIRYID </span><span style="color:blue;">INT FOREIGN KEY REFERENCES </span><span style="color:black;">tblENQUIRY</span><span style="color:gray;">(</span><span style="color:black;">ENQUIRYID</span><span style="color:gray;">),<br />
</span><span style="color:black;">ISCOMPLETED BIT<br />
</span><span style="color:gray;">)</span></code>
</p>
<p style="text-align:justify;">Insert 5 rows in tblENQUIRY</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblENQUIRY </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;">'Enquiry1'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblENQUIRY </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;">'Enquiry2'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblENQUIRY </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;">'Enquiry3'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblENQUIRY </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;">'Enquiry4'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblENQUIRY </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;">'Enquiry5'</span><span style="color:gray;">)</span></code>
</p>
<p style="text-align:justify;">Insert 10 rows in tblAPPLICANTS</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblAPPLICANTS </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;">1</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblAPPLICANTS </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;">1</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblAPPLICANTS </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;">2</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblAPPLICANTS </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:black;">2</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblAPPLICANTS </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:black;">3</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblAPPLICANTS </span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">6</span><span style="color:gray;">,</span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblAPPLICANTS </span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7</span><span style="color:gray;">,</span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblAPPLICANTS </span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">8</span><span style="color:gray;">,</span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblAPPLICANTS </span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">9</span><span style="color:gray;">,</span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">tblAPPLICANTS </span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">,</span><span style="color:black;">5</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)</span></code>
</p>
<p style="text-align:justify;">Select statement to confirm the data is inserted or not.</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;">tblENQUIRY<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">tblAPPLICANTS </span></code>
</p>
<p style="text-align:justify;">Here is a select query which is not correct. The query tells select APPLICANTID from tblENQUIRY table where ENQUIRYID = 1. But the problem here is tblENQUIRY is not having a column named APPLICANTID. So this query throws an error and that’s a correct behaviour.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">APPLICANTID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">tblENQUIRY<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ENQUIRYID </span><span style="color:blue;">= </span><span style="color:black;">1</span></code>
</p>
<p style="text-align:justify;">Here is the magic. I have used the above select query which is not correct as a inner query with a update statement. What to do you think? What will happen?     Update query should throw an error. Logically it should.I was thinking the same But just execute this update query and you will be shocked.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">UPDATE </span><span style="color:black;">tblAPPLICANTS </span><span style="color:blue;">SET </span><span style="color:black;">ISCOMPLETED </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">APPLICANTID </span><span style="color:blue;">IN </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">APPLICANTID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">tblENQUIRY<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ENQUIRYID </span><span style="color:blue;">= </span><span style="color:black;">1</span><span style="color:gray;">)</span></code></p>
<p style="text-align:justify;">Oops, 10 rows affected. Surpised. All the data in ISCOMPLETED field is set to 1 but my inner query<br />
<code style="font-size:12px;"><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">APPLICANTID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">tblENQUIRY<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ENQUIRYID </span><span style="color:blue;">= </span><span style="color:black;">1</span><span style="color:gray;">) </span></code><br />
is wrong.
</p>
<p style="text-align:justify;">This is just a sample query but When I executed somewhat similar statement 3364 rows in my table was updated.</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;">tblENQUIRY<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">tblAPPLICANTS</span></code>
</p>
<p style="text-align:justify;">Fortunately It was not on the Live server otherwise things could have been worse for me.</p>
<p style="text-align:justify;">I think this is a severe bug.You all are expert. Can anyone explain me why this is happening?</p>
<p style="text-align:justify;">If I am correct, Please pass on to the Microsoft SQL SERVER Team.</p>
<p style="text-align:justify;">Waiting for a favorable reply.</p>
<p style="text-align:justify;"><em><strong>Thanks,</strong></em></p>
<p style="text-align:justify;"><em><strong>(Name of blog reader)</strong></em></p>
<p style="text-align:justify;">If you have directly jumped here without reading complete email I suggest that you read this email again and run the script associated with it and observe why more than one rows are updated.</p>
<p style="text-align:justify;">It is correct observation of reader that when</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">APPLICANTID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">tblENQUIRY<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ENQUIRYID </span><span style="color:blue;">= </span><span style="color:black;">1</span></code>
</p>
<p style="text-align:justify;">is ran it gives error but when complete query is ran it runs fine because APPLICANTID of sub query is actually column of outer query. Effect query is actually following query and that is why it goes not give any error.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">UPDATE </span><span style="color:black;">tblAPPLICANTS </span><span style="color:blue;">SET </span><span style="color:black;">ISCOMPLETED </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">APPLICANTID </span><span style="color:blue;">IN </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">tblAPPLICANTS.APPLICANTID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">tblENQUIRY<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ENQUIRYID </span><span style="color:blue;">= </span><span style="color:black;">1</span><span style="color:gray;">)</span></code>
</p>
<p style="text-align:justify;">Now because same column is used in outerquery and subquery it gives effective result like this.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">UPDATE </span><span style="color:black;">tblAPPLICANTS </span><span style="color:blue;">SET </span><span style="color:black;">ISCOMPLETED </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">1 </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">)</span></code>
</p>
<p style="text-align:justify;">Due to same reason all the columns of the table are updated not only one.</p>
<p style="text-align:justify;">You can verify that is same in subquery and outerquery running following query.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">tblAPPLICANTS.APPLICANTID</span><span style="color:gray;">, </span><span style="color:black;">t.APPLICANTID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">tblAPPLICANTS<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">tblAPPLICANTS.APPLICANTID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">tblENQUIRY<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">ENQUIRYID </span><span style="color:blue;">= </span><span style="color:black;">1</span><span style="color:gray;">) </span><span style="color:black;">t </span><span style="color:blue;">ON </span><span style="color:black;">tblAPPLICANTS.APPLICANTID </span><span style="color:blue;">= </span><span style="color:black;">t.APPLICANTID</span></code>
</p>
<p style="text-align:justify;">When you run above query you will find that both the APPLICANTID are the same.</p>
<p style="text-align:justify;">I hope this clears the issue. If you have read till this point in this article, congratulations and leave a comment for me that you read. This way I can understand that my readers are interested in topics like this and in future I may have more articles like this.</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 Error Messages, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: SQL Sub Query <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1721/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1721/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1721/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1721/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1721/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1721/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1721/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1721/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1721/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1721/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1721&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/12/13/sql-server-interesting-email-from-blog-reader-puzzle/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; Interesting Interview Questions &#8211; Part 2 &#8211; Puzzle &#8211; Solution</title>
		<link>http://blog.sqlauthority.com/2008/12/11/sql-server-interesting-interview-questions-part-2-puzzle-solution/</link>
		<comments>http://blog.sqlauthority.com/2008/12/11/sql-server-interesting-interview-questions-part-2-puzzle-solution/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 01:30:57 +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 Interview Questions and Answers]]></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=1652</guid>
		<description><![CDATA[Yesterday we looked at Puzzle and I did got great response to this question. Very interestingly not many got it right. First go through the puzzle first and then come back here and read answer.
Read Original Interview Question and Puzzle.
Question: Select all the person from table PersonColor who have same color as ColorCode or have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1652&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Yesterday we looked at Puzzle and I did got great response to this question. Very interestingly not many got it right. First go through the puzzle first and then come back here and read answer.</p>
<h3 style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/" target="_blank">Read Original Interview Question and Puzzle</a></strong>.</h3>
<p style="text-align:justify;"><em><strong>Question: Select all the person from table PersonColor who have same color as ColorCode or have more colors than table ColorCode.</strong></em></p>
<p style="text-align:justify;"><em>UPDATE: Following solution is written with assumption that in SelectedColors table Name and ColorCode are Primary Key. This requirement was not specified in original question.<strong><br />
</strong></em></p>
<p><code style="font-size:12px;"><span style="color:green;">/*Answer to Interview Question*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Name<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">PersonColors pc<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">SelectedColors sc </span><span style="color:blue;">ON </span><span style="color:black;">sc.ColorCode </span><span style="color:blue;">= </span><span style="color:black;">pc.ColorCode<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">pc.Name<br />
</span><span style="color:blue;">HAVING </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:black;">pc.ColorCode</span><span style="color:gray;">) &gt;= (</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:black;">ColorCode</span><span style="color:gray;">) </span><span style="color:blue;">FROM </span><span style="color:black;">SelectedColors</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/bimg/int2.gif" alt="" width="159" height="98" /></p>
<p style="text-align:justify;">If you want to download complete script for this interview question. <a href="http://www.pinaldave.com/bimg/interviewquestion.zip">Please download it from here</a>.</p>
<p style="text-align:justify;">Complete script of the puzzle is also listed here.</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;">/*Create First Table PersonColors*/<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">PersonColors </span><span style="color:gray;">(</span><span style="color:black;">Name </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">), </span><span style="color:black;">ColorCode </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">PersonColors </span><span style="color:gray;">(</span><span style="color:black;">Name</span><span style="color:gray;">,</span><span style="color:black;">ColorCode</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:red;">'Tom'</span><span style="color:gray;">,</span><span style="color:red;">'Red'</span><span style="color:gray;">),(</span><span style="color:red;">'Tom'</span><span style="color:gray;">,</span><span style="color:red;">'Blue'</span><span style="color:gray;">),(</span><span style="color:red;">'Tom'</span><span style="color:gray;">,</span><span style="color:red;">'Green'</span><span style="color:gray;">),(</span><span style="color:red;">'Tom'</span><span style="color:gray;">,</span><span style="color:red;">'Brown'</span><span style="color:gray;">),<br />
(</span><span style="color:red;">'Mike'</span><span style="color:gray;">,</span><span style="color:red;">'Red'</span><span style="color:gray;">),(</span><span style="color:red;">'Mike'</span><span style="color:gray;">,</span><span style="color:red;">'Blue'</span><span style="color:gray;">),<br />
(</span><span style="color:red;">'James'</span><span style="color:gray;">,</span><span style="color:red;">'Green'</span><span style="color:gray;">),(</span><span style="color:red;">'James'</span><span style="color:gray;">,</span><span style="color:red;">'Brown'</span><span style="color:gray;">),<br />
(</span><span style="color:red;">'Joe'</span><span style="color:gray;">,</span><span style="color:red;">'Red'</span><span style="color:gray;">),(</span><span style="color:red;">'Joe'</span><span style="color:gray;">,</span><span style="color:red;">'Blue'</span><span style="color:gray;">),(</span><span style="color:red;">'Joe'</span><span style="color:gray;">,</span><span style="color:red;">'Green'</span><span style="color:gray;">),<br />
(</span><span style="color:red;">'Matt'</span><span style="color:gray;">,</span><span style="color:red;">'Brown'</span><span style="color:gray;">)<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;">PersonColors<br />
GO<br />
</span><span style="color:green;">/*Create Second Table SelectedColors*/<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">SelectedColors </span><span style="color:gray;">(</span><span style="color:black;">ColorCode </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">SelectedColors </span><span style="color:gray;">(</span><span style="color:black;">ColorCode</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:red;">'Red'</span><span style="color:gray;">),(</span><span style="color:red;">'Blue'</span><span style="color:gray;">),(</span><span style="color:red;">'Green'</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;">SelectedColors<br />
GO<br />
</span><span style="color:green;">/*Answer to Interview Question*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Name<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">PersonColors pc<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">SelectedColors sc </span><span style="color:blue;">ON </span><span style="color:black;">sc.ColorCode </span><span style="color:blue;">= </span><span style="color:black;">pc.ColorCode<br />
</span><span style="color:blue;">GROUP BY </span><span style="color:black;">pc.Name<br />
</span><span style="color:blue;">HAVING </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:black;">pc.ColorCode</span><span style="color:gray;">) &gt;= (</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:black;">ColorCode</span><span style="color:gray;">) </span><span style="color:blue;">FROM </span><span style="color:black;">SelectedColors</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:gray;">*</span><span style="color:black;">Clean up </span><span style="color:blue;">DATABASE</span><span style="color:green;">*/<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">PersonColors<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">SelectedColors<br />
GO</span></code></p>
<p style="text-align:justify;">Let me know your opinion about this puzzle.</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 Interview Questions and Answers, 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/1652/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1652/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1652/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1652/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1652/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1652/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1652/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1652/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1652/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1652/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1652&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/12/11/sql-server-interesting-interview-questions-part-2-puzzle-solution/feed/</wfw:commentRss>
		<slash:comments>21</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/int2.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Interesting Interview Questions &#8211; Part 2 &#8211; Puzzle</title>
		<link>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/</link>
		<comments>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 01:30:35 +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 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=1640</guid>
		<description><![CDATA[In the recent time of recession my company is able to continue its progress and we are hiring. It is very surprising to me that many developers who have experience with SQL Server could not get following simple question right. There were nearly 40 candidates I interviewed but none of the candidate was able to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1640&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">In the recent time of recession my company is able to continue its progress and we are hiring. It is very surprising to me that many developers who have experience with SQL Server could not get following simple question right. There were nearly 40 candidates I interviewed but none of the candidate was able to solve this problem. When I displayed final answer they could not believe that it is that simple.</p>
<p style="text-align:justify;">When I asked some of the MCITP or Oracle certified candidate about why they can not get this simple question, they smiled and answered that I did not have that on my blog or on my <strong><a href="http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/" target="_blank">interview questions list</a></strong>. Let us go over the interview question.</p>
<p style="text-align:justify;">Let us create two tables. Once Created SELECT values from both the tables.</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 First Table PersonColors*/<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">PersonColors </span><span style="color:gray;">(</span><span style="color:black;">Name </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">), </span><span style="color:black;">ColorCode </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">PersonColors </span><span style="color:gray;">(</span><span style="color:black;">Name</span><span style="color:gray;">,</span><span style="color:black;">ColorCode</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:red;">'Tom'</span><span style="color:gray;">,</span><span style="color:red;">'Red'</span><span style="color:gray;">),(</span><span style="color:red;">'Tom'</span><span style="color:gray;">,</span><span style="color:red;">'Blue'</span><span style="color:gray;">),(</span><span style="color:red;">'Tom'</span><span style="color:gray;">,</span><span style="color:red;">'Green'</span><span style="color:gray;">),(</span><span style="color:red;">'Tom'</span><span style="color:gray;">,</span><span style="color:red;">'Brown'</span><span style="color:gray;">),<br />
(</span><span style="color:red;">'Mike'</span><span style="color:gray;">,</span><span style="color:red;">'Red'</span><span style="color:gray;">),(</span><span style="color:red;">'Mike'</span><span style="color:gray;">,</span><span style="color:red;">'Blue'</span><span style="color:gray;">),<br />
(</span><span style="color:red;">'James'</span><span style="color:gray;">,</span><span style="color:red;">'Green'</span><span style="color:gray;">),(</span><span style="color:red;">'James'</span><span style="color:gray;">,</span><span style="color:red;">'Brown'</span><span style="color:gray;">),<br />
(</span><span style="color:red;">'Joe'</span><span style="color:gray;">,</span><span style="color:red;">'Red'</span><span style="color:gray;">),(</span><span style="color:red;">'Joe'</span><span style="color:gray;">,</span><span style="color:red;">'Blue'</span><span style="color:gray;">),(</span><span style="color:red;">'Joe'</span><span style="color:gray;">,</span><span style="color:red;">'Green'</span><span style="color:gray;">),<br />
(</span><span style="color:red;">'Matt'</span><span style="color:gray;">,</span><span style="color:red;">'Brown'</span><span style="color:gray;">)<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;">PersonColors<br />
GO<br />
</span><span style="color:green;">/*Create Second Table SelectedColors*/<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">SelectedColors </span><span style="color:gray;">(</span><span style="color:black;">ColorCode </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">SelectedColors </span><span style="color:gray;">(</span><span style="color:black;">ColorCode</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:red;">'Red'</span><span style="color:gray;">),(</span><span style="color:red;">'Blue'</span><span style="color:gray;">),(</span><span style="color:red;">'Green'</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;">SelectedColors<br />
GO </span></code>
</p>
<p style="text-align:justify;">The resultset of created table is as following.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/int1.gif" alt="" width="230" height="474" /></p>
<p style="text-align:justify;">Now the question is find all the persons from table PersonColor who have all the ColorCode mentioned in the table ColorCode. From the example it is clear that final result will contain Tom and Joe as answer. Tom has Red, Blue and Green as well Brown colorcode. If any person have Red, Blue and Green color they should qualify in our resultset. Joe has all the same colorcode as ColorCode table so it should also qualify in our resultset. Other person Mike and James did not have same kind of colorcode as table ColorCode.</p>
<p style="text-align:justify;"><em><strong>In summary, Select all the person from table PersonColor who have same color as ColorCode or have more colors than table ColorCode.</strong></em></p>
<p style="text-align:justify;">When you look at this question it is very simple and answer is very simple as well. One of my employee was able to solve it in 3 mins. However, all the 40 interview candidates found it very difficult to write the query for the same.</p>
<p style="text-align:justify;">Now before looking at the answer, please try to find the solution by yourself. Once you find the solution compare to the solution I have and see if you got it correct.</p>
<p style="text-align:justify;">Expected Resultset should look like this.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/int2.gif" alt="" width="159" height="98" /></p>
<p style="text-align:justify;">Again, it may look very simple I would strongly suggest you try to solve it by yourself. If you can not solve it then try to look solution here.</p>
<p style="text-align:justify;"><strong>UPDATE:</strong> I have used SQL Server 2008 script to generate the same table. I thank <strong>Tejas Shah</strong> for providing the script of SQL Server 2005 <strong><a href="http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/#comment-44674">here</a></strong>.</p>
<p style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2008/12/11/sql-server-interesting-interview-questions-part-2-puzzle-solution/" target="_blank">Click here for Solution. (Go live on 12/11/2008)<br />
</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 Pinal Dave, SQL, SQL Authority, SQL Interview Questions and Answers, 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/1640/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1640/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1640/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1640/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1640/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1640/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1640/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1640/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1640/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1640/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1640&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/12/10/sql-server-interesting-interview-questions-part-2-puzzle/feed/</wfw:commentRss>
		<slash:comments>35</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/int1.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/int2.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Puzzle &#8211; Solution &#8211; Computed Columns Datatype Explanation</title>
		<link>http://blog.sqlauthority.com/2008/09/29/sql-server-puzzle-solution-computed-columns-datatype-explanation/</link>
		<comments>http://blog.sqlauthority.com/2008/09/29/sql-server-puzzle-solution-computed-columns-datatype-explanation/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 01:30:35 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MVP]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Documentation]]></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>
		<category><![CDATA[Computed Columns]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1326</guid>
		<description><![CDATA[Just a day before I wrote article SQL SERVER &#8211; Puzzle &#8211; Computed Columns Datatype Explanation
which was inspired by SQL Server MVP Jacob Sebastian. I suggest that before continuing this article read original puzzle question SQL SERVER &#8211; Puzzle &#8211; Computed Columns Datatype Explanation.

The question was if computed column was of datatype TINYINT how to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1326&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Just a day before I wrote article <strong><a href="http://blog.sqlauthority.com/2008/09/27/sql-server-puzzle-computed-columns-datatype-explanation/" target="_blank">SQL SERVER &#8211; Puzzle &#8211; Computed Columns Datatype Explanation</a></strong><br />
which was inspired by SQL Server MVP <strong><a href="http://www.sqlserverandxml.com/" target="_blank">Jacob Sebastian</a></strong>. I suggest that before continuing this article read original puzzle question <strong><a href="http://blog.sqlauthority.com/2008/09/27/sql-server-puzzle-computed-columns-datatype-explanation/" target="_blank">SQL SERVER &#8211; Puzzle &#8211; Computed Columns Datatype Explanation</a></strong>.
</p>
<p style="text-align:justify;"><em><strong>The question was if computed column was of datatype TINYINT how to create Computed Column of datatype INT?</strong></em></p>
<p style="text-align:justify;">Before we continue with the answer let us run following script and understand how computed column is created.</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;">MyTable<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">ID TINYINT </span><span style="color:gray;">NOT NULL </span><span style="color:#434343;">IDENTITY </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">),<br />
</span><span style="color:black;">FirstCol TINYINT </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">SecondCol TINYINT </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">ThirdCol TINYINT </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">ComputedCol </span><span style="color:blue;">AS </span><span style="color:gray;">(</span><span style="color:black;">FirstCol</span><span style="color:gray;">+</span><span style="color:black;">SecondCol</span><span style="color:gray;">)*</span><span style="color:black;">ThirdCol<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">MyTable<br />
</span><span style="color:gray;">(</span><span style="color:black;">[FirstCol]</span><span style="color:gray;">,</span><span style="color:black;">[SecondCol] </span><span style="color:gray;">,</span><span style="color:black;">[ThirdCol]</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:black;">2</span><span style="color:gray;">,</span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:black;">4<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:black;">5<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:black;">5</span><span style="color:gray;">,</span><span style="color:black;">6<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:black;">5</span><span style="color:gray;">,</span><span style="color:black;">6</span><span style="color:gray;">,</span><span style="color:black;">7<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;">MyTable<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">MyTable<br />
GO</span></code>
</p>
<p style="text-align:justify;">If you see in above script <strong>ComputedCol </strong>is computed column and notice that user can not specify datatype of Computed Columns. Definition of computed column is given by using other columns of the table. (In our case ComputedCol is defined as adding of FirstCol to SecondCol, multiplied by ThirdCol.)</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/compcol1.jpg" alt="" width="433" height="662" /></p>
<p style="text-align:justify;">As we have seen we do not specify the datatype of the computed column, question comes to mind that what will be the datatype of the computed column. From <a href="http://blog.sqlauthority.com/2008/09/27/sql-server-puzzle-computed-columns-datatype-explanation/" target="_blank">previous article</a> we have observed when there are operation between different datatypes it follows the datatype hierarchy.</p>
<p style="text-align:justify;"><em>When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type. (From <a href="http://msdn.microsoft.com/en-us/library/ms190309(SQL.90).aspx" target="_blank">BOL</a>). Read the precedence <a href="http://msdn.microsoft.com/en-us/library/ms190309(SQL.90).aspx" target="_blank">here</a>.</em></p>
<p style="text-align:justify;">As in computed column all the datatypes are TINYINT the computed column (ComputedCol) will be of TINYINT as well. Let us test the assumption here. Run following query where we try to insert such values in the first three columns its computed result will be larger than datatype TINYINT&#8217;s range. Range of TINYINT is from 0 to 255.</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;">MyTable<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">ID TINYINT </span><span style="color:gray;">NOT NULL </span><span style="color:#434343;">IDENTITY </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">),<br />
</span><span style="color:black;">FirstCol TINYINT </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">SecondCol TINYINT </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">ThirdCol TINYINT </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">ComputedCol </span><span style="color:blue;">AS </span><span style="color:gray;">(</span><span style="color:black;">FirstCol</span><span style="color:gray;">+</span><span style="color:black;">SecondCol</span><span style="color:gray;">)*</span><span style="color:black;">ThirdCol<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">MyTable<br />
</span><span style="color:gray;">(</span><span style="color:black;">[FirstCol]</span><span style="color:gray;">,</span><span style="color:black;">[SecondCol]</span><span style="color:gray;">,</span><span style="color:black;">[ThirdCol]</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">6</span><span style="color:gray;">,</span><span style="color:black;">6</span><span style="color:gray;">,</span><span style="color:black;">100</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Run only till here first.*/</span></code></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Running following SELECT will give Error*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">MyTable<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">MyTable<br />
GO</span></code>
</p>
<p style="text-align:justify;">While running above script we found that there was absolutely no issue till INSERT statement was ran. As soon as next SELECT statement ran it throwed following error.</p>
<p style="text-align:justify;"><em><span style="color:#ff0000;">(1 row(s) affected)<br />
Msg 8115, Level 16, State 2, Line 4<br />
Arithmetic overflow error converting expression to data type tinyint.</span><br />
</em>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/compcol2.jpg" alt="" width="515" height="558" /></p>
<p style="text-align:justify;">What this means is <strong>when SELECT statement is executed Computed Columns is built/calculated. </strong>This observation is interesting as well also makes sense. Computed Column is like addendum to any table and its definition can be changed easily. If computed column does not support any column type there should be no constraint on inserting in the original columns (if their datatype has no conflicts).</p>
<p style="text-align:justify;">So far we have understood what is computed column and how it works and what is its limited. This also gives answer to our original puzzle.</p>
<p style="text-align:justify;"><em><strong>Now the question is what happens when there are more than 256 TINYINT datetypes are as part of definition of the computed column?<br />
</strong></em><br />
Options are:</p>
<p style="text-align:justify;"><span style="color:#339966;"><strong>a) It will error Out.</strong> (Correct Answer)</span><br />
b) It will convert it automatically to next upper range datatype (i.e. INT etc)<br />
<span style="color:#0000ff;">c) <strong>User workaround.</strong> (Preferred Solution)</span>
</p>
<p style="text-align:justify;">Answer to our puzzle is it will error out but it would be really good if we use Workaround and we do not get limited with SQL Server&#8217;s automatic assumption of datatype for Computed Column. If it was regular column we would have changed the datatype of the column if arithmetic overflow had occurred. In case of the computed columns we do not specify datatype of the computed column when we create it.</p>
<p style="text-align:justify;">The solution is very simple and it follow the same pricincipal of which we discussed earlier. <em>When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.</em> We can convert one of the datatype of the our computed column definition to the datatype which we want and it can solve the problem.</p>
<p style="text-align:justify;">In our example we will have 1200 value as final answer which is result of computation (6+6)*100. Datatype TINYINT can not store 1200 value but datatype INT can. Let us run following T-SQL script and understand workaround.</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;">MyTable<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">ID TINYINT </span><span style="color:gray;">NOT NULL </span><span style="color:#434343;">IDENTITY </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">),<br />
</span><span style="color:black;">FirstCol TINYINT </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">SecondCol TINYINT </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">ThirdCol TINYINT </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">ComputedCol </span><span style="color:blue;">AS </span><span style="color:gray;">(</span><span style="color:black;">FirstCol</span><span style="color:gray;">+</span><span style="color:black;">SecondCol</span><span style="color:gray;">)*</span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">ThirdCol </span><span style="color:blue;">AS INT</span><span style="color:gray;">)<br />
) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">MyTable<br />
</span><span style="color:gray;">(</span><span style="color:black;">[FirstCol]</span><span style="color:gray;">,</span><span style="color:black;">[SecondCol]</span><span style="color:gray;">,</span><span style="color:black;">[ThirdCol]</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">6</span><span style="color:gray;">,</span><span style="color:black;">6</span><span style="color:gray;">,</span><span style="color:black;">100</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">MyTable<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">MyTable<br />
GO</span></code>
</p>
<p style="text-align:justify;">In our example in <strong>the definition of the computed column one of the columns datatype is converted to INT, as result whole computed column is converted or escalated INT.</strong></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/compcol3.jpg" alt="" width="500" height="505" /></p>
<p style="text-align:justify;">This was one interesting puzzle to solve. The summary is very simple but keeping this concept in focus will help Database Developer who have to deal with different datatype operations.</p>
<p style="text-align:justify;">My friend Jacob has also pointed out that this concept is also applied while using CASE statement. We will understand that concept with example in future articles. I suggest following related reading along with this article.</p>
<h3 style="text-align:justify;"><a href="http://www.pinaldave.com/blogfolder/ComputedCols.zip" target="_blank">Download all the script used in this article.</a></h3>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/26/sql-server-division-by-float/" target="_blank">SQL SERVER &#8211; Get Answer in Float When Dividing of Two Integer</a><br />
<a href="http://blog.sqlauthority.com/2008/09/27/sql-server-puzzle-computed-columns-datatype-explanation/" target="_blank">SQL SERVER &#8211; Puzzle &#8211; Computed Columns Datatype Explanation</a><br />
<a href="http://sqlauthority.wordpress.com/2008/09/29/sql-server-puzzle-solution-computed-columns-datatype-explanation/" target="_blank">SQL SERVER &#8211; Puzzle &#8211; Solution &#8211; Computed Columns Datatype Explanation</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 Database, MVP, Pinal Dave, SQL, SQL Authority, SQL Documentation, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: Computed Columns <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1326/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1326&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/29/sql-server-puzzle-solution-computed-columns-datatype-explanation/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/blogfolder/compcol1.jpg" medium="image" />

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

		<media:content url="http://www.pinaldave.com/blogfolder/compcol3.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Puzzle &#8211; Computed Columns Datatype Explanation</title>
		<link>http://blog.sqlauthority.com/2008/09/27/sql-server-puzzle-computed-columns-datatype-explanation/</link>
		<comments>http://blog.sqlauthority.com/2008/09/27/sql-server-puzzle-computed-columns-datatype-explanation/#comments</comments>
		<pubDate>Sat, 27 Sep 2008 01:30:31 +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 Documentation]]></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>
		<category><![CDATA[Computed Columns]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1314</guid>
		<description><![CDATA[Yesterday I wrote post about SQL SERVER &#8211; Get Answer in Float When Dividing of Two Integer. I received excellent comment from SQL Server MVP Jacob Sebastian. Jacob has clarified the concept which I was trying to convey. He is famous for his &#8220;behind the scene insight&#8220;.
When I read his comment, I realize another interesting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1314&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Yesterday I wrote post about <a href="http://blog.sqlauthority.com/2008/09/26/sql-server-division-by-float/" target="_blank">SQL SERVER &#8211; Get Answer in Float When Dividing of Two Integer</a>. I received excellent <a href="http://blog.sqlauthority.com/2008/09/26/sql-server-division-by-float/#comment-43276" target="_blank">comment</a> from SQL Server <strong>MVP <a href="http://www.sqlserverandxml.com/" target="_blank">Jacob Sebastian</a></strong>. Jacob has clarified the concept which I was trying to convey. He is famous for his &#8220;<strong>behind the scene insight</strong>&#8220;.</p>
<p style="text-align:justify;">When I read his comment, I realize another interesting concept which is related to same idea which is being discussed in this post. Let us read what Jacob says first.</p>
<p style="text-align:justify;"><strong>Jacob Sebastian:</strong></p>
<p style="padding-left:30px;text-align:justify;"><em>Nice post and something that is very much useful in the day-to-day programming life.</em></p>
<p style="padding-left:30px;text-align:justify;"><em>Just wanted to add to what is already explained, that, SQL Server automatically does a cast to the data type having the highest precedence. So the result of INT and INT will be INT, but INT and FLOAT will be FLOAT because FLOAT has a higher precedence. If you want a different data type, you need to do an EXPLICIT cast.</em></p>
<p style="padding-left:30px;text-align:justify;"><em>For example:</em></p>
<p style="padding-left:30px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">100</span><span style="color:gray;">/</span><span style="color:black;">3 </span><span style="color:blue;">AS </span><span style="color:black;">Result<br />
</span><span style="color:green;">/*<br />
Result<br />
———–<br />
33<br />
*/</span></code></p>
<p style="text-align:justify;"><span style="color:blue;">SELECT </span><span style="color:black;">100</span><span style="color:gray;">/</span><span style="color:black;">3.0 </span><span style="color:blue;">AS </span><span style="color:black;">Result<br />
</span><span style="color:green;">/*<br />
Result<br />
—————————————<br />
33.333333<br />
*/<br />
</span>
</p>
<p style="padding-left:30px;text-align:justify;"><em>The precedence of each data type is documented here: <a href="http://msdn.microsoft.com/en-us/library/ms190309(SQL.90).aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms190309(SQL.90).aspx</a></em></p>
<p style="text-align:justify;">When I read his comment I realize the same concept is applied to when computed columns are created.</p>
<p style="text-align:justify;"><strong>A computed column</strong> is computed from an expression that can use other columns in the same table. The expression can be a noncomputed column name, constant, function, and any combination of these connected by one or more operators. The expression cannot be a subquery. (Definition from <a href="http://technet.microsoft.com/en-us/library/ms191250.aspx" target="_blank">BOL</a>)</p>
<p style="text-align:justify;">When column is added to table as computed column expression is specified for its definition. If expression is addition of two integers, the final answer of datatype INT. This is interesting concept. If more than two columns with datatype of TINYINT, computed columns from this table is also of datatype TINYINT. The unsigned range is 0 to 255. Storage size is 1 byte.</p>
<p style="text-align:justify;"><em><strong>Now the question is what happens when there are more than 256 TINYINT datetypes are as part of definition of the computed column?<br />
</strong></em><br />
Options are:</p>
<p style="text-align:justify;">a) It will error Out.<br />
b) It will convert it automatically to next upper range datatype (i.e. INT etc)<br />
c) User workaround.
</p>
<p style="text-align:justify;">I will let all of you my readers answer this question. I will conver this concept in future articles, where I will explain how to create computed columns. If you select answer c) please post your workaround as well.</p>
<p style="text-align:justify;">UPDATE : <strong><a href="http://blog.sqlauthority.com/2008/09/29/sql-server-puzzle-solution-computed-columns-datatype-explanation/" target="_blank">Solution to Puzzle &#8211; SQL SERVER &#8211; Puzzle &#8211; Solution &#8211; Computed Columns Datatype Explanation</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>
<p style="padding-left:30px;text-align:justify;">
<p style="padding-left:30px;text-align:justify;">
<p style="padding-left:30px;text-align:justify;">
Posted in MVP, Pinal Dave, SQL, SQL Authority, SQL Documentation, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: Computed Columns <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1314/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1314&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/27/sql-server-puzzle-computed-columns-datatype-explanation/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>
	</item>
		<item>
		<title>SQL SERVER &#8211; Simple Puzzle Using Union and Union All &#8211; Answer</title>
		<link>http://blog.sqlauthority.com/2008/03/25/sql-server-simple-puzzle-using-union-and-union-all-answer/</link>
		<comments>http://blog.sqlauthority.com/2008/03/25/sql-server-simple-puzzle-using-union-and-union-all-answer/#comments</comments>
		<pubDate>Tue, 25 Mar 2008 01:30:59 +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 Joins]]></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://sqlauthority.wordpress.com/?p=547</guid>
		<description><![CDATA[Yesterday I posted a puzzle SQL SERVER &#8211; Simple Puzzle Using Union and Union All, today we will see the answer of this. Following image explains the answer of puzzle.

You can read the explanation of why this is answer read my previous article SQL SERVER &#8211; Union vs. Union All &#8211; Which is better for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=547&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Yesterday I posted a puzzle <strong><a href="http://blog.sqlauthority.com/2008/03/24/sql-server-simple-puzzle-using-union-and-union-all/" target="_blank">SQL SERVER &#8211; Simple Puzzle Using Union and Union All</a></strong>, today we will see the answer of this. Following image explains the answer of puzzle.</p>
<p style="text-align:justify;"><img src="http://www.pinaldave.com/blogfolder/puzzlesunion.gif" alt="" width="203" height="439" /></p>
<p style="text-align:justify;">You can read the explanation of why this is answer read my previous article <strong><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></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/547/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/547/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/547/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=547&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/03/25/sql-server-simple-puzzle-using-union-and-union-all-answer/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

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