SQL RIGHT JOIN

  • Join operation in SQL is used to combine multiple tables together into a single table.
  • If we use the right join to combine two different tables, then we will get all the records from the right table. But we will get only those records from the left table, which have the corresponding key in the right table. Rest other records in the left table for which the common column value doesn’t match with the common column value of the right table; displayed as NULL.
  • Let us take a look at the syntax of writing a query to perform the right join operation in SQL.

SELECT TableName1.columnName1, TableName2.columnName2 FROM TableName1   

RIGHT JOIN TableName2 ON TableName1.ColumnName = TableName2.ColumnName;   

    Now let us see take a deeper dive into the right join in SQL with the help of examples. All the queries in the examples will be written using the MySQL database.

    Consider we have the following tables with the given data:

    Table 1: employee

    EmployeeIDEmployee_NameEmployee_Salary
    1Arun Tiwari50000
    2Sachin Rathi64000
    3Harshal Pathak48000
    4Arjun Kuwar46000
    5Sarthak Gada62000
    6Saurabh Sheik53000
    7Shubham Singh29000
    8Shivam Dixit54000
    9Vicky Gujral39000
    10Vijay Bose28000

    Table 2: department

    DepartmentIDDepartment_NameEmployee_ID
    1Production1
    2Sales3
    3Marketing4
    4Accounts5
    5Development7
    6HR9
    7Sales10

    Table 3: Loan

    LoanIDBranchAmount
    1B115000
    2B210000
    3B320000
    4B4100000
    5B5150000
    6B650000
    7B735000
    8B885000

    Table 4: Borrower

    CustIDCustNameLoanID
    1Sonakshi Dixit1
    2Shital Garg4
    3Swara Joshi5
    4Isha Deshmukh2
    5Swati Bose7
    6Asha Kapoor10
    7Nandini Shah9

    Table 5: customer

    Customer_IDNameAgeSalary
    1Aryan Jain5156000
    2Arohi Dixit2125000
    3Vineet Garg2431000
    4Ajeet Singh2332000
    5Ravi Rathod2342000
    6Paras Aggrawal2250000
    7Sonakshi Kapadiya2428000
    8Sonali Kapoor2882000

    Table 6: orders

    Order_IDOrder_DateCutomer_IDAmount
    12012-01-2023000
    22012-05-1822000
    32012-06-2834000
    42012-04-1145000
    52012-05-0488000

    Example 1:

    Write a query to perform a right join operation considering the employee table as the left table and the department table as the right table.

    Query:

    mysql> SELECT e.EmployeeID, e.Employee_Name, e.Employee_Salary, d.DepartmentID, d.Department_Name FROM employee e RIGHT JOIN department d ON e.EmployeeID = d.Employee_ID;  

    We have used the SELECT command to retrieve EmployeeID, Employee_Name, Employee_Salary, DepartmentID, Department_Name present in the employee and department table. Then we have used the RIGHT JOIN keyword to perform the right join operation on the employee and department table where ‘e’ and ‘d’ are aliases. These two tables are joined on the column EmployeeID which is present in both the tables.

    You will get the following output:

    EmployeeIDEmployee_NameEmployee_SalaryDepartmentIDDepartment_Name
    1Arun Tiwari500001Production
    3Harshal Pathak480002Sales
    4Arjun Kuwar460003Marketing
    5Sarthak Gada620004Accounts
    7Shubham Singh290005Development
    9Vicky Gujral390006HR
    10Vijay Bose280007Sales

    EmployeeID, Employee_Name, Employee_Salary, DepartmentID, Department_Name are retrieved from employee and department tables. All the records from the department table are retrieved. Only those records that have a corresponding EmployeeID in the department table are retrieved from the employee table.

    Example 2:

    Write a query to perform the right join operation considering the loan table as the left table and the borrower table as the right table.

    Query:

    mysql> SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.CustName FROM Loan l RIGHT JOIN Borrower b ON l.LoanID = b.LoanID;  

    We have used the SELECT command to retrieve LoanID, Branch, Amount, CustID, CustName present in the loan and borrower table. Then we have used the RIGHT JOIN keyword to perform the right join operation on the loan and borrower table where ‘l’ and ‘b’ are aliases. These two tables are joined on the column LoanID which is present in both the tables.

    You will get the following output:

    LoanIDBranchAmountCustIDCustName
    1B1150001Sonakshi Dixit
    4B41000002Shital Garg
    5B51500003Swara Joshi
    2B2100004Isha Deshmukh
    7B7350005Swati Bose
    NULLNULLNULL6Asha Kapoor
    NULLNULLNULL7Nandini Shah

    LoanID, Branch, Amount, CustID, CustName are retrieved from loan and borrower tables. All the records from the borrower table are retrieved. Only those records that have a corresponding LoanID in the borrower table are retrieved from the loan table. Rest other records in the loan table for which a LoanID doesn’t match with the LoanID of the borrower table, are displayed as NULL.

    Example 3:

    Write a query to perform a right join operation considering the customer table as the left table and the orders table as the right table.

    Query:

    mysql> SELECT c.Customer_ID, c.Name, c.Age, c.Salary, o.Order_ID, o.Order_Date, o.Amount FROM customer c RIGHT JOIN orders o ON c.Customer_ID = o.Customer_ID;  

    We have used the SELECT command to retrieve Customer_ID, Name, Age, Salary, Order_ID, Order_Date, Amount present in customer and orders table. Then we have used the RIGHT JOIN keyword to perform the right join operation on the customer and orders table where ‘c’ and ‘o’ are aliases. These two tables are joined on the column Customer_ID which is present in both the tables.

    You will get the following output:

    Customer_IDNameAgeSalaryOrder_IDOrder_DateAmount
    2Arohi Dixit212500012012-01-203000
    2Arohi Dixit212500022012-05-182000
    3Vineet Garg243100032012-06-284000
    4Ajeet Singh233200042012-04-115000
    8Sonali Kapoor288200052012-05-048000

    Customer_ID, Name, Age, Salary, Order_ID, Order_Date, Amount are retrieved from customer and orders tables. All the records from the orders table are retrieved. From the customer table, only those records which have a corresponding Customer_ID in the orders table are retrieved.


    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *