fhg
Volume 7, Number 14 -- April 11, 2007

Missing In Action: The Full Outer Join

Published: 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


Sponsored By
PRODATA COMPUTER SERVICES

DBU - Burning Love!

DBU, the "original" database utility, inspires burning love!
With over 20,000 installs of DBU in 31 countries,
it is the leading database utility.

Being without DBU has been compared to
being naked in a crowd...you don't know
what to do or which way to go!

Enter the DBU Challenge for
your chance to win $1,000!

ProData Computer Services
800.228.6318
www.DoDBU.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

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

 

The Four Hundred
Hello, New York? Buy IBM

Security Still an Issue in 2007 for System i5 Shops

A Trained IT Staff Is A Happy and Competitive One

As I See It: The Legacy

The Linux Beacon
AMD Pushes Opteron Clocks to 3 GHz, Will Miss Q1 Revenue Targets

Xandros Server 2 To Get Integrated Virtualization and Messaging

X4 Chipset from IBM Tuned for Tigerton Quad Core Xeon MPs

The X Factor: Virtualization Belongs in the System, Not in the Software

Four Hundred Stuff
Aldon Tackles Parallel Development Problems with LMi 7.5

Ricoh in Deal for AFP/IPDS Emulation

S4i Web Interfaces to Document Management Offering

CA Tweaks Job Schedulers, Positions Them as Workload Automation

Big Iron
IBM Replies To Platform: No More Compatibles

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
April 7, 2007: Volume 9, Number 14

March 31, 2007: Volume 9, Number 13

March 24, 2007: Volume 9, Number 12

March 17, 2007: Volume 9, Number 11

March 10, 2007: Volume 9, Number 10

March 3, 2007: Volume 9, Number 9

February 24, 2007: Volume 9, Number 8

The Windows Observer
Microsoft Loosens the Licensing Screws for Vista Virtualization

Microsoft Patches Animated Cursor Flaw in Windows

XenSource Extends and Improves Windows Support with 3.2 Release

Intel Shows Off Future Penryn and Nehalem Chip Designs

The Unix Guardian
Sun Boosts Performance of UltraSparc-IV+ Chips

Intel Shows Off Future Penryn and Nehalem Chip Designs

IBM Offers Rebates on System p5 and ISV Software Bundles

The X Factor: Virtualization Belongs in the System, Not in the Software

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

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

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
iSeries career training ideas

SQLERRD(3) not getting populated

Command line question

Immediate need for a experienced CGIDEV2 programmer

Data Structure Array (SORTING)





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement