RIGHT JOIN
A RIGHT JOIN returns all rows from the right table, along with any matching rows from the left table.
If there's no match, the 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 (
table2) comes after the RIGHT JOIN keyword - All rows from the right table appear in the result, whether or not they have a match
Example: Students and Enrollments
Below is an example of a RIGHT JOIN between the students and enrollments tables.
students
| student_id | name |
|---|---|
| 1 | John Smith |
| 2 | Emily Davis |
| 3 | Michael Brown |
| 4 | Jessica Lee |
| 5 | David Johnson |
enrollments
| student_id | class_name |
|---|---|
| 1 | Math |
| 2 | History |
| 3 | Science |
| 6 | Art |
| 7 | Economics |
RIGHT JOIN example
SELECT students.name, enrollments.class_name FROM students RIGHT JOIN enrollments ON students.student_id = enrollments.student_id;
Result:
| name | class_name |
|---|---|
| John Smith | Math |
| Emily Davis | History |
| Michael Brown | Science |
| NULL | Art |
| NULL | Economics |
The classes
ArtandEconomicshave no matching students, so theirnamevalues areNULL.
LEFT JOIN vs RIGHT JOIN
Both joins return unmatched rows, but from opposite sides:
- LEFT JOIN → keeps all rows from the left table, adds matches from the right
- RIGHT JOIN → keeps all rows from the right table, adds matches from the left
In essence, they are mirror images of each other.
In practice,
LEFT JOINis used more frequently, and aRIGHT JOINcan usually be rewritten as aLEFT JOINby swapping the table order.
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