A RIGHT OUTER JOIN in SQL is a type of join that combines rows from two or more tables based on a specified condition and includes all rows from the right (or second) table, regardless of whether there is a match in the left (or first) table. If there is no match, NULL values are filled in for columns from the left table.
Syntax
SELECT *
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;Example
Consider we have two tables, students and grades, with information about students and their respective grades:
Image scaled to 85%
Now, let’s perform a RIGHT OUTER JOIN to retrieve information about all grades and their corresponding students:
Database Exercise
Database Schema:
-- Database schema would be rendered hereExercise Script:
-- Exercise script would be rendered hereAvailable actions: Execute
Result
Image scaled to 45%
In this example, the RIGHT OUTER JOIN ensures that all grades from the grades table are included in the result set. The matching rows display the respective students from the students table, while the grade with student_id “5” does not have a corresponding student, resulting in NULL values for student-related columns. This is useful when retrieving information about all grades, including those without associated students.