Coding ON Clauses for Outer Joins - Teradata Database

SQL Data Manipulation Language

Product
Teradata Database
Release Number
15.10
Language
English (United States)
Last Update
2018-10-06
dita:id
B035-1146
lifecycle
previous
Product Category
Teradata® Database

Different join conditions in a FROM clause can yield very different results.

For example, suppose you wish to find out, using the three tables courses, offerings, and enrollment, whether the current course offerings satisfy the employee requests for courses, including the following information in the query.

  • Courses offered
  • Course locations
  • Courses requested but not offered
  • Employees who requested courses
  • In the SELECT statement that follows, the offerings table is full outer joined to the enrollment table according to the first ON condition (offerings.course_no = enrollment.course_no).

         SELECT courses.course_no, offerings.course_no, offerings.location,
                enrollment.course_no, enrollment.emp_no
         FROM offerings
         FULL OUTER JOIN enrollment ON offerings.course_no =
                         enrollment.course_no 
         RIGHT OUTER JOIN courses ON courses.course_no =
                          offerings.course_no;

    The result of this intermediate join is shown in the following table:

     

    Note that BTEQ reports represent nulls with the QUESTION MARK character.

    This intermediate derived table is next right outer joined to the courses table, according to the second join condition (courses.course_no = offerings.course_no).

    The final results appear in the following table:

     

    If the same SELECT statement is written with a different second join condition, as illustrated in the current example, then the offerings table is full outer joined to the enrollment table, according to the first JOIN condition (offerings.course_no = enrollment.course_no).

         SELECT courses.course_no, offerings.course_no,
                enrollment.course_no, enrollment.emp_no
         FROM offerings 
         FULL OUTER JOIN enrollment ON offerings.course_no =
                          enrollment.course_no 
         RIGHT OUTER JOIN courses ON courses.course_no =
                          enrollment.course_no;

    The result of this intermediate join is shown in the following table:

     

    Note that BTEQ reports represent nulls with the QUESTION MARK character.

    This intermediate derived table is next right outer joined to the courses table, according to the second join condition (courses.course_no = enrollment.course_no).

    The final results appear in the following table: