Let's say we want to display the name of all students and the names of the courses they are enrolled in, in two columns side by side. However, we also want to include courses that don't have any students enrolled in them.
This again, is a problem where INNER JOIN
will fail.
There are only 22 records in the final result. It does not contain the language C
,
where no student is enrolled.
This is where RIGHT OUTER JOIN
comes in.
A RIGHT OUTER JOIN
or a RIGHT JOIN
returns records from the table at the right in the JOIN, that are unmatched from the connecting query in ON
, along with the matched records.
Note that the table courses
is at the right in the RIGHT JOIN
clause, so all records
from the courses
table appear in the result. For the courses that don't have a student enrolled in, the student name is returned as blank.