Understanding Right Join in SQL Server with simple examples

It returns all records from right side table and matching records from left side table.

Right Join Diagram


RIGHT JOIN SYNTAX:

SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;

RIGHT JOIN EXAMPLE 1:

Requirement: I need to display all records from Country and matching records from Users

SELECT u.UserName,u.CountryName from users u RIGHT JOIN country c on u.countryid=c.countryid;

RIGHT JOIN EXAMPLE 2:

Requirement: I need to display all records from Orders and matching records from Customers

SELECT c.CustomerName,ord.OrderDate from Customers c RIGHT JOIN Orders ord ON c.Customerid=ord.Customerid

RIGHT JOIN EXAMPLE 3:

Requirement: I need to display all records from Course and matching records from Students

SELECT s.StudentName,c.CourseName from Students s RIGHT JOIN Course c ON s.Studentid = c.Studentid