Left outer join
A left outer join returns all the rows that an inner join returns plus one row for each of the other rows in the first table that do not have a match in the second table.
Suppose you want to find all employees and the projects they are currently responsible for. You want to see those employees that are not currently in charge of a project as well. The following query will return a list of all employees whose names are greater than 'S', along with their assigned project numbers.
SELECT EMPNO, LASTNAME, PROJNO
FROM CORPDATA.EMPLOYEE LEFT OUTER JOIN CORPDATA.PROJECT
ON EMPNO = RESPEMP
WHERE LASTNAME > 'S'
The result of this query contains some employees that do not have a project number. They are listed in the query, but have the null value returned for their project number.
EMPNO | LASTNAME | PROJNO |
---|---|---|
000020 | THOMPSON | PL2100 |
000060 | STERN | MA2110 |
000100 | SPENSER | OP2010 |
000170 | YOSHIMURA | - |
000180 | SCOUTTEN | - |
000190 | WALKER | - |
000250 | SMITH | AD3112 |
000280 | SCHNEIDER | - |
000300 | SMITH | - |
000310 | SETRIGHT | - |
200170 | YAMAMOTO | - |
200280 | SCHWARTZ | - |
200310 | SPRINGER | - |
200330 | WONG | - |
Note: Using the RRN scalar function to return the relative
record number for a column in the table on the right in a left outer
join or exception join will return a value of 0 for the unmatched
rows.