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:
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:
In this code:
SELECT movies.movie_name, characters.character_name: We are selectingmovie_namefrom themoviestable andcharacter_namefrom thecharacterstable.FROM movies: This specifies the first table in ourFULL JOIN.FULL JOIN characters ON movies.movie_id = characters.movie_id: This joins themoviestable with thecharacterstable usingmovie_idas the common column.
The output is:
- The result set includes
movie_namefrom themoviestable andcharacter_namefrom thecharacterstable wherever there is a match onmovie_id. - If a
movie_idexists in themoviestable but not in thecharacterstable, thecharacter_namewill beNULL. - Conversely, if a
movie_idexists in thecharacterstable but not in themoviestable, themovie_namewill 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_namebut no matchingmovie_idin thecharacterstable; hence, itscharacter_nameisNULL. - The character "T'challa" does not have a corresponding movie entry; hence, its
movie_nameisNULL.
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:
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_namecontains "Stark". The%wildcard allows for any characters to appear before or after "Stark".ORDER BY movies.movie_name: This sorts the filtered results bymovie_namein ascending order.
This result set below shows all movies that include at least one character whose name contains "Stark," ordered by movie_name.
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 JOINs 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.
