Hony Moon

Section 2.3 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';

Note the following about 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.

                                        page: 1 from 1