Welcome back! By now, you are familiar with different types of SQL joins, such as INNER JOIN, LEFT JOIN, and RIGHT JOIN. Today, we will dive into another powerful join operation known as FULL JOIN.
A FULL JOIN
combines the results of both LEFT JOIN
and RIGHT JOIN
. This means it returns all rows when there is a match in one of the tables, as well as rows that do not have a match in either table. It fills in NULL
values for columns from the table lacking corresponding rows. Essentially, a FULL JOIN
gives you a complete picture of data from two tables.
To give you a quick reminder, here are the three tables we are using:
Movies Table
movie_id | movie_name |
---|---|
1 | Iron Man |
2 | The Incredible Hulk |
3 | Captain America: The First Avenger |
4 | The Avengers |
Movie Details Table
movie_id | imdb_rating | box_office_million_usd |
---|---|---|
1 | 7.9 | 585.2 |
3 | 6.9 | 370.6 |
4 | 8.0 | 1519.6 |
Characters Table
character_id | movie_id | character_name |
---|---|---|
1 | 1 | Tony Stark/Iron Man |
2 | 1 | Pepper Potts |
3 | 1 | James Rhodes |
4 | 1 | Obadiah Stane |
5 | 3 | Steve Rogers/Captain America |
6 | 3 | Peggy Carter |
7 | 3 | Bucky Barnes/Winter Soldier |
8 | 3 | Johann Schmidt/Red Skull |
9 | 3 | Howard Stark |
10 | 4 | Steve Rogers/Captain America |
11 | 4 | Tony Stark/Iron Man |
12 | 4 | Thor |
13 | 4 | Natasha Romanoff/Black Widow |
14 | 4 | Bruce Banner/Hulk |
15 | 4 | Clint Barton/Hawkeye |
16 | 4 | Loki |
17 | 5 | T'challa |
Let's start by examining the basic syntax of a FULL JOIN
in SQL:
SQL1SELECT column1, column2, ...
2FROM table1
3FULL JOIN table2 ON table1.common_column = table2.common_column;
This syntax queries two tables (table1
and table2
) and returns rows where there is either a match in one of the tables or rows that do not match, filling in any missing values with NULL
.
Let's work through an example using our Marvel movies dataset. Suppose we want to join the movies
table and characters
tables. We want to include all rows from both tables.
The SQL query is:
SQL1SELECT movies.movie_name, characters.character_name
2FROM movies
3FULL JOIN characters ON movies.movie_id = characters.movie_id;
In this code:
SELECT movies.movie_name, characters.character_name
: We are selectingmovie_name
from themovies
table andcharacter_name
from thecharacters
table.FROM movies
: This specifies the first table in ourFULL JOIN
.FULL JOIN characters ON movies.movie_id = characters.movie_id
: This joins themovies
table with thecharacters
table usingmovie_id
as the common column.
The output is:
Plain text1 movie_name | character_name 2------------------------------------+------------------------------ 3 Iron Man | Tony Stark/Iron Man 4 Iron Man | Pepper Potts 5 Iron Man | James Rhodes 6 Iron Man | Obadiah Stane 7 Captain America: The First Avenger | Steve Rogers/Captain America 8 Captain America: The First Avenger | Peggy Carter 9 Captain America: The First Avenger | Bucky Barnes/Winter Soldier 10 Captain America: The First Avenger | Johann Schmidt/Red Skull 11 Captain America: The First Avenger | Howard Stark 12 The Avengers | Steve Rogers/Captain America 13 The Avengers | Tony Stark/Iron Man 14 The Avengers | Thor 15 The Avengers | Natasha Romanoff/Black Widow 16 The Avengers | Bruce Banner/Hulk 17 The Avengers | Clint Barton/Hawkeye 18 The Avengers | Loki 19 NULL | T'challa 20 The Incredible Hulk | NULL 21(18 rows)
- The result set includes
movie_name
from themovies
table andcharacter_name
from thecharacters
table wherever there is a match onmovie_id
. - If a
movie_id
exists in themovies
table but not in thecharacters
table, thecharacter_name
will beNULL
. - Conversely, if a
movie_id
exists in thecharacters
table but not in themovies
table, themovie_name
will beNULL
.
Let's break down the output for each row:
- For "Iron Man", we get multiple rows, each showing its characters like "Tony Stark/Iron Man", "Pepper Potts", etc.
- Similarly, "Captain America: The First Avenger" and "The Avengers" have multiple entries showing their respective characters.
- "The Incredible Hulk" has a
movie_name
but no matchingmovie_id
in thecharacters
table; hence, itscharacter_name
isNULL
. - The character "T'challa" does not have a corresponding movie entry; hence, its
movie_name
isNULL
.
So, the final result set includes all movies and all characters, with NULL
values filled in wherever there is no match for a movie_id
.
Once you understand how FULL JOIN
works, you may want to filter and order the results to gain better insights into your data. This can be done using the WHERE
clause for filtering and the ORDER BY
clause for ordering. For example, if you want to see only the movies that have a member of the Stark family with the movies listed in alphabetical order, you can use the following query:
SQL1SELECT movies.movie_name, characters.character_name
2FROM movies
3FULL JOIN characters ON movies.movie_id = characters.movie_id
4WHERE characters.character_name LIKE '%Stark%'
5ORDER BY movies.movie_name;
The key lines of this code are:
WHERE characters.character_name LIKE '%Stark%'
: This condition filters the results to include only those rows where thecharacter_name
contains "Stark". The%
wildcard allows for any characters to appear before or after "Stark".ORDER BY movies.movie_name
: This sorts the filtered results bymovie_name
in ascending order.
This result set below shows all movies that include at least one character whose name contains "Stark," ordered by movie_name
.
Plain text1 movie_name | character_name 2------------------------------------+--------------------- 3 Captain America: The First Avenger | Howard Stark 4 Iron Man | Tony Stark/Iron Man 5 The Avengers | Tony Stark/Iron Man 6(3 rows)
By using filtering and ordering, you can refine and arrange your FULL JOIN
results to better serve your analysis needs.
In this lesson, we explored the concept of FULL JOIN
and its application using the Marvel movies dataset. We saw how FULL JOIN
s can provide a comprehensive view of data by including rows with no matches in one of the tables and handling NULL
values. In addition, we saw how to filter and order the entries of the table, introducing the LIKE %
syntax.
Now, it's your turn to practice! Head over to the practice exercises to apply what you've learned. Keep experimenting with the FULL JOIN
to get comfortable with its behavior.