{"id":862,"date":"2022-08-04T06:47:41","date_gmt":"2022-08-04T06:47:41","guid":{"rendered":"https:\/\/craftcookcode.com\/?p=862"},"modified":"2024-07-10T05:05:46","modified_gmt":"2024-07-10T05:05:46","slug":"the-fake-left-outer-join","status":"publish","type":"post","link":"https:\/\/craftcookcode.com\/?p=862","title":{"rendered":"The Fake LEFT OUTER JOIN"},"content":{"rendered":"\n<p>In my role I review a wide variety of SQL code.<\/p>\n\n\n\n<p>Some is highly professional that is above my skill level so I absorb as much learning as I can from it.<\/p>\n\n\n\n<p>On the other side is self taught SQLers from the business world who are trying to build weird and wonderful reports with SQL held together with sticky tape and don&#8217;t truly comprehend the code that is written.<\/p>\n\n\n\n<p>An example &#8211; I see the following a lot with people who don&#8217;t understand the data and trying to program stability in their code like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT \n    A.BDATE,\n    A.ID,\n    A.CUST_ID,\n    C.CUSTOMER_NAME,\n    C.PHONE\nFROM dbo.ACCOUNTS A\nLEFT OUTER JOIN dbo.CUSTOMER C\nON   C.BDATE = @DATE\n     C.CUST_ID = A.CUST_ID \nWHERE\n    A.BDATE = @DATE AND\n    A.ACCOUNT_TYPE = &#039;XX&#039;;\n<\/pre><\/div>\n\n\n<p>This means if for some reason if there is an integrity problem between ACCOUNTS and CUSTOMER; they will still get the account data on the report.<\/p>\n\n\n\n<p>The problem arises when the business requirement of the report evolve and the developer needs to filters on the left joined table.&nbsp;<\/p>\n\n\n\n<p>For instance &#8211; &#8220;Report on Accounts on type XX but remove deceased customers so we don&#8217;t ring them&#8221;&nbsp;<\/p>\n\n\n\n<p>I quite often see this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT \n    A.BDATE,\n    A.ID,\n    A.CUST_ID,\n    C.CUSTOMER_NAME,\n    C.PHONE\nFROM dbo.ACCOUNTS A\nLEFT OUTER JOIN dbo.CUSTOMER C\nON   C.BDATE = @DATE\n     C.CUST_ID = A.CUST_ID \nWHERE\n    A.BDATE = @DATE AND\n    A.ACCOUNT_TYPE = &#039;XX&#039; \n\n    AND\n\n    C.DECEASED = &#039;N&#039;;\n<\/pre><\/div>\n\n\n<p>Without knowing it; the developer has broken their &#8220;Get all account XX report&#8221; business rule by turning their LEFT OUTER JOIN into effectively a INNER JOIN.<\/p>\n\n\n\n<p>The correct way to write the query is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT \n    A.BDATE,\n    A.ID,\n    A.CUST_ID,\n    C.CUSTOMER_NAME,\n    C.PHONE\nFROM dbo.ACCOUNTS A\nLEFT OUTER JOIN dbo.CUSTOMER C\nON  C.BDATE = @DATE\n    C.CUST_ID = A.CUST_ID \n\n    -------------------------------- \n    AND\n\n    C.DECEASED = &#039;N&#039;\n    -------------------------------- \nWHERE\n    A.BDATE = @DATE AND\n    A.ACCOUNT_TYPE = &#039;XX&#039; ;\n<\/pre><\/div>\n\n\n<p>You can get a good gauge of the skill level of the developer by checking their LEFT OUTER JOINS.&nbsp; If they using them on every join where you know there is integrity between the two data objects; you can have an educated guess that you&#8217;re reviewing a novice developer.<\/p>\n\n\n\n<p>This is where you have to pay extra attention to their joins and where predicates and make them aware of the consequences of the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my role I review a wide variety of SQL code. Some is highly professional that is above my skill level so I absorb as much&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[13,17],"tags":[],"class_list":["post-862","post","type-post","status-publish","format-standard","hentry","category-code","category-sql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts\/862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=862"}],"version-history":[{"count":6,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts\/862\/revisions"}],"predecessor-version":[{"id":1462,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts\/862\/revisions\/1462"}],"wp:attachment":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}