Contents Index Non-ANSI joins Generated joins and the ON phrase

ASA SQL User's Guide
  Joins: Retrieving Data from Several Tables

Explicit join conditions (the ON phrase)


Instead of, or along with, a key or natural join, you can specify a join using an explicit join condition. You specify a join condition by inserting an ON phrase immediately after the join. The join condition always refers to the join immediately preceding it.

Example 

In the following query, the first ON phrase is used to join sales_order to customer. The second ON phrase is used to join the table expression (sales_order JOIN customer) to the base table sales_order_item.

SELECT *
FROM sales_order JOIN customer
    ON sales_order.cust_id = customer.id
  JOIN sales_order_items
    ON sales_order_items.id = sales_order.id
Tables that can be referenced 

The tables that are referenced in an ON phrase must be part of the join that the ON phrase modifies. For example, the following is invalid:

FROM (A KEY JOIN B) JOIN (C JOIN D ON A.x = C.x)

The problem is that the join condition A.x = C.x references table A, which is not part of the join it modifies (in this case, C JOIN D).

However, as of the ANSI/ISO standard SQL99 and Adaptive Server Anywhere 7.0, there is an exception to this rule: if you use commas between table expressions, an ON condition of a join can reference a table that precedes it syntactically in the FROM clause. Therefore, the following is valid:

FROM (A KEY JOIN B) , (C JOIN D ON A.x = C.x)

For more information about commas, see Commas.


Generated joins and the ON phrase
Types of explicit join conditions
Using the WHERE clause for join conditions

Contents Index Non-ANSI joins Generated joins and the ON phrase