# Outer Joins – Best Practice Tip

2/13/2008 Purpose This document will illustrate how outer joins work and discusses how to ensure filters work as intended with outer joins. Here are the two example tables used throughout this document: V_AC_APPL APPL_KEY EDW_PERS_ID TERM_CD APPL_NBR 101 12 420048 1 301 13 420058 1 401 13 420068 2 501 14 420078 1 601 15 420088 1 V_AC_APPL_ATTR APPL_KEY ATTR_CD 101 A 301 B Examples

AC_DT 9/12/2005 9/12/2007

INNER JOIN
Problem: Find applications with student attributes Solution: Select APPL.APPL_KEY,APPL.EDW_PERS_ID,APPL.TERM_CD, APPL.APPL_NBR, ATTR.ATTR_CD,ATTR.AC_DT From V_AC_APPL APPL, V_AC_APPL_ATTR ATTR Where APPL.APPL_KEY=ATTR.APPL_KEY Result: ROW APPL_KEY EDW_PERS_ID TERM_CD APPL_NBR ATTR_CD AC_DT 1 101 12 420048 1 A 9/12/2005 2 301 13 420058 1 B 9/12/2007 NOTE: The result set contains the selected columns from those rows on both tables whose keys matched

INNER JOIN with Filter
Problem: Find applications with student attributes and whose Application Census Date is less than 9/12/2007. Solution: Select APPL.APPL_KEY,APPL.EDW_PERS_ID,APPL.TERM_CD, APPL.APPL_NBR, ATTR.ATTR_CD,ATTR.AC_DT From V_AC_APPL APPL, V_AC_APPL_ATTR ATTR Where APPL.APPL_KEY=ATTR.APPL_KEY And APPL.AC_DT < to_date(’9/12/2007’, ‘MM/DD/YYYY’) Result: ROW APPL_KEY EDW_PERS_ID TERM_CD APPL_NBR ATTR_CD AC_DT 1 101 12 420048 1 A 9/12/2005 NOTE: The result set contains the selected columns from those rows on both tables whose keys matched and whose application census date value meets the filter condition.

1 of 5

ATTR_CD. the table which will contribute rows to the result set when a match to the dominant table exists. • V_AC_APPL_ATTR is the subordinate table.ATTR.APPL.ATTR.e. The RIGHT OUTER JOIN does the reverse of this.AC_DT From V_AC_APPL APPL LEFT OUTER JOIN V_AC_APPL_ATTR ATTR ON (APPL.APPL.AC_DT From V_AC_APPL APPL.EDW_PERS_ID.APPL_KEY(+) NOTE: • V_AC_APPL is the dominant table.APPL. ATTR. APPL.APPL_KEY=ATTR. i. In the illustration below V_AC_APPL would be the table referred to as “1” and V_AC_APPL_ATTR is the “0-toMany” rows table. Finally. the FULL OUTER JOIN returns all rows from both tables.e.APPL_NBR.TERM_CD. Outer Join Syntax as Used in Business Objects: Problem: Find all applications and list the students attributes of the applications that have them Solution: Select APPL.APPL_KEY) 2 of 5 . ATTR. along with the values from the right hand side..APPL.APPL_KEY.APPL_KEY=ATTR.APPL_KEY. APPL. There are three variations on the outer join.TERM_CD. the table for which all its rows (which satisfy any additional filters) will be in the result set. filling in any blanks with NULLs: Here’s the solution immediately above rewritten as a Left Outer Join: Solution: Select Select APPL. you will need to use an outer join if you need to pull all rows from the dominant table. i. V_AC_APPL_ATTR ATTR Where APPL. The LEFT OUTER JOIN returns all the rows from the table on the left side of the join.APPL_NBR.EDW_PERS_ID. or NULLs if a matching row doesn't exist. Alternate Syntax: ANSI Standard Out Join Oracle9i supports the ANSI/ISO SQL: 1999 standards. Technically the relationship between the two table would be called “1 to 0-to-Many” rows.OUTER JOINS When to Use an Outer Join When you need join two tables that do not always have the matching keys.ATTR_CD.

This allow users to utilize 2 keys when joining the application or person views to the fact table. Solution: Select APPL.AC_DT From V_AC_APPL APPL.APPL_KEY(+) And NVL(ATTR.CENSUS_DT_KEY = V_AC_APPL_CENSUS.APPL_KEY. ‘MM/DD/YYYY’) Problem: AC_DT exists only on the view for V_AC_APPL_PERS and V_AC_APPL in the Application Census Data Mart. Using the NULL value function appropriately will prevent the undesirable results that may arise when NULL values are possible.AC_DT(+). not on the corresponding tables. Since the table V_AC_APPL_ATTR did not have any rows whose appl_key values were either 501 or 601. Solution: Add an additional key "CENSUS_DT_KEY" to 2 views. the results for the columns from V_AC_APPL_ATTR will be NULL. Outer Join With a Filter on AC_DT Outer Join Syntax as Used in Business Objects: Problem: Find all applications and list the student attributes for the applications which have them.ATTR. this tip would refer to the possible NULL values that columns ATTR_CD or AC_DT in the result set if the query also included filters on either or both columns. rather than 1 key plus a specific application date.to_date(’9/12/2005’. the census_date for the attributes must be less than or equal to 9/12/2005. it will also be necessary to add the following joins: V_AC_APPL_PERS. ATTR.CENSUS_DT_KEY and V_AC_APPL. V_AC_APPL_ATTR ATTR Where APPL. ‘MM/DD/YYYY’)) <=to_date(’9/12/2005’.EDW_PERS_ID.Left Outer Join Result: Row APPL_KEY EDW_PERS_ID 1 101 12 2 301 13 3 401 13 4 501 14 5 601 15 TERM_CD 420048 420058 420068 420078 420088 APPL_NBR 1 1 2 1 1 ATTR_CD A B NULL NULL NULL AC_DT 9/12/2005 9/12/2007 NULL NULL NULL NOTE: Observe last two columns contain NULLs. This causes a cartesion product when there is more than one census load in a term.APPL. In the DMAppCns universe. Here’s when you’ll need the NULL Value function: • Your query includes one or more outer joins.CENSUS_DT_KEY 3 of 5 . BEST PRACTICE TIP If you want to further constrain on a subordinate table column. APPL. • Your query also filters on a subordinate table column that is NOT a key column used in the join itself. The more outer joins in the query.CENSUS_DT_KEY = V_AC_APPL_CENSUS.TERM_CD. In the case of the last example.APPL_KEY=ATTR.ATTR_CD.APPL_NBR. be sure your SQL addresses the possible NULL values in the result set for filtering subordinate table column(s).APPL. higher the risk of undesirable results. If attributes exist.

AC_DT. ‘MM/DD/YYYY’) How The Solutions Work NVL(x. AC_DT.ATTR_CD. V_AC_APPL_ATTR ATTR Where APPL.ATTR_CD. The examples use the to_date function in order to compare an explicit character string to the date column.y) is the function that converts the character string in x to a datetime value in the date format y.True ATTR_CD is NULL.True ATTR_CD is NULL. This function checks to see if x is NULL. AC_DT From V_AC_APPL APPL.True ATTR_CD = ‘A’ -. but set to A which is = ‘A’ -. Results for the Outer Join with a Filter Row 1 3 4 5 APPL_KEY 101 401 501 601 EDW_PERS_ID 12 13 14 15 TERM_CD 420048 420068 420078 420088 APPL_NBR 1 2 1 1 ATTR_CD A NULL NULL NULL AC_DT 9/12/2005 NULL NULL NULL How the Filter evaluated NVL(ATTR. To_date(x. MM/DD/YYYY’) Row 1 9/12/2005 <= 9/12/2005 -.APPL_KEY=ATTR.APPL_NBR.APPL_NBR. Select APPL_KEY. then the value of y is substituted. AC_DT From V_AC_APPL APPL LEFT OUTER JOIN V_AC_APPL_ATTR ATTR ON (APPL. to_date(’9/12/2007’.TERM_CD.ANSI Standard Left Outer Join Problem: (Same as immediately above. ‘MM/DD/YYYY’))<=to_date(’9/12/2007’.APPL_KEY) Where NVL(ATTR.True (include in results) 2 9/12/2007 <=9/12/2005 – FALSE (omit from results) 3 (NULL replaced) 9/12/2005 <=9/12/2005 – True (include in results) 4 (NULL replaced) 9/12/2005 <=9/12/2005 – True (include in results) 5 (NULL replaced) 9/12/2005 <=9/12/2005 – True (include in results) Using Current Syntax constraining on ATTR_CD: Problem: Find all applications and list the student attributes if the Attribute Code = ‘A’.to_date(’9/12/2005’. ‘A’) =’A’ ATTR_CD =’A’ -.) Solution: Select APPL_KEY.APPL_KEY(+) And NVL(ATTR_CD.True 4 of 5 .False ATTR_CD is NULL.y) is the NULL value function. If x is NULL.EDW_PERS_ID.EDW_PERS_ID. but set to A which is = ‘A’ -.AC_DT. but set to A which is = ‘A’ -. ‘MM/DD/YYYY’)) <=to_date(’9/12/2005’.TERM_CD. ‘A’) =’A’ How Filter evaluated Row ATTR_CD 1 A 2 B 3 NULL 4 NULL 5 NULL NVL(ATTR_CD.APPL_KEY=ATTR.

Result data set (Row 2 is filtered out which is correct) Row 1 3 4 5 APPL_KEY 101 401 501 601 EDW_PERS_ID 12 13 14 15 TERM_CD 420048 420068 420078 420088 APPL_NBR 1 2 1 1 ATTR_CD A NULL NULL NULL AC_DT 9/12/2005 NULL NULL NULL 5 of 5 .