Professional Documents
Culture Documents
A join works through the use of keys. Continuing our example, our supplier table contains a column designated as
thesupplier_id. This column is configured as the primary key (for details on primary keys see Database Basics).
The product table contains all of the products sold by our company, including product id, product description and
product name. In addition it also contains the supplier id of the supplier from which we buy the individual products.
Because this is a key from a different table (the suppliers table) it is referred to as a foreign key. When using
a SELECT statement to retrieve data from the product table we can use this foreign key to extract the relevant
supplier information from the supplier table for each product.
Let's begin by looking at our two tables, the supplier table and the product. First, the supplier table contains the
following rows:
| supplier_contact |
+-------------+---------------+-------------------+------------------+
|
1 | Microsoft
| 1 Microsoft Way
| Bill Gates
2 | Apple, Inc.
| 1 Infinite Loop
| Steve Jobs
3 | EasyTech
4 | WildTech
| Alan Wilkes
+-------------+---------------+-------------------+------------------+
4 rows in set (0.00 sec)
| prod_desc
| supplier_id |
+-----------+----------------------------+-----------------------------+-------------+
|
| CD Writer
3 |
| Cordless Mouse
3 |
4 |
| Ergonomic Keyboard
1 |
| Smart Phone
2 |
+-----------+----------------------------+-----------------------------+-------------+
As you can see from the above output, the product rows contain a column which holds the supplier_id of the supplier
from which the product is obtained. Now that we have the tables created, we can begin to perform some joins.
Performing a Cross-Join
Joining tables involves combining rows from two tables. The most basic of join types is the cross-join. The cross-join
simply assigns a row from one table to every row of the second table. This is of little or no use in real terms, but for
the purposes of completeness, the syntax for a cross-join is as follows:
SELECT column_names FROM table1, table2;
For example, if we were to perform the following command on our sample table we would get the following output:
+----------------------------+---------------+
| prod_name
| supplier_name |
+----------------------------+---------------+
| CD-RW Model 4543
| Microsoft
| Apple, Inc.
| EasyTech
| WildTech
| Microsoft
| Apple, Inc.
| EasyTech
| WildTech
| Microsoft
| Apple, Inc.
| EasyTech
| WildTech
| Microsoft
| Apple, Inc.
| EasyTech
| WildTech
| Microsoft
| Apple, Inc.
| EasyTech
| WildTech
+----------------------------+---------------+
As you can see, it is hard to imagine how this could of use in many situations. A much more useful type of join is
the Equi-Join or Inner Join.
The Equi-Join joins rows from two or more tables based on comparisons between a specific column in each table.
The syntax for this approach is as follows:
SELECT column_names FROM table1, table2 WHERE (table1.column = table2.column);
For example, to extract the product name and supplier name for each row in our product table we would use the
following command:
Note that we have to use what is known as the fully qualified name for the supplier_id column in each table since both
tables contain a supplier_id. A fully qualified column name is defined by specifying the table name followed by a dot
(.) and then the column name.
The result of the above command is to produces a list of products and the name and address of the supplier for each
product:
+--------------------------+---------------+-------------------+
| prod_name
| supplier_name | supplier_address
+--------------------------+---------------+-------------------+
| Microsoft 10-20 Keyboard | Microsoft
| 1 Microsoft Way
| Apple, Inc.
| 1 Infinite Loop
| EasyTech
| EasyTech
| WildTech
+--------------------------+---------------+-------------------+
5 rows in set (0.00 sec)
| supplier_name | supplier_address
+----------------------------+---------------+-------------------+
| CD-RW Model 4543
| EasyTech
| EasyTech
| WildTech
| Microsoft
| 1 Microsoft Way
| Apple, Inc.
| 1 Infinite Loop
+----------------------------+---------------+-------------------+
One key different with the LEFT JOIN is that it will also list rows from the first table for which there is no match in the
second table. For example, suppose we have product in ourproduct table for which there is no matching supplier in
the supplier table. When we run our SELECT statement the row will still be displayed, but with NULL values for the
supplier columns since no such supplier exists:
+----------------------------+---------------+-------------------+
| prod_name
| supplier_name | supplier_address
+----------------------------+---------------+-------------------+
| CD-RW Model 4543
| EasyTech
| EasyTech
| WildTech
| Microsoft
| 1 Microsoft Way
| Apple, Inc.
| 1 Infinite Loop
| Moto Razr
| NULL
| NULL
+----------------------------+---------------+-------------------+
The opposite effect can be achieved using a RIGHT JOIN, whereby all the rows in a the second table (i.e.
our supplier table) will be displayed regardless of whether that supplier has any products in our product table:
| supplier_name
| supplier_address
+--------------------------+-----------------+------------------------+
| 1 Microsoft Way
| Apple, Inc.
| 1 Infinite Loop
| EasyTech
| EasyTech
| WildTech
| NULL
+--------------------------+-----------------+------------------------+
| supplier_name | supplier_address |
+--------------------------+---------------+------------------+
| Microsoft 10-20 Keyboard | Microsoft
| 1 Microsoft Way
+--------------------------+---------------+------------------+
1 row in set (0.00 sec)
The USING clause further simplifies the tasks of creating joins. The purpose of USING is to avoid the use of fully
qualified names (such as product.supplier_id and supplier.supplier_id) when reference columns that reside in different
tables but have the names. For example, to perform the same join above based on the values of product.supplier_id
and supplier.supplier_id we can simply use the following syntax:
+--------------------------+---------------+------------------+
| prod_name
| supplier_name | supplier_address |
+--------------------------+---------------+------------------+
| Microsoft 10-20 Keyboard | Microsoft
| 1 Microsoft Way
+--------------------------+---------------+------------------+
1 row in set (0.00 sec)