RelationalDBDesign RelationalDBDesign



Database Analysis   «Prev  Next»
Lesson 3 Types of joins: 1)inner join
Objective Define an inner join.

SQL Inner Joins

You should consider creating a join between tables whenever the records in the tables will be combined frequently to produce meaningful information. In the previous lesson you saw the result of a join between two tables (CD and Distributor) with records having the same value in a common field. That type of join is referred to as an equi-join, because the common field contains equivalent values in both records. Equi-joins also are referred to as natural joins.
In addition to the equi-join, there are several other types of joins you can perform, including:
  1. Inner join: A link between tables in a database that displays only the rows with a match (true value of the join condition) for both join tables. An inner join does not guarantee the return of every row of data that you expect will be returned.
  2. same-table join: A type of inner join, so named because it creates two copies of a table and then joins records from the tables where the tables have equivalent values in designated fields.
  3. outer join: A link between tables in a database. When two tables are combined by an outer join, any records from the base table with no matching records from the other table are included in the results and any columns where no values are available are filled with nulls.
  4. Cartesian product: A type of table join: the Cartesian product of two tables consists of the combination of every record in one table with every record in another table.

The remainder of this lesson and the following lesson discuss these joins in detail.

Inner Joins

Below is the equi-join you saw in the previous lesson
The virtual table consists of CD and DISTRIBUTOR

This is also an example of an inner join. The best way to define an inner join is to show the Structured Query Language (SQL) command used to create the records shown in the preceding figure. That command is:
SELECT CDNo, Title, DistID, DistName
FROM CD JOIN Distributor 
ON CD.DistID = Distributor.DistID

This course only touches on the aspects of SQL that are absolutely essential to explaining joins.
See the Sitemap for a listing of courses offering in-depth instruction on writing effective SQL statements.
This join is called an inner join because the join only produces records when there is a match between the value in the fields in the ON statement (in this case, CD.DistID and Distributor.DistID). It's as if the RDBMS pulled the fields for the join from the "inside" of the tables involved and wrote the new records to a virtual table.
You should create an inner join whenever you need to combine records from more than one table, with the provision that the records have matching values in equivalent fields. In the example above, you would create records that listed every
  1. CD's number,
  2. title,
  3. the id. code of the CD's distributor, and
  4. the distributor's name.

The next lesson discusses same-table, outer, and Cartesian product joins.