The result set of an inner join does not contain the rows of any of the involved tables for which no corresponding row can be found in the other tables involved according to the join condition. Sometimes you may want to include such rows in the result set, to get the full set of rows of one table and augment the data with data from other tables as far as available. In addition to the inner join, three types of Outer Joins exist for this purpose:
Left outer join: includes all entries in the first (left) table, even if no corresponding entries are found in the second (right) table.
Right outer join: includes all entries in the second table even if no corresponding entries are found in the first table.
Full outer join: is the union of the left and right outer join.
In all three cases, the following factors apply:
LEFT OUTER JOIN: Explanation
Left Outer Joins:
- One row of a table and one row of another table are always connected to a common result row - provided the JOIN condition is fulfilled.
- In addition, rows of the left table without matching row in the right table are copied to the query result. The missing values (from the right table) are filled with NULL values.
LEFT OUTER JOIN: Example
Which car is registered to which individual or company? Individuals and companies that currently do not have any cars registered should also be included. Cars without a registration should not be included in the result.
12
SELECT Name, CarID, Brand
FROM Owner LEFT OUTER JOIN Car ON OwnerID = Owner;
| NAME | CARID | BRAND |
|---|
| Ms T | F19 | VW |
| Ms U | F09 | Skoda |
| Ms U | F11 | BMW |
| SAP AG | F02 | VW |
| SAP AG | F03 | BMW |
| SAP AG | F18 | Renault |
| SAP AG | F05 | Mercedes |
| SAP AG | F15 | Skoda |
| SAP AG | F07 | Audi |
| SAP AG | F14 | Mercedes |
| HDM AG | F12 | Skoda |
| HDM AG | F10 | BMW |
| Mr V | F06 | Audi |
| Ms W | F01 | Fiat |
| IKEA | F13 | Renault |
| IKEA | F08 | VW |
| IKEA | F04 | Mercedes |
| Mr X | ? | ? |
| Ms Y | F17 | Audi |
| Mr Z | ? | ? |
RIGHT OUTER JOIN: Explanation
Right Outer Joins:
- One row of a table and one row of another table are always connected to a common result row, provided that the JOIN condition is fulfilled.
- In addition, rows of the right table without matching row in the left table are copied to the query result. The missing values (from the left table) are filled with NULL values.
- Note that reversing the order of tables in the join condition AND switching the direction of the join ( LEFT ↔ RIGHT) produces the same result set.
RIGHT OUTER JOIN: Example
Which car is registered to which individual or company? Cars without a current registration should also be included. Individuals and companies that currently do not have a car registered should not be included in the result.
12
SELECT Name, CarID, Brand
FROM Owner RIGHT OUTER JOIN Car ON OwnerID = Owner;
| NAME | CARID | BRAND |
|---|
| Ms W | F01 | Fiat |
| SAP AG | F02 | VW |
| SAP AG | F03 | BMW |
| IKEA | F04 | Mercedes |
| SAP AG | F05 | Mercedes |
| Mr V | F06 | Audi |
| SAP AG | F07 | Audi |
| IKEA | F08 | VW |
| Ms U | F09 | Skoda |
| HDM AG | F10 | BMW |
| Ms U | F11 | BMW |
| HDM AG | F12 | Skoda |
| IKEA | F13 | Renault |
| SAP AG | F14 | Mercedes |
| SAP AG | F15 | Skoda |
| ? | F16 | Opel |
| Ms Y | F17 | Audi |
| SAP AG | F18 | Renault |
| Ms T | F19 | VW |
| ? | F20 | Audi |