Using Natural Joins

A Brief Overview

A natural join automatically selects columns from each table to match rows. With a natural join, PROC SQL identifies columns in each table that have the same name and type, and uses those as the join criteria. The advantage of using a natural join is that the coding is streamlined. The ON clause is implied, and you do not need to use table aliases to qualify column names that are common to both tables.
If you specify a natural join on tables that do not have at least one column with a common name and type, the result is a Cartesian product. You can use a WHERE clause to limit the output. Because the natural join makes certain assumptions about what you want to accomplish, you should examine your data thoroughly before using it.

Natural Join Syntax

Syntax, SELECT statement for outer join:
SELECT column-1<,...column-n>
FROM table-1 | view-1 NATURAL JOIN table-2 | view-2
<other clauses>;
NATURAL JOIN
specifies the type of join.
table
specifies the name of the source table.
<other clauses>
refers to optional PROC SQL clauses.
Note: Do not use an ON clause with a natural join. When using a natural join, an ON clause is implied, matching all like columns. You can use a WHERE clause to subset the query results. A natural join functions the same as a qualified join with the USING clause. A natural join is a shorthand form of USING. As is the case with USING, like columns appear only once in the result set.

Example: Using a Natural Join

A natural join assumes that you want to base the join on equal values of all pairs of all common columns. The following example selects all columns from Certadv.Schedule and Certadv.Courses and creates an output using a natural join.
proc sql;
   select *
      from certadv.schedule natural join
           certadv.courses
;
quit;
PROC SQL identified Course_Code as the common column between Certadv.Schedule and Certadv.Courses. Based on that column, the natural join selected columns from each table to match the rows.
Output 3.9 PROC SQL Query Result: Natural Join
PROC SQL Query Result: Natural Join
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset