<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'Best Practices', 'ANSI-92', and 'habits'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Best+Practices,ANSI-92,habits&amp;orTags=0</link><description>Search results matching tags 'Best Practices', 'ANSI-92', and 'habits'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Bad habits to kick : using old-style JOINs</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins.aspx</link><pubDate>Thu, 08 Oct 2009 15:26:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17366</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;&lt;i&gt;In my &lt;a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/07/bad-habits-to-kick-using-a-loop-to-populate-a-table.aspx" title="http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/07/bad-habits-to-kick-using-a-loop-to-populate-a-table.aspx" target="_blank"&gt;last post in this series&lt;/a&gt;, I talked about using simple loops to populate large tables.&amp;nbsp; This time I'd like to focus on getting rid of old, ANSI-89 joins.&lt;/i&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;I am sure most veterans know better than to use old ANSI-89 JOIN syntax, such as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderID&lt;/font&gt;&lt;font color="gray"&gt;,&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;od.ProductID&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o&lt;/font&gt;&lt;font color="gray"&gt;,&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.OrderDetails&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;od&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  WHERE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderDate&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;&amp;gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'20091001'&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;  AND&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderID&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;od.ProductID&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;



&lt;p&gt;One reason to avoid this syntax is that the query is often less readable than when you separate the joining criteria from the filter criteria.&amp;nbsp; For example, the above query re-written to use a "proper" join becomes: &lt;br&gt;&lt;/p&gt;&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderID&lt;/font&gt;&lt;font color="gray"&gt;,&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;od.ProductID&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  INNER&amp;nbsp;JOIN&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.OrderDetails&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;od&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  ON&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderID&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;od.ProductID&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  WHERE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderDate&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;&amp;gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'20091001'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;

&lt;p&gt;This is subjective, of course, but seems to be a consensus.&amp;nbsp; Now, bring in four or five more tables to the query, and then try to debug when you are not getting the expected number of rows.&amp;nbsp; When the joined-in tables are grouped together with their joining criteria, commenting them out or changing the criteria becomes a much simpler task. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;The OUTER JOIN version of this syntax (which uses *=, =*) is &lt;a href="http://msdn.microsoft.com/en-us/library/ms143729.aspx" title="http://msdn.microsoft.com/en-us/library/ms143729.aspx" target="_blank"&gt;already on the deprecation list&lt;/a&gt;, so there is another reason to avoid the syntax in general.&amp;nbsp; Many people have memorized which is which, but deprecation aside, isn't it better to explicitly state what you are doing?&amp;nbsp; For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;p.ProductName&lt;/font&gt;&lt;font color="gray"&gt;,&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;p.ProductID&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.Products&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;p&lt;br&gt;&lt;/font&gt;&lt;font color="magenta"&gt;  &lt;/font&gt;&lt;font color="gray"&gt;LEFT OUTER&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;JOIN&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.OrderDetails&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;od&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  ON&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;p.ProductID&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;od.ProductID&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  WHERE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;od.ProductID&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;IS&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;

&lt;p&gt;There are also documented cases where the old-style outer joins produce incorrect results depending on the ordering of evaluation of the "filter" criteria (which happened to include the joining criteria).&amp;nbsp; Largely a moot point since *= / =* is deprecated, but I wanted to point it out all the same.&amp;nbsp; After all, since I know many of us will be on 2000, 2005 or 2008 for some time, we *could* continue using this syntax if we wanted to.&amp;nbsp; But I don't think we should.&lt;br&gt;&lt;/p&gt;



&lt;p&gt;Are you sold yet?&amp;nbsp; No?&amp;nbsp; Well, there is an even bigger potential problem in using the implicit INNER JOIN syntax.&amp;nbsp; What if you forget the JOIN criteria in the WHERE clause?&amp;nbsp; You've turned it into a CROSS JOIN (otherwise known as the Cartesian Product).&amp;nbsp; Take the first query again, and leave out the last line: &lt;br&gt;&lt;/p&gt;&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderID&lt;/font&gt;&lt;font color="gray"&gt;,&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;od.ProductID&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o&lt;/font&gt;&lt;font color="gray"&gt;,&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.OrderDetails&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;od&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  WHERE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderDate&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;&amp;gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'20091001'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;

&lt;p&gt;For every single order after October 1st, you're going to get a copy of every single row from OrderDetails *for all of time* since you've lost the joining criteria *and* the filtering criteria for that table.&amp;nbsp; This could be a disastrous mistake on a production system, and while it can be caught relatively quickly after deployment and testing, it can be prevented entirely at compile time, since an error will be thrown if you leave out the ON clause for an INNER JOIN.&amp;nbsp; Sure, you can still get your join criteria wrong and push the change without noticing (for example, ON o.OrderID = o.OrderID), but with the newer syntax, there's one less thing that is likely to go wrong.&lt;/p&gt;

&lt;p&gt;My biggest beef about trying to help people kick these habits is that Microsoft themselves keep publishing sample code that promotes the syntax.&amp;nbsp; Again, this isn't so much a problem for the veterans (except those with insurmountable inertia), who fully understand how JOINs work, and know the history of ANSI-89 and ANSI-92.&amp;nbsp; But for someone brand new to SQL Server, they will pick up a current copy of Books Online, and see sample queries that enforce these coding standards.&amp;nbsp; The other day, &lt;a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=496012" title="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=496012" target="_blank"&gt;I complained about this on Connect&lt;/a&gt;, when I came across yet another example of old-style joins in the SQL Server 2008 version of Books Online.&amp;nbsp; They quickly agreed that, when they review the documentation for 2008 R2, they will update any code samples they deem "suspect."&lt;/p&gt;&lt;p&gt;&lt;i&gt;I am working on a series of "Bad habits to kick" articles, in an
effort to motivate people to drop some of the things that I hate to see
when I inherit code.&amp;nbsp; Up next: &lt;a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-table-aliases-like-a-b-c-or-t1-t2-t3.aspx" title="http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-table-aliases-like-a-b-c-or-t1-t2-t3.aspx" target="_blank"&gt;using table aliases like (a, b, c) or (t1, t2, t3)&lt;/a&gt;.&lt;/i&gt; &lt;br&gt;&lt;/p&gt;</description></item></channel></rss>