Right Join

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
By Srikanth Varma
Free
star5
Enrolled: 1000
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
Srikanth Varma
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

What is a Right Join in SQL?

As you already know, the term JOIN combines the records of two or more tables. In this section, you will learn about one of the 3 forms of OUTER JOINs, i.e. "RIGHT JOIN in SQL".

The RIGHT JOIN in SQL returns all the records from the table that is to the right of the JOIN keyword. The resultant table also includes the matched records from the left table, but if no records match in both tables, it returns NULL.

For example, you have two tables, Table X and Table Y. If the RIGHT JOIN in SQL is applied to the two tables, the resultant table includes all the records from Table Y (because it is on the right side of JOIN) and also the matched records from Table X and the rows without a match returns NULL. The below Venn diagram shows how the RIGHT JOIN works on Table X and Table Y.

diagram-representation-of-right-join-in-sql

Let's see the syntax of the RIGHT JOIN in SQL.

The RIGHT JOIN in SQL is also called as RIGHT OUTER JOIN.

Why Use the Right Join in SQL?

The RIGHT JOIN in SQL is used when you want to JOIN the records of the tables. When you want to return all the rows from one table and only the matched rows from the other tables, then also RIGHT JOIN is used.

The RIGHT JOIN is opposite to that of LEFT JOIN. It returns all the rows from the right table and rows of the left table for which the JOIN condition is satisfied.

Let's take an example to understand why and where to use the right join in SQL.

Customers:

cust_idnameemailcity
101Surbhi Shahsurbhi@helloAhmedabad
104Meet Velanimeet@googleNagpur
108Raj Singhrajsingh@yahooMumbai
112Shreya Vaghelashreya@googleBangalore
115Ritik Sharmaritik@yahooPune

Orders:

order_iditemsamountcustomer
110010000104
2502000105
3604000108
4556600115
5404000107

SQL:

Output:

cust_idnamecityorder_iditemsamount
104Meet VelaniNagpur110010000
108Raj SinghMumbai3604000
115Ritik SharmaPune4556600
NULLNULLNULL2502000
NULLNULLNULL5404000

As shown in the output, the RIGHT JOIN in SQL will return the result set of those rows where there is a match between cust_id of the Customers table and customer of the Orders table along with all the remaining rows from the table which is present at the right of the JOIN i.e. Orders table.

Thus to retrieve such results, you can use the RIGHT JOIN in SQL. This is also possible when you swap the tables, and instead of RIGHT JOIN, use the LEFT JOIN to get the same results.

Example of Right Join in SQL

Now, let's take some examples to understand the working of RIGHT JOIN in SQL.

Consider the two tables, Employee and Departments table, using which the RIGHT JOIN is explained in the below example.

Employee:

IDnamecitydate_of_joiningsalarydept_id
101KunalRajasthan2022 - 02 - 26400001
104SavioJaipur2022 - 04 - 19500004
109RonakMumbai2022 - 01 - 30600002
112MitaliJaipur2022 - 02 - 15300003
143KavishDelhi2022 - 05 - 23550001
132YakshAhmedabad2022 - 06 - 11350005

Department:

dept_iddept_namedept_strength
1Finance10
2Marketing3
3IT20
4HR4
5Sales5
6Product2

Example - 1

Now, you want to fetch the records of all the departments in the company and the details of the employees currently working in the departments.

To achieve this, you can use RIGHT JOIN in SQL. The below SQL query is used to retrieve all the departments that are available in the company, along with the employee details.

SQL:

Output:

IDnamecitydate_of_joiningsalarydept_iddept_name
101KunalRajasthan2022 - 02 - 26400001Finance
143KavishDelhi2022 - 05 - 23550001Finance
109RonakMumbai2022 - 01 - 30600002Marketing
112MitaliJaipur2022 - 02 - 15300003IT
104SavioJaipur2022 - 04 - 19500004HR
132YakshAhmedabad2022 - 06 - 11350005Sales
NULLNULLNULLNULLNULL6Product

As shown in the above example, the RIGHT JOIN in SQL includes all the records from the table that is present on the right side of the JOIN, i.e. Department table. Notice that there is no match of the dept_id number 6 from the Employee table. Still, the department of Product is included in the final result.


Example - 2

Now, let’s take another example in which you’re provided with the Products table and Suppliers table. The SQL query fetches the details of all the suppliers supplying multiple products to the vendor.

Products:

product_idproduct_namesupplier_idprice
1Blueberry Sauce101300
2Jumbo Spice Bread501200
3Apple Cider Vinegar101500
4Apple Pie401330
5Cheese Cake301500
6Strawberry Milkshake101250
7Fresh Watermelon juice501400

Suppliers:

supplier_idsupplier_namecityphone
101FoodexMumbai9112 838 727
501Food FieryDelhi9132 232 124
401Feed BearPune9575 124 252
330IndustryBeaChennai9657 673 562
601Feed DuskAhmedabad9456 213 685
701FoodonusDelhi9346 828 239

SQL:

Output:

product_idproduct_namesupplier_idpricesupplier_namecityphone
1Blueberry Sauce101300FoodexMumbai9112 838 727
3Apple Cider Vinegar101500FoodexMumbai9112 838 727
6Strawberry Milkshake101250FoodexMumbai9112 838 727
2Jumbo Spice Bread501200Food FieryDelhi9132 232 124
7Fresh Watermelon juice501400Food FieryDelhi9132 232 124
4Apple Pie401330Feed BearPune9575 124 252
NULLNULL330NULLIndustryBeaChennai9657 673 562
NULLNULL601NULLFeed DuskAhmedabad9456 213 685
NULLNULL701NULLFoodonusDelhi9346 828 239

In the above example, the details of all the suppliers are fetched as the products that they deliver to the vendor. Note that a single supplier delivers multiple products, and hence all the matching records from the Suppliers table with the Products table are retrieved.

Learn More About Join in SQL

Conclusion

  • The RIGHT JOIN combines two or more tables in SQL.
  • The RIGHT JOIN in SQL returns all the records from the right table, i.e. table A, and matching records from the left table, i.e. table B.
  • If a row in the right table does not match with the left table, then the resultant record of the left table displays NULL.
  • RIGHT JOIN and RIGHT OUTER JOIN in SQL are the same. The OUTER keyword is optional.
  • In a JOIN query, the table that appears to the left of JOIN or that appears leftmost in the JOIN clause is the left table, and the right table is the one that appears to the right of the JOIN.
  • The RIGHT JOIN is opposite to that of LEFT JOIN. It returns all the rows from the right table and rows of the left table for which the JOIN condition is satisfied.