• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Missing In Action: The Full Outer Join

    April 11, 2007 Hey, Ted

    As far as I know, you haven’t covered my situation in Four Hundred Guru. I need to join two physical files using SQL. It’s possible that some records in the first file won’t have matches in the second file. It’s also possible that some records in the second file won’t have matches in the first file. What type of join do I use?

    –Chris

    You need a full outer join, Chris, and DB2/400 (or whatever they call it these days) won’t handle it–yet. I expect IBM to add the full outer join any release now.

    To simulate the full outer join, I use the following formula: Left outer join + right exception join = full outer join.

    Suppose you work for an institution of higher learning of some sort, and need a list of the professors and the classes each one has been assigned to teach. Some professors have not yet been assigned to teach any classes. Some classes have not yet been assigned to a professor. How would you get a full schedule?

    First, here is the FACULTY table:

    Professor ID

    Name

    P01

    Cake, Patty

    P02

    Dover, Ben

    P03

    Flett, Pam

    And here is the SCHEDULE table, which lists all the classes.

    Class ID

    Period

    Building

    Room

    Instructor

    101

    A

    41

    320

    P02

    102

    A

    41

    218

    P03

    103

    B

    41

    212

    P02

    104

    B

    42

    302

    NULL

    105

    C

    41

    165

    P04

    Notice a few things:

    • No classes have been assigned to instructor P01.
    • Class 104 has not been assigned to an instructor.
    • Class 105 has been assigned to non-existent instructor P04.

    Here’s the join:

    SELECT f.FacID, f.Name, s.classID, s.period, s.Building, s.Room
      FROM Faculty AS f
      LEFT JOIN Schedule AS s
        ON f.FacID = s.Instructor
    UNION
    SELECT s.Instructor, f.Name, s.classID, s.period, s.Building, s.Room
      FROM Faculty AS f
     RIGHT EXCEPTION JOIN Schedule AS s
        ON f.FacID = s.Instructor
    

    Notice that the two SELECT expressions are almost identical. They differ in the type of join, of course. In the second SELECT, I placed the instructor ID from the secondary table, rather than from the primary table, because any values from the primary table will always be null in a right exception join.

    Here is the result set.

    Instructor ID

    Instructor

    Class

    Period

    Building

    Room

    P01

    Cake, Patty

    NULL

    NULL

    NULL

    NULL

    P02

    Dover, Ben

    101

    A

    41

    320

    P02

    Dover, Ben

    103

    B

    41

    212

    P03

    Flett, Pam

    102

    A

    41

    218

    NULL

    NULL

    104

    B

    42

    302

    P04

    NULL

    105

    C

    41

    165

    That technique will have to do us until the database team in Rochester adds a full outer join to the SQL syntax. I am surprised that I have not covered this technique in the newsletter already. Nobody ever asked.

    –Ted



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Midrange Dynamics North America

    Want to deliver DevOps on IBM i?

    DevOps enables your IBM i development teams to shorten the software development lifecycle while delivering features, fixes, and frequent updates that are closely aligned with business objectives. Flexible configuration options within MDChange make it easy to adapt to new workflow strategies and policies as you adopt DevOps practices across your organization.

    Learn More.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    COMMON:  Join us at the 2007 conference, April 29 – May 3, in Anaheim, California
    Vision Solutions:  The first new HA release from the newly merged Vision and iTera companies
    LASERTEC USA:  Fully integrate MICR check printing with your existing application

    IT Jungle Store Top Book Picks

    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket Developers' Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    iSeries Express Web Implementer's Guide: List Price, $59.00
    Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    WebFacing Application Design and Development Guide: List Price, $55.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    The All-Everything Machine: List Price, $29.95
    Chip Wars: List Price, $29.95

    Computer Makers Tout Ways to Reduce Carbon Dioxide Emissions, Save Money IBM Goes After Windows with User-Priced System i Servers

    Leave a Reply Cancel reply

Volume 7, Number 14 -- April 11, 2007
THIS ISSUE SPONSORED BY:

ProData Computer Services
WorksRight Software
COMMON

Table of Contents

  • Missing In Action: The Full Outer Join
  • Reader Feedback on One-Man System i Shops
  • Admin Alert: The Process and Pitfalls of Duplicating Libraries

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • You Can Now Get IBM Tech Support For VS Code For i
  • Price Cut On Power S1012 Mini Since Power S1112 Ain’t Coming Until 2026
  • IBM i: Pro and Con
  • As I See It: Disruption
  • IBM i PTF Guide, Volume 27, Number 30
  • The Turning Point For Power Systems Is Here, And Now
  • How IBM i Users Can Compete In The Digital Era With Composable Commerce
  • IBM Streamlines Data Migration With New Partition Mirror Tech
  • Profound Logic Adds MCP To IBM i AI Tool
  • IBM i PTF Guide, Volume 27, Number 29

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle