Syntax, SELECT statement
for inner join:
SELECT column-1<,...column-n>
FROM table-1
| view-1 INNER JOIN table-2
| view-2 <INNER JOIN...table-n | view-n>
ON table1.column=table2.column
<other
clauses>;
INNER JOIN
specifies the join
type.
table
specifies the name
of the source table.
ON
specifies join conditions,
which are expressions that specify the column or columns on which
the tables are to be joined.
table.column
refers to the source
table and the column name on which the join occurs.
<other clauses>
refers to optional
PROC SQL clauses.
|
proc sql; select * from certadv.one inner join certadv.two on one.x=two.x; quit;
on table1.id = table2.empid
proc sql; select * from certadv.one inner join certadv.two on one.x=two.x; quit;
proc sql;
select one.x, a, b
from certadv.one inner join certadv.two
on one.x=two.x;
quit;
proc sql;
select one.*, b
from certadv.one inner join certadv.two
on one.x=two.x;
quit;
proc sql;
select one.x as ID, two.x, a, b
from certadv.one inner join certadv.two
on one.x=two.x;
quit;
2
for
column X. proc sql; select * from certadv.three inner join certadv.four on three.x=four.x; quit;
proc sql; title 'Employee Names and Job Codes'; select staffmaster.empid, lastname, firstname, jobcode from certadv.staffmaster inner join certadv.payrollmaster on staffmaster.empid=payrollmaster.empid; quit;
proc sql; title 'Employee Names and Job Codes'; select s.empid, lastname, firstname, jobcode from certadv.staffmaster as s inner join certadv.payrollmaster as p on s.empid=p.empid; quit;
from certadv.staffmaster s, certadv.payrollmaster p
When Are Table Aliases
Required?
|
Example
|
---|---|
a table is joined to
itself (called a self-join or reflexive
join)
|
from certadv.staffmaster as s1, certadv.staffmaster as s2 |
you need to reference
columns from same-named tables in different libraries
|
from certadv.flightdelays as af, certadvf.flightdelays as wf on af.delay > wf.delay |
Table
|
Relevant Columns
|
---|---|
Certadv.Staffmaster
|
EmpID, LastName, FirstName,
State
|
Certadv.Payrollmaster
|
EmpId, JobCode, DateOfBirth
|
proc sql outobs=15; title 'New York Employees'; select substr(firstname,1,1) || '. ' || lastname /*1*/ as Name, jobcode, int((today() - dateofbirth)/365.25) as Age from certadv.payrollmaster as p inner join /*2*/ certadv.staffmaster as s on p.empid = /*3*/ s.empid where state='NY' /*4*/ order by 2,3 /*5*/ ; quit;
1 | The SELECT clause specifies the
new column Name, the existing column JobCode, and the new column Age.
The SELECT clause uses
functions and expressions to create two new columns.
|
2 | The FROM clause lists the tables to select from. The FROM clause uses the AS keyword to distinguish a table alias from other table names. The FROM clause also specifies the INNER JOIN keyword to specify the join type. |
3 | The ON clause describes the join criteria for matching rows in the tables. The ON clause produces a match for the rows with identical values in the EmpID column. |
4 | The WHERE clause subsets the data
further by selecting only the rows where State is NY . |
5 | The ORDER BY clause specifies the order in which rows are displayed in the result table. |
NY
.
You do not need to prefix the column name State with a table name
because State occurs in only one of the tables.proc sql outobs=15; title 'Average Age of New York Employees'; select jobcode,count(p.empid) as Employees, /*1*/ avg(int((today() - dateofbirth)/365.25)) format=4.1 as AvgAge from certadv.payrollmaster as p inner join /*2*/ certadv.staffmaster as s on p.empid= /*3*/ s.empid where state='NY' /*4*/ group by jobcode /*5*/ order by jobcode /*6*/ ; quit;
1 | The SELECT clause uses summary
functions to create two new columns.
|
2 | The FROM clause lists the tables to select from. The FROM clause uses the AS keyword to distinguish a table alias from other table names. The FROM clause also specifies the INNER JOIN keyword to specify the join type. |
3 | The ON clause describes the join criteria for matching rows in the tables. The ON clause produces a match for the rows with identical values in the EmpID column. |
4 | The WHERE clause subsets the data
further by selecting only the rows where State is NY . |
5 | The GROUP BY clause specifies what variable to group the data by for summarization. |
6 | The ORDER BY clause specifies the order in which rows are displayed in the result table. |