What is SQL RIGHT OUTER JOIN
The SQL RIGHT OUTER JOIN is a type of outer join to which prefer all the rows of a right table or second table to combine the two tables. It adds all the rows from the second table to the resulted table. If there is no matching value in the two tables, it returns the null value.
In order to use the right join output in SQL, the query result finds all the rows from the second table and includes the matching rows from the left table. One thing you should notice that if there is no match in both the tables it returns a null value.
How to Use RIGHT OUTER JOIN in SQL
If you want to perform the right outer join in SQL, you have to use the two tables and the syntax given below. The second table is the main table from where you have to take all the rows for the matching columns.
Syntax
1 2 3 4 |
SELECT column_name FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name; |
The above syntax finds all the rows from both tables selected columns in the SQL query. It results out all the matching column rows from both the table and if there is no match for the column value, it returns out null value.
Example of SQL RIGHT OUTER JOIN
Now, let’s create the two tables Purchaser and Seller given below to understand the example of right outer join in SQL server.
Below are the example tables contain one column which has matching rows.
SQL RIGHT OUTER Join Example Using the Select Statement
There are two tables, the first table is Purchaser table and second is the Seller table.
The first table contains the list of the purchasers.
Table 1: Purchaser
Purchaser_ID | Purchaser_Name | Plot_No | Service_Id |
---|---|---|---|
1 | Sam | 12 | 1001 |
2 | Pill | 13 | 1002 |
3 | Don | 14 | 1003 |
4 | Brock | 15 | 1004 |
The second table contains the list of sellers.
Table 2: Seller
Id | Seller_Name | Seller_Email |
---|---|---|
1001 | Big Show | [email protected] |
1002 | Gem | [email protected] |
1003 | Matt | [email protected] |
Now, let’s combine the above two tables using the example given below.
Example
1 2 3 4 |
SELECT Service_Id, Seller_Name, Purchaser_Name, Plot_No FROM Purchaser RIGHT JOIN Seller ON Purchaser.Service_Id = Seller.Id |
Output
Service_Id | Seller_Name | Purchaser_Name | Plot_No |
---|---|---|---|
1001 | Big Show | Sam | 12 |
1002 | Gem | Pill | 13 |
1003 | Matt | Don | 14 |
You must also read.
Reference
Join Syntax