Lecture

RIGHT JOIN

A RIGHT JOIN returns all rows from the right table, plus any matching rows from the left table.

If there's no match, columns from the left table will contain NULL.


Syntax

Here is the syntax for a RIGHT JOIN:

RIGHT JOIN Syntax
SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
  • The right table comes after RIGHT JOIN
  • All rows from the right table appear in the result, matched or not

Example: Students and Enrollments

Below is an example of a RIGHT JOIN between the students and enrollments tables.

students

student_idname
1John Smith
2Emily Davis
3Michael Brown
4Jessica Lee
5David Johnson

enrollments

student_idclass_name
1Math
2History
3Science
6Art
7Economics
RIGHT JOIN example
SELECT students.name, enrollments.class_name FROM students RIGHT JOIN enrollments ON students.student_id = enrollments.student_id;

Result:

nameclass_name
John SmithMath
Emily DavisHistory
Michael BrownScience
NULLArt
NULLEconomics

The classes Art and Economics have no matching students, so the name is NULL.


Why It Matters

Use RIGHT JOIN when:

  • You need to preserve every row from the right-side table
  • You're identifying missing or unmatched data from the left
  • The right table is the one you care most about auditing or reporting

Note: SQLite does not support RIGHT JOIN natively. You can workarounds like subqueries or LEFT JOIN with table order reversed.


What's Next?

Next, you'll learn about FULL OUTER JOIN, which returns all rows from both tables — including unmatched rows from each side.

Quiz
0 / 1

What does a RIGHT JOIN return in SQL?

A RIGHT JOIN returns all rows from the table, along with matching rows from the other table.
left
right
both
none

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Tables

Execution Result