# 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

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

the census_date for the attributes must be less than or equal to 9/12/2005. the results for the columns from V_AC_APPL_ATTR will be NULL.AC_DT From V_AC_APPL APPL.CENSUS_DT_KEY and V_AC_APPL. Since the table V_AC_APPL_ATTR did not have any rows whose appl_key values were either 501 or 601. Here’s when you’ll need the NULL Value function: • Your query includes one or more outer joins. This causes a cartesion product when there is more than one census load in a term. higher the risk of undesirable results.CENSUS_DT_KEY = V_AC_APPL_CENSUS. be sure your SQL addresses the possible NULL values in the result set for filtering subordinate table column(s). rather than 1 key plus a specific application date.APPL_KEY. ‘MM/DD/YYYY’)) <=to_date(’9/12/2005’. Solution: Add an additional key "CENSUS_DT_KEY" to 2 views.AC_DT(+). If attributes exist.CENSUS_DT_KEY 3 of 5 .TERM_CD. The more outer joins in the query. This allow users to utilize 2 keys when joining the application or person views to the fact table. 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.ATTR. In the case of the last example. ‘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.to_date(’9/12/2005’. Solution: Select APPL.ATTR_CD. APPL. it will also be necessary to add the following joins: V_AC_APPL_PERS. 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. In the DMAppCns universe. Using the NULL value function appropriately will prevent the undesirable results that may arise when NULL values are possible.APPL. not on the corresponding tables.APPL. ATTR. • Your query also filters on a subordinate table column that is NOT a key column used in the join itself.APPL_KEY(+) And NVL(ATTR.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.CENSUS_DT_KEY = V_AC_APPL_CENSUS. BEST PRACTICE TIP If you want to further constrain on a subordinate table column.EDW_PERS_ID.APPL_KEY=ATTR.APPL_NBR. V_AC_APPL_ATTR ATTR Where APPL.

APPL_NBR. ‘MM/DD/YYYY’)) <=to_date(’9/12/2005’.ANSI Standard Left Outer Join Problem: (Same as immediately above.APPL_KEY(+) And NVL(ATTR_CD. Select APPL_KEY. ‘A’) =’A’ ATTR_CD =’A’ -. but set to A which is = ‘A’ -. ‘MM/DD/YYYY’))<=to_date(’9/12/2007’.True ATTR_CD is NULL. ‘MM/DD/YYYY’) How The Solutions Work NVL(x.AC_DT. AC_DT From V_AC_APPL APPL.y) is the function that converts the character string in x to a datetime value in the date format y. AC_DT From V_AC_APPL APPL LEFT OUTER JOIN V_AC_APPL_ATTR ATTR ON (APPL.False ATTR_CD is NULL.True ATTR_CD is NULL.) Solution: Select APPL_KEY.APPL_KEY=ATTR.EDW_PERS_ID. V_AC_APPL_ATTR ATTR Where APPL. 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(’9/12/2007’. but set to A which is = ‘A’ -.True ATTR_CD = ‘A’ -.to_date(’9/12/2005’. MM/DD/YYYY’) Row 1 9/12/2005 <= 9/12/2005 -. but set to A which is = ‘A’ -.EDW_PERS_ID.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’. AC_DT. This function checks to see if x is NULL. ‘A’) =’A’ How Filter evaluated Row ATTR_CD 1 A 2 B 3 NULL 4 NULL 5 NULL NVL(ATTR_CD.ATTR_CD.True 4 of 5 .AC_DT. To_date(x.TERM_CD. then the value of y is substituted.APPL_NBR.y) is the NULL value function.APPL_KEY=ATTR. The examples use the to_date function in order to compare an explicit character string to the date column. If x is NULL.ATTR_CD.TERM_CD.

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 .