A query result can include data from multiple tables. To combine data from tables, you use the Join operation from SQL.
The basic join operation
The Join operation matches rows of one table with rows of another table, based on values in those rows. For example, you can join the table titles
with the table publishers
. Each row in the result set will describe a title, including information about that titleÆs publisher, as shown in the following illustration:
Types of joins
When you join tables, the type of join that you create affects the rows that appear in the result set. You can create the following types of joins:
titles
and publishers
tables to create a result set that shows the publisher name for each title. In an inner join, titles for which you do not have publisher information are not included in the result set, nor are publishers with no titles. The resulting SQL for such a join might look like this: SELECT title, pub_name
FROM titles INNER JOIN publishers
ON titles.pub_id = publishers.pub_id
Note Columns containing NULL do not match any values when you are creating an inner join and are therefore excluded from the result set. Null values do not match other null values.
titles
and publishers
tables to include all titles, even those you do not have publisher information for: SELECT titles.title_id,
titles.title,
publishers.pub_name
FROM titles LEFT OUTER JOIN publishers
ON titles.pub_id
= publishers.pub_id
titles
and publishers
tables will include all publishers, even those who have no titles in the titles
table. The resulting SQL might look like this: SELECT titles.title_id,
titles.title,
publishers.pub_name
FROM titles RIGHT OUTER JOIN publishers
ON titles.pub_id
= publishers.pub_id
titles
and publishers
shows all titles and all publishers, even those that have no match in the other table. SELECT titles.title_id,
titles.title,
publishers.pub_name
FROM titles FULL OUTER JOIN publishers
ON titles.pub_id
= publishers.pub_id
SELECT *
FROM authors CROSS JOIN publishers
Joining columns
The JOIN operator matches rows by comparing values in one table with values in another. You decide which columns from each table should be matched. You have several choices:
SELECT *
FROM discounts INNER JOIN stores
ON stores.stor_id = discounts.stor_id
SELECT au_lname,
au_fname,
pub_name,
authors.state
FROM authors INNER JOIN publishers
ON authors.state
= publishers.state
Note also that you use multiple columns to match rows from the joined tables. For example, to find the author-publisher pairs in which the author and publisher are located in the same city, you use a join operation matching the respective state columns and the respective city columns of the two tables. You need to match both city and state because it is possible that different states could have like-named cities (e.g., Springfield, Illinois and Springfield, Massachusetts).
Join comparison operators
The JOIN operator matches rows by comparing values in one table with values in another. You can decide exactly what constitutes a match. Your choices fall into two broad categories:
SELECT *
FROM publishers INNER JOIN pub_info
ON publishers.pub_id
= pub_info.pub_id
SELECT
fname, minit, lname,
job_desc, job_lvl, min_lvl
FROM employee INNER JOIN jobs
ON employee.job_lvl
< jobs.min_lvl
Joining tables
When combining data from multiple tables, you must decide what tables to use. There are several noteworthy considerations:
SELECT
title,
au_fname,
au_lname,
royaltyper
FROM
authors
INNER JOIN
titleauthor
ON authors.au_id
= titleauthor.au_id
INNER JOIN
titles
ON titleauthor.title_id
= titles.title_id
SELECT title, stor_name
FROM titles
INNER JOIN
sales
ON titles.title_id = sales.title_id
INNER JOIN
stores
ON
sales.stor_id = stores.stor_id
Notice that the sales table contributes no columns to the result set.