RIGHT JOIN (or RIGHT OUTER JOIN) returns all the rows from the right table and the matching rows from the left table. If there is no match, the result will have null values for the left table columns.
SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
• 'table1' and 'table2' refer to the two tables being joined together.
• 'column_name(s)' refer to the selected columns from the tables being joined together.
• 'ON' clause is used to specify the conditions for joining two or more tables together.
As seen in above image, SQL right join produces a result set that includes each row from the right table (Table-B) and any matching rows from the right table (Table-A), if any.
Let's suppose we have two tables :
[a] Employees Table :
→ It contains columns [Employee_Id], [Employee_Name], [Gender] and [Department_Id].
[b] Departments Table :
→ It contains columns [Department_Id] and [Department_Name].
1. Let's use SQL right join to retrieve the employees names and their respective department names by joining the [Employees] and [Departments] tables together based on matching values in both the tables.
2. Please run the following SQL statement :
SELECT Emp.Employee_Name, Emp.Department_Id, Dpt.Department_Name FROM [Employees] Emp RIGHT JOIN [Departments] Dpt ON Emp.Department_Id = Dpt.Department_Id
3. Above statement selects the columns from both tables based on the matching values in the 'DepartmentId' column. It returns all the rows from the right table and the matching rows from the left table as the output.
• As you have seen, Only specific selected columns ( [Employee_Name], [Department_Id] from "Employees" table and [Department_Name] from "Departments" table ) are returned.
• It returned rows where the [Department_Id] column contained the values '1', '2'. This was done because '1' and '2' were the only values shared by both tables.
• The right table ('Departments') also included 'Sales' and 'Maintenance' values but with no matching 'Department_Name' value based on [Department_Id] column, resulting in a 'NULL' value for the left table columns."
That's it! You have successfully retrieved data using 'RIGHT JOIN' statement.
A right join in SQL returns all records from the right table and the matching records from the left table, with unmatched records in the left table being null.
The 4 types of JOINs in SQL are INNER JOIN, LEFT JOIN (or LEFT OUTER JOIN), RIGHT JOIN (or RIGHT OUTER JOIN), and FULL JOIN (or FULL OUTER JOIN).
The main difference between an inner join and a right join in SQL is that an inner join returns only the matching records between both tables, while a right join returns all records from the right table along with matching records from the left table.
The difference between a left join and a right join in SQL lies in which table's records are preserved. A left join preserves all records from the left table and includes matching records from the right table, whereas a right join preserves all records from the right table and includes matching records from the left table.