Right Outer Join
What You'll Learn
You'll learn how to use RIGHT JOIN to preserve all rows from the right table in a join, filling in NULL values where no match exists in the left table.
The Core Idea
A RIGHT OUTER JOIN (often shortened to just RIGHT JOIN) is the mirror image of a left outer join. It returns all rows from the right table and matching rows from the left table. When a row in the right table has no corresponding match in the left table, the result still includes that row—but all columns from the left table are filled with NULL values.
Think of it like a school roster where you're tracking which students (right table) have completed their homework assignments (left table). A right outer join ensures every student appears in your report, even if they haven't submitted any assignment yet—those students just show NULL for assignment details.
SQL Syntax
SELECT columns
FROM left_table
RIGHT JOIN right_table
ON left_table.key = right_table.key;
Practical Example
Imagine you have an assignments table (left) and a students table (right):
SELECT students.name, assignments.title, assignments.grade
FROM assignments
RIGHT JOIN students
ON assignments.student_id = students.id;
This query returns every student, even those who haven't submitted any assignments. For students without assignments, title and grade will be NULL.
When to Use It
Use RIGHT JOIN when the right table is your "master list" that must appear completely in your results, regardless of matches. In practice, many developers prefer converting right joins to left joins (by swapping table order) for consistency, but understanding both directions gives you flexibility.
Key Takeaway: RIGHT JOIN includes all rows from the right table, with NULL values in left-table columns when no match exists—guaranteeing complete right-side representation.