| Examples |
An outer join combines rows of data from two tables. There are three types of outer joins:
returns all matching rows in both tables, in addition to rows in the left table that have no matching rows in the right table.
returns all matching rows in both tables, in addition to rows in the right table that have no matching rows in the left table.
returns all matching and nonmatching rows from both tables.
In all three types of outer joins, the columns in the result row that are from the unmatched row are set to missing values.
In this example, you first create an inner join that relates employee identification number and salary. Then, you create an outer join that combines this data with data from another table to compute the gross monthly pay for employees who have taken leave.
| Creating a Query View |
You can create an SQL view that contains the syntax of your query. For this example, you use a view to create an outer join query.
In the SQL QUERY TABLES window, select SAMPLE.EMPINFO and SAMPLE.SALARY from the Available Tables list and add them to the Selected Tables list. Select .
In the SQL QUERY COLUMNS window, select NAME, the two ADDRESS items, Identification Number, Employee number, Salary, BEGDATE, and ENDDATE and add them to the Selected Columns list.
Select View
Where
Conditions for Subset to display the WHERE EXPRESSION
window.
Select EMPINFO.Identification Number from the Available Columns list. Select EQ from the list of operators. Select Salary.Identification Number from the Available Columns list. Select .
This WHERE expression creates an inner join of EMPINFO and Salary based
on Identification Number. To save the query as a view, select Tools
Show Query
to display the SQL QUERY window. Select .
![[Creating an SQL view]](images/ex-oj1.gif)
Select the right arrow next to the Library field to display a list of SAS libraries.
![[List of SAS libraries]](images/ex-oj2.gif)
The list of libraries displayed at your site might be different from the ones in the illustration. Select SAMPLE from the Libraries list. Select .
Type MYVIEW in the View field. Select to return to the SQL QUERY window. Select to return to the SQL QUERY COLUMNS window.
| Creating an Outer Join |
Select
Tools
Reset
to reset the query. Select in the dialog
box that appears.
Select SAMPLE.MYVIEW and SAMPLE.LEAVE from the Available Tables list and add them to the Selected Tables list. Select to display the SQL QUERY COLUMNS window.
Select
View
Join
Type.
Select Matched Join and Unmatched Rows (Outer Join). Select to display the Columns for Setting Join Criteria window.
![[Columns for Setting Join Criteria window]](images/ex-oj3.gif)
Select Identification Number from the SAMPLE.MYVIEW Columns (Left) list. Select Identification Number from the SAMPLE.LEAVE Columns (Right) list. Select the down arrow next to Join Type. Select Left from the pop-up menu. Select to return to the SQL QUERY COLUMNS window.
Select View
Distinct to eliminate duplicate values from your output.
Select NAME, Identification Number, and Employee number from the Available Columns list and add them to the Selected Columns list.
![[Selecting columns]](images/ex-oj4.gif)
| Building a Column Expression |
Select to display the BUILD A COLUMN EXPRESSION window.
Select MYVIEW.Salary from the Available Columns list. Select / from the list of operators. Select <CONSTANT enter value> from the Available Columns list. Type 12 in the Numeric field. Select . Select outside the list of operators to dismiss it.
Select Column Attributes to display the Expression Column Attributes window. Enter monthpay in the Alias Name field. Enter dollar12.2 in the Format field. Enter Employee's Monthly Pay in the Label field.
![[Setting attributes for the new column]](images/ex-oj5.gif)
Select to return to the BUILD A COLUMN EXPRESSION window. Select OK to return to the SQL QUERY COLUMNS window.
In the SQL QUERY COLUMNS window, select to display the BUILD A COLUMN EXPRESSION window. Select . Select ( from the list of operators.
Select monthpay from the Available Columns list. Select * from the list of operators. Select LEAVE.Payroll percentage from the Available Columns list. Select ) from the list of operators. Select outside the list of operators to dismiss it.
![[Coimplete column expression]](images/ex-oj6.gif)
Select to display the Expression Column Attributes window. Enter adjstpay in the Alias Name field. Enter dollar12.2 in the Format field. Enter Employee's Gross Pay in the Label field. Select to return to the BUILD A COLUMN EXPRESSION window. Select to return to the SQL QUERY COLUMNS window.
| Order By Columns |
In the SQL QUERY COLUMNS window, select
View
Order By to display
the ORDER BY COLUMNS window.
![[ORDER BY COLUMNS window]](images/ex-oj7.gif)
Select the second Identification Number from the Available Columns list and add it to the Order By Columns list. Select to return to the SQL QUERY COLUMNS window.
| Viewing Your Output |
Select Tools
Run Query
Run
Immediate to display the results of the query in the
Output window.
![[Query output]](images/ex-oj8.gif)
Copyright © 2008 by SAS Institute Inc., Cary, NC, USA. All rights reserved.