SQL RIGHT JOIN
Basic Fundamental Of Joining
First Things! You need to create two SQL tables [table_1 as (Left Table) and table_2 as (Right Table)], because two tables are required for JOIN each other's in SQL JOIN.
The RIGHT JOIN used to returns all record from the RIGHT TABLE (table_2) no matter if they have matches in the LEFT TABLE (table_1), and also returns the matched records from the LEFT TABLE, if there is no matches are available in LEFT TABLE then the query returen result is NULL.
RIGHT JOIN Syntax
SELECT column_name(s)
FROM table_1
RIGHT JOIN table_2
ON table_1.column_name = table_2.column_name;
Example With Two Tables
Customers Table
Simple Customers record table & the name of the table is CUSTOMERS. The Column(s) name of the table like [ID, NAME, EMAIL, ADDRESS, and SALARY]:
ID |
NAME |
EMAIL |
ADDRESS |
SALARY |
1 |
Mark JK |
mark@gmail.com |
Mumbai |
5000 |
2 |
Nitika |
nitika@gmail.com |
Delhi |
6700 |
3 |
Suneel Kumar |
kumars@gmail.com |
Pune |
9400 |
4 |
Jenifer |
jenifer@gmail.com |
Indore |
3400 |
5 |
McKinley |
mckinley@gmail.com |
UK |
7200 |
6 |
Deepika |
deepika@gmail.com |
Nashik |
3600 |
Orders Table
This is a sample ORDER table; The columns of the table are [OID, DATE, CUSTOMER_ID, and AMOUNT]:
OID |
DATE |
CUSTOMER_ID |
AMOUNT |
210 |
12/01/2019 |
2 |
4000 |
209 |
11/22/2019 |
4 |
2000 |
211 |
12/10/2019 |
1 |
1000 |
212 |
12/17/2019 |
6 |
3000 |
Mapping Image Of RIGHT JOIN
JOIN Both Table Using RIGHT JOIN
SELECT ID, NAME, DATE, AMOUNT, EMAIL
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Result Look Like
ID |
NAME |
DATE |
AMOUNT |
EMAIL |
2 |
Nitika |
12/01/2019 |
4000 |
nitika@gmail.com |
4 |
Jenifer |
11/22/2019 |
2000 |
jenifer@gmail.com |
1 |
Mark JK |
12/10/2019 |
1000 |
mark@gmail.com |
6 |
Deepika |
12/17/2019 |
3000 |
deepika@gmail.com |
SQL RIGHT OUTER JOIN
A RIGHT OUTER JOIN used to return a result set that contains all rows in the RIGHT TABLE (table_2) that do not exist in the LEFT TABLE (table_1).
Mapping Image Of RIGHT OUTER JOIN
RIGHT OUTER JOIN Syntax
SELECT *
FROM table_1
RIGHT [OUTER] JOIN table_2
ON table_1.column_name = table_2.column_name;