Overview: In this tutorial you will learn and understand what is RIGHT join in SQL explained with examples.
Introduction RIGHT Join
RIGHT join in SQL returns all row records from the Right side of the SQL table and only those rows from the left table where the join condition matched. Table name mentioned in SELECT statement after FROM keyword is always consider as right table in RIGHT join.
Syntax:
SELECT
column1, Column2, ....
FROM
right_table
RIGHT [OUTER] JOIN left_table
ON right_table.columnName = left_table.columnName;
Example #1: RIGHT join using SELECT statement
So lets understand RIGHT join with example using two table as shown below.
Table is Customers_Tbl and right table is Orders_Tbl
Customer_ID | Customer_Name | |
---|---|---|
121 | Deep | Deep@a.cm |
123 | Robert | Robert@a.cm |
521 | Markus | Markus@a.cm |
500 | Kumar | Kumar@a.cm |
200 | Ravi | Ravi@a.cm |
300 | Daniel | Daniel@a.cm |
Customer_ID | Order_Date | Order_Quantity |
---|---|---|
500 | 4/15/2020 | 3456 |
521 | 6/19/2021 | 3000 |
600 | 8/15/2019 | STV |
You can see yellow marked rows are common in both table so only yellow marked rows will start appearing in front of Customer ID when its matched.
We will apply the RIGHT join on Customer table and below query will bring all common Customer_ID from left table into the Right table as shown below
SELECT
c.Customer_ID, c.Customer_Name,
o.Customer_ID, o.Order_Quantity
FROM
Customers_Tbl c
RIGHT JOIN Orders_Tbl o
ON c.Customer_ID = o.Customer_ID;
The new output table will look like as shown below table
Customer_ID | Customer_name | Customer_ID | Order_Quantity |
---|---|---|---|
500 | Kumar | 500 | 3456 |
521 | Markus | 521 | 3000 |
NULL | NULL | 600 | STV |
So Green column is from left table that is Customer_Tbl and bring all matched rows based on Customer_ID condition.
Example #2: Update table Using RIGHT join
you can easily update your table using join in SQL. Sometime you want to bring records from another table to merge data in one table OR you need to do mapping in main table using Mapping table.
So let take example, how you can update SQL table using UPDATE Command with RIGHT Join.
Below have two table Employee and Country, you want to bring employee’s name from first table to country table as shown below table employee_name is blank.
Employee_ID | Employee_name |
---|---|
2001 | Deep |
2002 | Markus |
2005 | Robert |
2007 | Kumar |
2010 | Mathew |
Employee_ID | Country_name | Employee_name |
---|---|---|
2001 | USA | |
2005 | Germany | |
2007 | India | |
2009 | UK |
Below Query you can use to update Employee table
Update
Country_Table
SET Country_Table.Employee_name = Employee_Table.Employee_name
FROM
Country_Table
RIGHT JOIN Employee_Table
ON Country_Table.Customer_ID = Employee_Table.Customer_ID
Output of above query will be look like below table, and green table is from left table once employee_ID matched.
Employee_ID | Country_name | Employee_name |
---|---|---|
2001 | USA | Deep |
2005 | Germany | Robert |
2007 | India | Kumar |
2009 | UK | NULL |