
Merging Data with RIGHT-, INNER- and FULL JOIN
Introduction
We have learned to use a LEFT JOIN.
This is the most common way to combine data from tables.
We now look at some other ways:

1. RIGHT JOIN

A LEFT JOIN returns all results from a left table, with matching results from a right table.
- A
RIGHT JOINis the opposite of aLEFT JOIN.
A RIGHT JOIN returns all results from a right table, with matching results from a left table.
1.1. LEFT JOIN example
| id | name | year | rank | movie_id | genre |
|---|---|---|---|---|---|
| 10920 | Aliens | 1986 | 8.2 | 10920 | Sci-Fi |
| 10920 | Aliens | 1986 | 8.2 | 10920 | Action |
| 10920 | Aliens | 1986 | 8.2 | 10920 | Thriller |
| 10920 | Aliens | 1986 | 8.2 | 10920 | Horror |
| ... | ... | ... | ... | ... | ... |
1.2. RIGHT JOIN example
| movie_id | genre | id | name | year | rank |
|---|---|---|---|---|---|
| 10920 | Sci-Fi | 10920 | Aliens | 1986 | 8.2 |
| 10920 | Action | 10920 | Aliens | 1986 | 8.2 |
| 10920 | Thriller | 10920 | Aliens | 1986 | 8.2 |
| 10920 | Horror | 10920 | Aliens | 1986 | 8.2 |
| ... | ... | ... | ... | ... | ... |
This gives the same result (in a different order) as the LEFT JOIN.
1.3. When to use RIGHT JOIN
In practice, you will mostly use a LEFT JOIN, not a RIGHT JOIN.
A RIGHT JOIN can make your query clearer if you want to merge more than 2 tables in 1 query.
2. INNER JOIN

A LEFT JOIN returns all results from a left table, with matching results from a right table.
- An
INNER JOINreturns only results where there is a match in results from the left table and right table.
An INNER JOIN is therefore the same as:
LEFT JOINfollowed by filtering out empty values on added columns from the right table.
2.1. INNER JOIN example
SELECT
movies_after_2000.*,
movies_genres.genre
FROM
movies_genres
INNER JOIN (
SELECT * FROM movies WHERE year > 2000
) movies_after_2000 ON movies_after_2000.id = movies_genres.movie_id;
This example uses a subquery, making movies_after_2000 now refer to a table with only movies from after 2000 instead of all movies.
| id | name | year | rank | movie_id | genre |
|---|---|---|---|---|---|
| 10920 | Aliens | 1986 | 8.2 | 10920 | Sci-Fi |
| 10920 | Aliens | 1986 | 8.2 | 10920 | Action |
| 10920 | Aliens | 1986 | 8.2 | 10920 | Thriller |
| 10920 | Aliens | 1986 | 8.2 | 10920 | Horror |
| ... | ... | ... | ... | ... | ... |
You only see genres for which movies are also available that were released after 2000:
-
The query returns 35 rows as a result.
-
However, the
movies_genrestable contains 103 rows. Check this yourself usingSELECT COUNT(*) FROM movies_genres. -
The remaining 68 rows are linked to movies that were released before 2000 and are therefore not in the result of the
INNER JOINquery.
2.2. When to use INNER JOIN
In practice, you will mostly use a LEFT JOIN, not an INNER JOIN.
An INNER JOIN is used when you want only results that appear in both tables.
3. FULL JOIN

A LEFT JOIN returns all results from a left table, with matching results from a right table.
A FULL JOIN returns all results from a left table, with all results from a right table.
2.1. FULL JOIN example
| id | name | year | rank | movie_id | genre |
|---|---|---|---|---|---|
| 10920 | Aliens | 1986 | 8.2 | 10920 | Sci-Fi |
| 10920 | Aliens | 1986 | 8.2 | 10920 | Action |
| 10920 | Aliens | 1986 | 8.2 | 10920 | Thriller |
| 10920 | Aliens | 1986 | 8.2 | 10920 | Horror |
| ... | ... | ... | ... | ... | ... |
- You see all movies, and also all genres.
- For movies for which no genres are known, the columns from the
movies_genrestable are empty. - For genres for which no movies are known, the columns from the
moviestable are empty.
2.2. When to use FULL JOIN
In practice, you will mostly use a LEFT JOIN, not a FULL JOIN.
A FULL JOIN is used when you want all results that appear in both tables.
Summary

LEFT JOINis the most used.RIGHT JOINis the opposite of aLEFT JOIN.- Sometimes used for better clarity when merging more than 2 tables.
INNER JOIN.- When you want only results that appear in both tables.
FULL JOIN.- When you want all results from a left table, with all results from a right table.