Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - SQL Usage » Joins: Retrieving Data from Several Tables » Key joins » Key joins of table expressions

Key joins of table expressions Next Page

Key joins of table expressions that do not contain commas


When both of the two table expressions being joined do not contain commas, SQL Anywhere examines the foreign key relationships in the pairs of tables in the statement, and generates a single join condition.

For example, the following join has two table-pairs, A-C and B-C.

(A NATURAL JOIN B) KEY JOIN C

SQL Anywhere generates a single join condition for joining C with (A NATURAL JOIN B) by looking at the foreign key relationships within the table-pairs A-C and B-C. It generates one join condition for the two pairs according to the rules for determining key joins when there are multiple foreign key relationships:

For more information, see Key joins when there are multiple foreign key relationships.

Example

The following query finds all the employees who are sales representatives, and their departments.

SELECT Employees.Surname, 
        FK_DepartmentID_DepartmentID.DepartmentName
FROM (Employees KEY JOIN Departments 
        AS FK_DepartmentID_DepartmentID)
    KEY JOIN SalesOrders;

You can interpret this query as follows.

The following query is therefore equivalent to the previous query:

SELECT Employees.Surname, Departments.DepartmentName
FROM (Employees JOIN Departments
    ON ( Employees.DepartmentID = Departments.DepartmentID ) )
JOIN SalesOrders
    ON (Employees.EmployeeID = SalesOrders.SalesRepresentative);