01 / 21

RIGHT JOIN &
FULL OUTER JOIN

Master two powerful SQL join types that ensure no data gets left behind: keeping all rows from the right table, or from both tables.

SQL

What Are SQL JOINs?

A JOIN combines rows from two or more tables based on a related column between them. Think of it as merging two spreadsheets using a shared key, like a student ID or order number.

LEFT JOIN
Keeps all rows from the left table, matches from the right
RIGHT JOIN
Keeps all rows from the right table, matches from the left
FULL OUTER
Keeps all rows from both tables, filling gaps with NULL

The JOIN Family Visualized

INNER INNER JOIN ALL LEFT JOIN ALL RIGHT JOIN ALL ALL FULL OUTER JOIN Table A Table B

Why Do These Joins Matter?

In real databases, not every row has a match. Some customers have never ordered. Some products have never been purchased. These joins ensure you see the complete picture:

1
Data auditing: find records in one table that have no corresponding entry in another.
2
Complete reporting: generate reports that include all entities, even those with missing data.
3
Data reconciliation: compare two datasets and identify mismatches from both sides.

The RIGHT JOIN Syntax

SELECT columns FROM left_table RIGHT JOIN right_table ON left_table.key = right_table.key;

RIGHT JOIN in Action

Orders order_id cust_id 101 1 102 2 Customers ★ cust_id name 1 Alice 2 Bob 3 Carol match match no match Result order_id cust_id name source 101 1 Alice matched 102 2 Bob matched NULL 3 Carol no match

The FULL OUTER JOIN Syntax

SELECT columns FROM left_table FULL OUTER JOIN right_table ON left_table.key = right_table.key;

FULL OUTER JOIN in Action

Students ★ stu_id name 1 Amy 2 Ben 3 Cara Grades ★ stu_id grade 1 A 2 B 4 C match match no match → ← no match FULL OUTER JOIN Result stu_id name stu_id grade status 1 Amy 1 A matched 2 Ben 2 B matched 3 Cara NULL NULL left only NULL NULL 4 C right only

LEFT JOIN vs RIGHT JOIN

They are mirror images of each other. Any RIGHT JOIN can be rewritten as a LEFT JOIN by swapping the table order:

-- These two queries produce identical results: SELECT * FROM A RIGHT JOIN B ON A.id = B.id; SELECT * FROM B LEFT JOIN A ON B.id = A.id;

INNER vs FULL OUTER

INNER JOIN
Returns only rows that have matches in both tables. Unmatched rows are discarded entirely.
FULL OUTER JOIN
Returns all rows from both tables. Unmatched rows appear with NULL values filling the missing side.

How to Choose Your JOIN

1
Identify your tables: which table is "primary" (the one you want all rows from)?
2
Determine the key: find the shared column (foreign key) that connects the two tables.
3
Decide what to keep: all from the right? Use RIGHT JOIN. All from both? Use FULL OUTER JOIN.
4
Handle NULLs: plan for NULL values in your SELECT or WHERE clause when unmatched rows appear.
5
Test your query: verify with small data to confirm NULLs appear where expected.

Example 1: RIGHT JOIN

A school has a Clubs table and a Students table. Find all students, including those not in any club:

SELECT c.club_name, s.student_name FROM Clubs c RIGHT JOIN Students s ON c.student_id = s.id;

Example 2: FULL OUTER JOIN

A store has Products and Sales tables. Find all products and all sales, even if a product was never sold or a sale references a deleted product:

SELECT p.product_name, s.sale_date, s.quantity FROM Products p FULL OUTER JOIN Sales s ON p.id = s.product_id;

Example 3: Finding Orphan Records

Use FULL OUTER JOIN with a WHERE clause to find records that exist in only one table: the orphan records.

SELECT p.product_name, s.sale_id FROM Products p FULL OUTER JOIN Sales s ON p.id = s.product_id WHERE p.id IS NULL OR s.product_id IS NULL;

Common Mistakes to Avoid

✕ Confusing LEFT and RIGHT
Forgetting which table is "preserved." Always check: the keyword names the table whose rows are all kept.
✕ Ignoring NULLs in WHERE
Writing WHERE col = value after an outer join can accidentally filter out NULL rows. Use IS NULL or COALESCE.
✕ Using FULL OUTER in MySQL
MySQL does not support FULL OUTER JOIN directly. You must use a UNION of LEFT JOIN and RIGHT JOIN instead.

JOIN Types Compared

Feature INNER LEFT RIGHT FULL OUTER
Matched rows ✓ ✓ ✓ ✓
Unmatched left rows ✕ ✓ ✕ ✓
Unmatched right rows ✕ ✕ ✓ ✓
NULLs for missing data Never Right side Left side Both sides
Best for Exact matches All left + matches All right + matches Complete picture

Question 1 of 5

In a RIGHT JOIN, which table's rows are guaranteed to all appear in the result?

Question 2 of 5

If Table A has 3 rows and Table B has 5 rows, and only 2 rows match on the key, how many rows does a FULL OUTER JOIN produce?

Question 3 of 5

Which SQL statement is equivalent to: SELECT * FROM A RIGHT JOIN B ON A.id = B.id?

Question 4 of 5

After a FULL OUTER JOIN, you add WHERE A.id IS NULL OR B.id IS NULL. What does this return?

Question 5 of 5

In a RIGHT JOIN result, where do NULL values appear?