Right Join in SQL

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

The RIGHT JOIN in SQL returns all the records from the table 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 Venn diagram below shows how the RIGHT JOIN works on Table X and Table Y.

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

Syntax

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

SQL RIGHT Join Example

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 available in the company, along with the employee details.

Syntax:

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 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, and the Product's price value is greater than 100.

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

Syntax:

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; hence, all the matching records from the Suppliers table with the Products table are retrieved.

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 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.