CIT225 W05 Chapter5
CIT225 W05 Chapter5
Learning SQL
Chapter 5 – Querying Multiple Tables
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
What This PowerPoint Covers:
• Learn how to perform a cross join to get a Cartesian product.
• Learn how to perform an inner join to get the intersection or match
between two tables that share a column or set of column values.
• Learn how to perform an inner join to get the intersection or match
between a table and subquery result set that share a column or set of
column values.
• Learn how to perform an inner join to get the intersection or match
between three or more tables that share a set of columns values..
• Learn how to perform self-joins between two copies of the same table
by using table aliases.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Performing a Join
Retrieving information from two tables requires a query that instructs
the server to use the foreign key to act as a transport between the two
tables. This allows information to be collected from both tables in one
query.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Cartesian Product
If we do not specify in the query how to join tables, the server will
generate every permutation of the two tables. That is, the server will
join every item in one table with every item of the other table
A query needs to describe how the two tables are related in order to
return a single row for each item. For example:
mysql> SELECT c.first_name, c.last_name, a.address
-> FROM customer c JOIN address a
-> ON c.address_id = a.address_id;
If there is a value in one table, but not the other, the join will fail for that
row, and the results will not be included in the results. This is called an
inner join when accompanied by a join statement. This is the the default
join function run by the server with a join statement.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Inner Join Between Two Tables
There are several different type of joins. It is wise to get into the habit of
specifying the join type, even if it is the default inner join.
mysql> SELECT c.first_name, c.last_name, a.address
-> FROM customer c INNER JOIN address a
-> ON c.address_id = a.address_id;
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Inner Join Between Two Tables
There are several different type of joins. It is wise to get into the habit of
specifying the join type, even if it is the default inner join.
mysql> SELECT c.first_name, c.last_name, a.address
-> FROM customer c INNER JOIN address a
-> ON c.address_id = a.address_id;
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Join Three or More Tables
When joining three or more tables, we must include three (or more)
tables and two (or more) join types in the from clause. We will also
have two on subclauses. For example:
mysql> SELECT c.first_name, c.last_name, ct.city
-> FROM customer c
-> INNER JOIN address a
-> ON c.address_id = a.address_id;
-> INNER JOIN city ct
-> ON a.city_id = ct.city_id;
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Join Using Subqueries as Tables
We can create queries that use a subquery like a table. When we write
the query, we put a second query in parentheses in the place of a table.
We then use an alias to identify this. For example:
mysql> SELECT c.first_name, c.last_name, ct.city
-> FROM customer c
-> INNER JOIN address a
-> (SELECT a.address_id, a.address, ct.city
-> FROM address a
-> INNER JOIN city ct
-> ON a.city_id = ct.city_id;
-> WHERE a.district = 'California'
-> ) addr
-> ON c.address_id = addr.address_id;
The results of this query will pull all addresses that are in California, without
listing all addresses.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Join Using the Same Table Twice
We can create queries that will return results that use two items from
the same table. For example:
mysql> SELECT f.title
-> FROM film f
-> INNER JOIN film_actor fa1
-> ON f.film_id = fa1.film_id
-> INNER JOIN actor a1
-> On fa1.actor_id = a1.actor_id
-> INNER JOIN film_actor fa2
-> ON f.film_id = fa2.film_id
-> INNER JOIN actor a2
-> On fa2.actor_id = a2.actor_id
-> WHERE (a1.first_name = 'CATE' AND a1.last_name = 'MCQUEEN'
-> AND (a2.first_name = 'CUBA' AND a2.last_name = 'BIRCH');
The results of this query will pull all films that both Cate McQueen and Cuba
Birch are in.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Self-Joins
The results of this query will list all films in the table that have prequels,
along with the prequel title.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.