SQL - Outer Join

> (Data|State) Management and Processing > (Data Type|Data Structure) > (Relation|Table) - Tabular data > Structured Query Language (SQL)

1 - About

An outer join extends the result of a equi join. An outer join returns all rows that satisfy the join condition and also returns some or all of those rows from one table for which no rows from the other satisfy the join condition.

  • Include tuples with no matches in the output
  • Use NULL values for missing attributes
Advertising

3 - Outer Join are not evil

Outer Join are not evil. When used appropriately, they are no faster or slower than a inner join (or regular).

SELECT t.*, t2.c1, t3.c3
  FROM t, t2, t3
 WHERE t.key1 = t2.key1 (+)
   AND t.key2 = t3.key2 (+)

Is the same with a regular join transformation that :

SELECT t.*, (SELECT c1 FROM t2 WHERE t2.key1 = t.key1) c1,
            (SELECT c2 FROM t3 WHERE t3.key1 = t.key2) c2
  FROM t

4 - Implementation issues

Using outer joins can be very useful, but you should be aware of the following performance and implementation issues:

4.1 - Performance can be slower

More rows are returned and some databases will not use indexes when outer joins are involved, so large amounts of data could slow query performance.

4.2 - Incomplete query hierarchy path

for tables after the outer join (RDBMS dependent)

You should verify how your target RDBMS processes outer joins to avoid incomplete query paths after the original outer join.

For example, in a Microsoft Access database, all one-to-many joins following the outer join in the join path must also be defined as outer joins. If not, the original outer join will be ignored by the resulting query.

4.3 - Database limitations on the use of outer joins

Not all databases allow control over outer joins in the WHERE clause. This is necessary when using a self restricting join.

For example, a self restricting join 'TYPE_CODE=10', could return all rows where TYPE=10 or Type is NULL, as TYPE=10 will never be true when the type code is NULL, whereas NULL values are generated by the outer join.

Advertising