<?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: WHERE and JOIN order of execution in PROC SQL in Advanced Programming</title>
    <link>https://communities.sas.com/t5/Advanced-Programming/WHERE-and-JOIN-order-of-execution-in-PROC-SQL/m-p/992838#M391</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/479274"&gt;@hcstritz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;...&lt;BR /&gt;On page 68: "In all types of joins, PROC SQL generates a Cartesian product first, and then eliminates rows that do not meet any subsetting criteria that you have specified."&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is what &lt;STRONG&gt;I think&lt;/STRONG&gt;.&lt;BR /&gt;Conceptually ( ! ) that statement is correct.&lt;BR /&gt;But physically (physical execution) ... the "query optimizer" evaluates your code to avoid building massive, inefficient Cartesian products in memory or on disk. So, i&lt;SPAN&gt;n most cases the Cartesian product will never materialize and the query optimizer will use a different approach.&lt;/SPAN&gt;&lt;BR /&gt;In general the where-clause acts as an input filter and not an output filter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do not see the Cartesian Product Log Message (see below), then you are good and&amp;nbsp;&lt;SPAN&gt;the full Cartesian product of the participating tables was not built.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;NOTE: The execution of this query involves performing one or more Cartesian 
      product joins that can not be optimized.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/v_078/sqlproc/p0o4a5ac71mcchn1kc1zhxdnm139.htm" target="_blank"&gt;Selecting Data from More Than One Table By Using Joins : SAS Help Center&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.youtube.com/watch?v=afICXE5iZYo" target="_blank"&gt;SAS Tutorial | Mastering the WHERE Clause in PROC SQL - YouTube&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
    <pubDate>Mon, 31 Aug 2026 22:08:08 GMT</pubDate>
    <dc:creator>sbxkoenk</dc:creator>
    <dc:date>2026-08-31T22:08:08Z</dc:date>
    <item>
      <title>WHERE and JOIN order of execution in PROC SQL</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/WHERE-and-JOIN-order-of-execution-in-PROC-SQL/m-p/992835#M389</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I have a question about a statement in the SAS Certified Professional Prep Guide - Advanced Programming Using SAS 9.4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought that WHERE statements were evaluated after tables are joined in a PROC SQL query. On page 68: "In all types of joins, PROC SQL generates a Cartesian product first, and then eliminates rows that do not meet any subsetting criteria that you have specified."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when discussing outer joins on page 79, the text reads, "Note: To further subset the rows in the query output, you can follow the ON clause with a WHERE clause.&amp;nbsp;&lt;STRONG&gt;The WHERE clause subsets the individual detail rows before the outer join is performed&lt;/STRONG&gt;.&amp;nbsp;&lt;STRONG&gt;The ON clause then specifies how the remaining rows are to be selected for output.&lt;/STRONG&gt;"&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I might be missing something, but these statements seem contradictory. Are there situations where a WHERE clause (not dataset option) applies before the join is performed?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Aug 2026 20:00:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/WHERE-and-JOIN-order-of-execution-in-PROC-SQL/m-p/992835#M389</guid>
      <dc:creator>hcstritz</dc:creator>
      <dc:date>2026-08-31T20:00:38Z</dc:date>
    </item>
    <item>
      <title>Re: WHERE and JOIN order of execution in PROC SQL</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/WHERE-and-JOIN-order-of-execution-in-PROC-SQL/m-p/992837#M390</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/479274"&gt;@hcstritz&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Have a look at these two papers&lt;/P&gt;&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/101-30.pdf" target="_blank" rel="noopener"&gt;101-30: The SQL Optimizer Project: _Method and _Tree in SAS®9.1&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings13/200-2013.pdf" target="_blank" rel="noopener"&gt;200-2013: Exploring the PROC SQL _METHOD Option&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an additional link to look at in order to understand the typical order of SQL statements execution&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.datacamp.com/tutorial/sql-order-of-execution" target="_blank"&gt;SQL Order of Execution: Understanding How Queries Run | DataCamp&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt; SAS Proc SQL does not support limit nor offset statements!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;They should provide a nice explanation for what you are asking about&lt;/P&gt;&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Mon, 31 Aug 2026 21:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/WHERE-and-JOIN-order-of-execution-in-PROC-SQL/m-p/992837#M390</guid>
      <dc:creator>ahmedalattar</dc:creator>
      <dc:date>2026-08-31T21:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: WHERE and JOIN order of execution in PROC SQL</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/WHERE-and-JOIN-order-of-execution-in-PROC-SQL/m-p/992838#M391</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/479274"&gt;@hcstritz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;...&lt;BR /&gt;On page 68: "In all types of joins, PROC SQL generates a Cartesian product first, and then eliminates rows that do not meet any subsetting criteria that you have specified."&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is what &lt;STRONG&gt;I think&lt;/STRONG&gt;.&lt;BR /&gt;Conceptually ( ! ) that statement is correct.&lt;BR /&gt;But physically (physical execution) ... the "query optimizer" evaluates your code to avoid building massive, inefficient Cartesian products in memory or on disk. So, i&lt;SPAN&gt;n most cases the Cartesian product will never materialize and the query optimizer will use a different approach.&lt;/SPAN&gt;&lt;BR /&gt;In general the where-clause acts as an input filter and not an output filter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do not see the Cartesian Product Log Message (see below), then you are good and&amp;nbsp;&lt;SPAN&gt;the full Cartesian product of the participating tables was not built.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;NOTE: The execution of this query involves performing one or more Cartesian 
      product joins that can not be optimized.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/v_078/sqlproc/p0o4a5ac71mcchn1kc1zhxdnm139.htm" target="_blank"&gt;Selecting Data from More Than One Table By Using Joins : SAS Help Center&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.youtube.com/watch?v=afICXE5iZYo" target="_blank"&gt;SAS Tutorial | Mastering the WHERE Clause in PROC SQL - YouTube&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Mon, 31 Aug 2026 22:08:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/WHERE-and-JOIN-order-of-execution-in-PROC-SQL/m-p/992838#M391</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2026-08-31T22:08:08Z</dc:date>
    </item>
  </channel>
</rss>

