<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: sql nesting multiple left outer joins in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496386#M131270</link>
    <description>&lt;P&gt;Applying conditions in outer joins is often tricky. The starting point is always a cartesian product. Then join conditions are applied. Then WHERE conditions do further subsetting. Not&amp;nbsp;necessarily &amp;nbsp;what the programmer expected. You'll get your expected result by applying a WHERE condition first:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;TITLE 'Self Left Join';
PROC SQL;
  SELECT t1.student_id
    ,t1.grade AS calc1_grade
    ,t2.grade AS calc2_grade
  FROM (select * from class_grades where class = 'MTH101') as t1 
  LEFT JOIN (select * from class_grades where class = 'MTH102') as t2
    ON t1.student_id = t2.student_id;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 17 Sep 2018 19:41:04 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-09-17T19:41:04Z</dc:date>
    <item>
      <title>sql nesting multiple left outer joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496279#M131202</link>
      <description>&lt;P&gt;I&amp;nbsp; thought I knew how outer joins worked in SQL, but this simple join has me stumped. After referring to SQL for Smarties: Advanced SQL Programming, 3rd Ed, by Joe Celko (2005), I&amp;nbsp; &amp;nbsp;am convinced that the following query should return three records, but it returns eleven.&amp;nbsp; Am I missing something basic or is SAS not following the rules?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;DATA class_grades;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; INPUT student_id $ class $ grade $ ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; DATALINES;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;101 MTH101 C&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;102 MTH101 B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;103 MTH101 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;104 MTH101 D &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;105 MTH101 C&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;106 MTH103 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;101 MTH102 B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;102 MTH102 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;103 MTH102 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;106 MTH201 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;107 MTH102 B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;RUN;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;TITLE 'Self Left Join';&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;PROC SQL;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; SELECT t1.student_id&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; ,t1.grade AS calc1_grade&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; ,t2.grade AS calc2_grade&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; FROM class_grades t1 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; LEFT JOIN class_grades t2&lt;/FONT&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &lt;FONT color="#FF0000"&gt;ON t1.student_id = t2.student_id &lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; AND t1.class = 'MTH101'&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; AND t2.class = 'MTH102';&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;QUIT;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Title;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;======== OUTPUT =========&lt;/P&gt;
&lt;P&gt;obs&amp;nbsp; student_id&amp;nbsp; &amp;nbsp;mth101_grade&amp;nbsp; mth102_grade&lt;BR /&gt;&amp;nbsp;--------------------&amp;nbsp;&amp;nbsp;&lt;BR /&gt; &lt;FONT face="courier new,courier"&gt;&amp;nbsp;1 101 C&amp;nbsp; B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;2 101 B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;3 102 B&amp;nbsp; A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;4 102 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;5 103 A&amp;nbsp; A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;6 103 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;7 104 D&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;8 105 C&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;9 106 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;10 106 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11 107 B&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;========&lt;FONT color="#0000FF"&gt; &lt;STRONG&gt;DESIRED OUTPUT &lt;/STRONG&gt;&lt;/FONT&gt;=========&lt;/P&gt;
&lt;P&gt;&amp;nbsp;student_id&amp;nbsp; &amp;nbsp;mth101_grade&amp;nbsp; mth102_grade&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;101 C&amp;nbsp; B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;102 B&amp;nbsp; A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;103 A&amp;nbsp; A&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;104 D&amp;nbsp; &amp;lt;blank&amp;gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;- this was added later (Apologies to first respondent)&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;105 C&amp;nbsp; &amp;lt;blank&amp;gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;&amp;lt;- this was added later (Apologies to first respondent)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Sep 2018 18:03:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496279#M131202</guid>
      <dc:creator>optimist</dc:creator>
      <dc:date>2018-09-17T18:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: sql nesting multiple left outer joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496282#M131204</link>
      <description>SQL Join always result in a product which is filtered by any join/where clause. So if you have multiple rows for the join key in one table, the result will have it as well.</description>
      <pubDate>Mon, 17 Sep 2018 15:55:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496282#M131204</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2018-09-17T15:55:05Z</dc:date>
    </item>
    <item>
      <title>Re: sql nesting multiple left outer joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496283#M131205</link>
      <description>&lt;P&gt;A left join brings everything from the first table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An inner join is what you want here, or use a WHERE to filter the data (ON won't filter, just controls the JOIN conditions).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;TITLE 'Self Left Join';
PROC SQL;
  SELECT t1.student_id
    ,t1.grade AS calc1_grade
    ,t2.grade AS calc2_grade
  FROM class_grades t1 
  left JOIN class_grades t2
    ON t1.student_id = t2.student_id 
      &lt;FONT size="5" color="#800080"&gt;&lt;STRONG&gt;WHERE&lt;/STRONG&gt;&lt;/FONT&gt; t1.class = 'MTH101'
      AND t2.class = 'MTH102';
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;OR&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;TITLE 'Self Left Join';
PROC SQL;
  SELECT t1.student_id
    ,t1.grade AS calc1_grade
    ,t2.grade AS calc2_grade
  FROM class_grades t1 
&lt;FONT size="5" color="#800080"&gt;&lt;STRONG&gt;inner&lt;/STRONG&gt; &lt;/FONT&gt;JOIN class_grades t2
    ON t1.student_id = t2.student_id 
      and t1.class = 'MTH101'
      AND t2.class = 'MTH102';
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21425"&gt;@optimist&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I&amp;nbsp; thought I knew how outer joins worked in SQL, but this simple join has me stumped. After referring to SQL for Smarties: Advanced SQL Programming, 3rd Ed, by Joe Celko (2005), I&amp;nbsp; &amp;nbsp;am convinced that the following query should return three records, but it returns eleven.&amp;nbsp; Am I missing something basic or is SAS not following the rules?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;DATA class_grades;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; INPUT student_id $ class $ grade $ ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; DATALINES;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;101 MTH101 C&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;102 MTH101 B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;103 MTH101 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;104 MTH101 D &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;105 MTH101 C&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;106 MTH103 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;101 MTH102 B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;102 MTH102 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;103 MTH102 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;106 MTH201 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;107 MTH102 B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;RUN;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;TITLE 'Self Left Join';&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;PROC SQL;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; SELECT t1.student_id&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; ,t1.grade AS calc1_grade&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; ,t2.grade AS calc2_grade&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; FROM class_grades t1 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; LEFT JOIN class_grades t2&lt;/FONT&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &lt;FONT color="#FF0000"&gt;ON t1.student_id = t2.student_id &lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; AND t1.class = 'MTH101'&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; AND t2.class = 'MTH102';&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;QUIT;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Title;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;======== OUTPUT =========&lt;/P&gt;
&lt;P&gt;obs&amp;nbsp; student_id&amp;nbsp; &amp;nbsp;mth101_grade&amp;nbsp; mth102_grade&lt;BR /&gt;&amp;nbsp;--------------------&amp;nbsp;&amp;nbsp;&lt;BR /&gt; &lt;FONT face="courier new,courier"&gt;&amp;nbsp;1 101 C&amp;nbsp; B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;2 101 B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;3 102 B&amp;nbsp; A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;4 102 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;5 103 A&amp;nbsp; A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;6 103 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;7 104 D&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;8 105 C&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;9 106 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;10 106 A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11 107 B&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;========&lt;FONT color="#0000FF"&gt; &lt;STRONG&gt;DESIRED OUTPUT &lt;/STRONG&gt;&lt;/FONT&gt;=========&lt;/P&gt;
&lt;P&gt;&amp;nbsp;student_id&amp;nbsp; &amp;nbsp;mth101_grade&amp;nbsp; mth102_grade&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;101 C&amp;nbsp; B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;102 B&amp;nbsp; A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;103 A&amp;nbsp; A&lt;/FONT&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Sep 2018 15:56:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496283#M131205</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-17T15:56:26Z</dc:date>
    </item>
    <item>
      <title>Re: sql nesting multiple left outer joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496346#M131250</link>
      <description>&lt;P&gt;Reeza,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the quick reply.&amp;nbsp; &amp;nbsp;I made a mistake in saying what I wanted as my desired output. (Sorry!&amp;nbsp; I had to map my real query to the one shown below and screwed up on the desired results.)&amp;nbsp; I should have said I wanted to see:&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;101 C&amp;nbsp; B&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;102 B&amp;nbsp; A&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;103 A&amp;nbsp; A&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;104 D&lt;SPAN&gt;&amp;nbsp; &amp;lt;Blank&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;105 C&amp;nbsp; &amp;lt;Blank&amp;gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;In code, I could have done it this way:&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;PROC SQL;&lt;BR /&gt;&amp;nbsp; SELECT t1.student_id&lt;BR /&gt;&amp;nbsp; &amp;nbsp; ,t1.grade AS calc1_grade&lt;BR /&gt;&amp;nbsp; &amp;nbsp; ,t2.grade AS calc2_grade&lt;BR /&gt;&amp;nbsp; FROM class_grades &lt;FONT color="#FF0000"&gt;(WHERE=(&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;class = 'MTH101'))&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp; t1&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp; LEFT JOIN class_grades &lt;FONT color="#FF0000"&gt;(WHERE=(&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;class = 'MTH102'&lt;/FONT&gt;&lt;/STRONG&gt;))&lt;/FONT&gt; t2&lt;BR /&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT color="#000000"&gt;ON t1.student_id = t2.student_id&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;QUIT;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But I'm trying to find out if there is a problem with how I am thinking about joins. According to the reference I cited, I thought the first query I showed (without the SAS dataset options) should work for a self join.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Sep 2018 17:58:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496346#M131250</guid>
      <dc:creator>optimist</dc:creator>
      <dc:date>2018-09-17T17:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: sql nesting multiple left outer joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496386#M131270</link>
      <description>&lt;P&gt;Applying conditions in outer joins is often tricky. The starting point is always a cartesian product. Then join conditions are applied. Then WHERE conditions do further subsetting. Not&amp;nbsp;necessarily &amp;nbsp;what the programmer expected. You'll get your expected result by applying a WHERE condition first:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;TITLE 'Self Left Join';
PROC SQL;
  SELECT t1.student_id
    ,t1.grade AS calc1_grade
    ,t2.grade AS calc2_grade
  FROM (select * from class_grades where class = 'MTH101') as t1 
  LEFT JOIN (select * from class_grades where class = 'MTH102') as t2
    ON t1.student_id = t2.student_id;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Sep 2018 19:41:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-nesting-multiple-left-outer-joins/m-p/496386#M131270</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-17T19:41:04Z</dc:date>
    </item>
  </channel>
</rss>

