0% found this document useful (0 votes)
38 views11 pages

CIT225 W05 Chapter5

This document discusses different types of joins in SQL, including: - Cross joins that generate a Cartesian product between tables. - Inner joins that return rows where values in joined columns are equal. - Joins between three or more tables using multiple join types. - Joins between a table and subquery result set that share column values. - Self joins that use table aliases to join a table to itself based on a self-referencing key.

Uploaded by

Bismar Cosme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views11 pages

CIT225 W05 Chapter5

This document discusses different types of joins in SQL, including: - Cross joins that generate a Cartesian product between tables. - Inner joins that return rows where values in joined columns are equal. - Joins between three or more tables using multiple join types. - Joins between a table and subquery result set that share column values. - Self joins that use table aliases to join a table to itself based on a self-referencing key.

Uploaded by

Bismar Cosme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CIT 225

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. 

This collection operation is called join.

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

Suppose there are 30 items in Table 1, and 43 items in Table 2. Without


defining how to join the tables, the server will generate these
permutations:
       30 x 43 = 1,290 results

This generation of every permutation is called a Cartesian Product.

The joining of two tables in this way is called cross join.


Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Inner Join

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

Sometimes a table may have a self-referencing foreign key that would


refer to another column in the table. We can use a self-join to write a
query that use table aliases to find information. For example:
mysql> SELECT f.title, f_prnt.title prequel
     -> FROM film f
     ->   INNER JOIN film f_prnt
     ->   ON f_prnt.film_id = f.prequel_film_id
     -> WHERE f.prequel_film_id IS NOT NULL;  

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.

You might also like