Joins a table with itself or with other tables or views.
Restriction: | Joins are limited to 256 tables. |
See: | FROM Clause |
query-expression | |
Examples: | Joining Two Tables
Producing All the Possible Combinations of the Values in a Column |
specifies an alias for table-name. The AS keyword is optional.
is described in sql-expression.
can be one of the following:
data lefttab; input Continent $ Export $ Country $; datalines; NA wheat Canada EUR corn France EUR rice Italy AFR oil Egypt ; data righttab; input Continent $ Export $ Country $; datalines; NA sugar USA EUR corn Spain EUR beets Belgium ASIA rice Vietnam ;
proc sql; title 'Left Table - LEFTTAB'; select * from lefttab; title 'Right Table - RIGHTTAB'; select * from righttab;
Left Table - LEFTTAB Continent Export Country ----------------------------- NA wheat Canada EUR corn France EUR rice Italy AFR oil Egypt
Right Table - RIGHTTAB Continent Export Country ----------------------------- NA sugar USA EUR corn Spain EUR beets Belgium ASIA rice Vietnam
proc sql; title 'The Cartesian Product of'; title2 'LEFTTAB and RIGHTTAB'; select * from lefttab, righttab;
proc sql; title 'Inner Join'; select * from lefttab as l, righttab as r where l.continent=r.continent;
proc sql; title 'Left Outer Join'; select * from lefttab as l left join righttab as r on l.continent=r.continent;
proc sql; title 'Right Outer Join'; select * from lefttab as l right join righttab as r on l.continent=r.continent;
proc sql; title 'Full Outer Join'; select * from lefttab as l full join righttab as r on l.continent=r.continent;
proc sql; title 'Cross Join'; select * from lefttab as l cross join righttab as r;
proc sql; title 'Union Join'; select * from lefttab union join righttab;
data table1; input x y z; datalines; 1 2 3 2 1 8 6 5 4 2 5 6 ; data table2; input x b z; datalines; 1 5 3 3 5 4 2 7 8 6 0 4 ;
proc sql; title 'Natural Inner Join'; select * from table1 natural join table2;
a natural join b natural join c a natural join b cross join c
(a, b) left join c on a.X=c.Y a left join (b full join c on b.Z=c.Z) on a.Y=b.Y
data comm; input Continent $ Export $ Country $ ; datalines; NA wheat Canada EUR corn France EUR rice Italy AFR oil Egypt ; data price; input Export $ Price; datalines; rice 3.56 corn 3.45 oil 18 wheat 2.98 ; data amount; input Country $ Quantity; datalines; Canada 16000 France 2400 Italy 500 Egypt 10000 ;
proc sql; title 'COMM Table'; select * from comm; title 'PRICE Table'; select * from price; title 'AMOUNT Table'; select * from amount;
proc sql; select IDNumber, Birth from proclib.payroll where IDNumber in (select idnum from proclib.staff where lname like 'B%'); proc sql; select p.IDNumber, p.Birth from proclib.payroll p, proclib.staff s where p.idnumber=s.idnum and s.lname like 'B%';