MainframeSupports
tip week 20/2001:

Data inconsistency is a common problem for almost all installations. We have to deal with it every day. To find the inconsistencies is a fairly easy task when data is stored in a relational database like DB2. A common SQL call used in this context is:

select *
from table1 t1
where not exists
( select *
  from table2 t2
  where t1.col1 = t2.col1
)

If we assume that t2.col1 and t1.col1 forms the relationship between the two tables, this SQL call finds rows in table1 which doesn't relate to any rows in table2. Normally this will indicate that col1 is a primary key (or a part of it) in one of the tables and a foreign key (or a part of it) in the other table.

The tip is that you can use an outer join to perform the same task as the SQL call above performs. Outer joins has been available in DB2 since version 4.1, but they are not used a lot. With an outer join a SQL call producing the same result looks like this:

select *
from table1 t1
left outer join table2 t2
on t1.col1 = t2.col1
where t2.col1 is null

Whether you write your SQL call with exists or with outer join is a matter of taste, but in most circumstances the outer join will perform much faster. In version 4.1 of DB2 it was possible to spell outer like outter. I haven't investigated version 5 or 6 for this obvious bug, but my guess is that it is still there. You can read a lot more about outer joins in the SQL reference book for DB2 that you prefer.

Previous tip in english        Sidste danske tip        Tip list