JOIN

<< Click to Display Table of Contents >>

Navigation:  Apollo SQL > DDL & DML Statements >

JOIN

Using JOIN in the FROM clause is supported using the following Syntax:

... table1 [INNER] JOIN table2 ON (table1.common_field=table2.common_field)

[[INNER] JOIN table3 ON (table2.common_field=table3.common_field) ] ...

 

... table1 LEFT [OUTER] JOIN table2 ON (table1.common_field=table2.common_field)

[LEFT [OUTER] JOIN table3 ON (table2.common_field=table3.common_field) ] ...

RIGHT [OUTER] JOIN and FULL [OUTER] JOIN are not yet supported.

Good database design suggests that each table lists data only about a single entity, and detailed information can be obtained in a relational database, by using additional tables, and by using a join. Look at these example tables:

AntiqueOwners

OwnerID OwnerLastName OwnerFirstName

------- ------------- -------------

01 Jones Bill

02 Smith Bob

15 Lawson Patricia

21 Akins Jane

50 Fowler Sam

 

Orders

OwnerID ItemDesired

------- -----------

02 Table

02 Desk

21 Chair

15 Mirror

 

Antiques

SellerID BuyerID Item

-------- ------- -------------

01 50 Bed

02 15 Table

15 02 Chair

21 50 Mirror

50 01 Desk

01 21 Cabinet

02 21 Coffee Table

15 50 Chair

01 15 Jewelry Box

02 21 Pottery

21 02 Bookcase

50 01 Plant Stand

Keys

A primary key is a column or set of columns that uniquely identifies the rest of the data in any given row. For example, in the AntiqueOwners table, the OwnerID column uniquely identifies that row. This means two things: no two rows can have the same OwnerID, and, even if two owners have the same first and last names, the OwnerID column ensures that the two owners will not be confused with each other, because the unique OwnerID column will be used throughout the database to track the owners, rather than the names.

A foreign key is a column in a table where that column is a primary key of another table, which means that any data in a foreign key column must have corresponding data in the other table where that column is the primary key. In DBMS-speak, this correspondence is known as referential integrity. For example, in the Antiques table, both the BuyerID and SellerID are foreign keys to the primary key (OwnerID) of the AntiqueOwners table. For instance, one has to be an Antique Owner before one can buy or sell any items. In both tables, the ID rows are used to identify the owners or buyers and sellers, and that the OwnerID is the primary key of the AntiqueOwners table. In other words, all of this "ID" data is used to refer to the owners, buyers, or sellers of antiques, themselves, without having to use the actual names.

Performing a Join

The purpose of these keys is so that data can be related across tables, without having to repeat data in every table--this is the power of relational databases. For example, you can find the names of those who bought a chair without having to list the full name of the buyer in the Antiques table...you can get the name by relating those who bought a chair with the names in the AntiqueOwners table through the use of the OwnerID, which relates the data in the two tables. To find the names of those who bought a chair, use the following query:

SELECT OWNERLASTNAME, OWNERFIRSTNAME FROM ANTIQUEOWNERS, ANTIQUES

WHERE (BUYERID = OWNERID) AND (ITEM = 'Chair');

 

In this query, notice that both tables involved in the relation are listed in the FROM clause of the statement. In the WHERE clause, first notice that the ITEM = 'Chair' part restricts the listing to those who have bought (and in this example, thereby owns) a chair. Secondly, notice how the ID columns are related from one table to the next by use of the (BUYERID = OWNERID) clause. Only where ID's match across tables and the item purchased is a chair (because of the AND), will the names from the AntiqueOwners table be listed. Because the joining condition used an equal sign, this join is called an equijoin. The result of this query is two names: Smith, Bob & Fowler, Sam.

Dot notation refers to prefixing the table names to column names, to avoid ambiguity, as such:

SELECT ANTIQUEOWNERS.OWNERLASTNAME,

ANTIQUEOWNERS.OWNERFIRSTNAME

FROM ANTIQUEOWNERS, ANTIQUES

WHERE (ANTIQUES.BUYERID = ANTIQUEOWNERS.OWNERID) AND

(ANTIQUES.ITEM = 'Chair');

As the column names are different in each table, however, this wasn't necessary.

More Examples:

 

SELECT * FROM Customer INNER JOIN Orders ON (Customer.CustNo = Orders.CustNo)

INNER JOIN Items i ON (Orders.OrderNo = i.OrderNo);

 

SELECT * FROM Customer LEFT JOIN Orders ON (Customer.CustNo = Orders.CustNo)

LEFT OUTER JOIN Items i ON (Orders.OrderNo = i.OrderNo);

 

/* This syntax is also supported: */  

SELECT * FROM Table1 t1 INNER JOIN Table2 t2 ON

(t1.CommonField1 = t2.CommonField1) AND

(t1.CommonField2 = t2.CommonField2) INNER JOIN...