Entering content frame

JOIN Predicate (join_predicate) 

A JOIN predicate specifies a JOIN. A JOIN predicate can be specified with or without one or with two OUTER JOIN indicators.

Syntax

<join_predicate> ::=
<expression> [<outer_join_indicator>] <comp_op> <expression> [<outer_join_indicator>]

<outer_join_indicator> ::= (+)

expression, comp_op

Explanation

Each expression must contain a column specification. A column specification must exist for the first and second expression so that both specifications refer to different table names or reference names.

Let x be the value of the first expression and y the value of the second expression. The values x and y must be comparable with one another.

The rules outlined under comparison predicate apply here.

If at least one OUTER JOIN indicator is specified in a JOIN predicate of a search condition, the corresponding table expression must be based on exactly two tables, or the following has to apply:

·        OUTER JOIN indicators are only specified for one of the tables in the FROM clause.

·        All of the JOIN predicates in this table to just one other table contain the OUTER JOIN indicator.

·        All other JOIN predicates contain no OUTER JOIN indicators.

If a JOIN requires more than two tables for the QUERY specification and if one of the rules above cannot be observed, a QUERY expression can also be used in the FROM clause.

Only those rows from the table that have a counterpart of the comparison operator in the JOIN predicate specified in the table are transferred to the result table.

The OUTER JOIN indicator must be specified on the side of the comparison operator where the other table is specified if each row in a table is to appear at least once in the result table.
If it is not possible to find at least one counterpart for a table row in the other table, this row is used to build a row for the result table. The NULL value is then used for the output columns which are formed from the columns in the other table.
Since the OUTER JOIN indicator can be specified on both sides of the comparison operator if the table expression is based on just two tables, it can be ensured that each line in both tables appears at least once in the result table.

The JOIN predicate is a special case of the comparison predicate. The number of JOIN predicates in a search condition is limited to 128.

JOIN predicate

Example tables: customer, reservation

Is there a reservation for the customer 'Porter'? If so, for what date?

SELECT reservation.rno, customer.name,customer.firstname, reservation.arrival, departure

FROM customer, reservation

WHERE customer.name = 'Porter' AND customer.cno = reservation.cno

RNO

NAME

FIRSTNAME

ARRIVAL

DEPARTURE

100

Porter

Jenny

13/11/2001

15/11/2001

110

Porter

Jenny

24/12/2001

06/01/2002

120

Porter

Martin

14/11/2001

18/11/2002

Specifying an OUTER JOIN indicator

Example tables: hotel, reservation

List all the hotels in Los Angeles for which a reservation exists and those for which a reservation does not exist. Missing reservation numbers are assigned a NULL value.

SELECT hotel.hno, hotel.name, reservation.rno

FROM hotel, reservation

WHERE hotel.city = 'Los Angeles' AND hotel.hno = reservation.hno (+)

HNO

NAME

RNO

80

Midtown

100

50

Lake Michigan

120

80

Midtown

140

120

Sunshine

180

40

Eight Avenue

?

 

 

Leaving content frame