Home » SQL » Right Join in SQL

Right Join in SQL

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_IDCustomer_NameEmail
121DeepDeep@a.cm
123RobertRobert@a.cm
521MarkusMarkus@a.cm
500KumarKumar@a.cm
200RaviRavi@a.cm
300DanielDaniel@a.cm
Customers_Tbl (Left table)
Customer_IDOrder_DateOrder_Quantity
5004/15/20203456
5216/19/20213000
6008/15/2019STV
Orders_Tbl (Right table)

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_IDCustomer_nameCustomer_IDOrder_Quantity
500Kumar5003456
521Markus5213000
NULLNULL600STV
Orders_Tbl (Right table)

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_IDEmployee_name
2001Deep
2002Markus
2005Robert
2007Kumar
2010Mathew
employee_Table
Employee_IDCountry_nameEmployee_name
2001USA
2005Germany
2007India
2009UK
Country_Table

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_IDCountry_nameEmployee_name
2001USADeep
2005GermanyRobert
2007IndiaKumar
2009UKNULL
Country_Table