SQL RIGHT JOIN
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
Table 2: department
Table 3: Loan
Table 4: Borrower
Table 5: customer
Table 6: orders
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: 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:
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: 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:
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: 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_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. Next TopicSQL Full Join |