SQL RIGHT JOIN: A Comprehensive Guide
SQL RIGHT JOIN
Introduction
In SQL, records from two tables can be combined using the RIGHT JOIN procedure. All of the records from the right table are returned when a RIGHT JOIN is used, together with the matching entries from the left table. If there are no matching records in the left table, NULL values are displayed for the missing data.
With the help of MySQL, we will examine the RIGHT JOIN function in SQL in this tutorial. Let’s begin by comprehending the RIGHT JOIN syntax.
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here
Syntax
SELECT Table1.column1, Table2.column2
FROM Table1
RIGHT JOIN Table2
ON Table1.common_column = Table2.common_column;
In this case, the left table is denoted by Table 1 and the right table by Table 2. All of the records from Table 2 are guaranteed to be included in the result set by the RIGHT JOIN.
Understanding RIGHT JOIN with Examples
Let’s use the following tables to illustrate the RIGHT JOIN operation:
Table 1: Employees
EmpID | Emp_Name | Emp_Salary |
---|---|---|
101 | Raj Malhotra | 75000 |
102 | Ankit Sharma | 62000 |
103 | Meena Patel | 48000 |
104 | Jatin Verma | 53000 |
105 | Priya Singh | 59000 |
Table 2: Departments
DeptID | Dept_Name | Emp_ID |
---|---|---|
1 | Finance | 101 |
2 | Marketing | 103 |
3 | Sales | 104 |
4 | HR | 105 |
5 | IT | 106 |
Example 1: RIGHT JOIN Employees and Departments
The following query is used to retrieve employee information along with their department:
SELECT e.EmpID, e.Emp_Name, e.Emp_Salary, d.DeptID, d.Dept_Name
FROM Employees e
RIGHT JOIN Departments d
ON e.EmpID = d.Emp_ID;
Output:
EmpID | Emp_Name | Emp_Salary | DeptID | Dept_Name |
---|---|---|---|---|
101 | Raj Malhotra | 75000 | 1 | Finance |
103 | Meena Patel | 48000 | 2 | Marketing |
104 | Jatin Verma | 53000 | 3 | Sales |
105 | Priya Singh | 59000 | 4 | HR |
NULL | NULL | NULL | 5 | IT |
Explanation:
- Every record is obtained from the Departments table.
- The only records shown are those from the Employees table that correspond to an Emp_ID in Departments.
- NULL values are displayed because the IT department does not have a corresponding employee.
Table 3: Loans
LoanID | Branch | Amount |
---|---|---|
501 | B1 | 20000 |
502 | B2 | 50000 |
503 | B3 | 12000 |
504 | B4 | 90000 |
505 | B5 | 15000 |
Table 4: Borrowers
CustID | Cust_Name | LoanID |
---|---|---|
1 | Riya Kapoor | 501 |
2 | Mohit Verma | 503 |
3 | Ishaan Gupta | 504 |
4 | Alisha Jain | 506 |
Example 2: RIGHT JOIN Loans and Borrowers
SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.Cust_Name
FROM Loans l
RIGHT JOIN Borrowers b
ON l.LoanID = b.LoanID;
Output:
LoanID | Branch | Amount | CustID | Cust_Name |
---|---|---|---|---|
501 | B1 | 20000 | 1 | Riya Kapoor |
503 | B3 | 12000 | 2 | Mohit Verma |
504 | B4 | 90000 | 3 | Ishaan Gupta |
NULL | NULL | NULL | 4 | Alisha Jain |
Explanation:
- All records from
Borrowers
are displayed. - Only matching
LoanID
values from theLoans
table are shown. - Since LoanID 506 has no match in
Loans
, NULL values appear.
Table 5: Customers
CustID | Cust_Name | Age | Salary |
---|---|---|---|
201 | Aditi Rana | 27 | 65000 |
202 | Rohit Mehta | 30 | 50000 |
203 | Neha Singh | 26 | 55000 |
204 | Kunal Shah | 29 | 47000 |
Table 6: Orders
OrderID | Order_Date | Cust_ID | Amount |
---|---|---|---|
301 | 2022-01-10 | 201 | 12000 |
302 | 2022-03-15 | 202 | 15000 |
303 | 2022-04-20 | 205 | 8000 |
Example 3: RIGHT JOIN Customers and Orders
SELECT c.CustID, c.Cust_Name, c.Age, c.Salary, o.OrderID, o.Order_Date, o.Amount
FROM Customers c
RIGHT JOIN Orders o
ON c.CustID = o.Cust_ID;
Output:
CustID | Cust_Name | Age | Salary | OrderID | Order_Date | Amount |
---|---|---|---|---|---|---|
201 | Aditi Rana | 27 | 65000 | 301 | 2022-01-10 | 12000 |
202 | Rohit Mehta | 30 | 50000 | 302 | 2022-03-15 | 15000 |
NULL | NULL | NULL | NULL | 303 | 2022-04-20 | 8000 |
Explanation:
- Included are all Orders records.
- Only customer records that match are displayed.
- NULL values are displayed because OrderID 303 does not have a matching Cust_ID in Customers.
Download New Real Time Projects :-Click here
Complete Advance AI topics:-Â CLICK HERE
Conclusion
While the left table only offers matching records, the RIGHT JOIN procedure guarantees that every record from the right table is shown. It is frequently used in database administration to preserve all of the data from the primary reference table while extracting insightful information.
Â
For more SQL tutorials, stay tuned to UpdateGadh!
SQL RIGHT JOIN
sql right join vs left join
sql right outer join
right join example
sql joins
left join in sql
full join in sql
inner join in sql
cross join in sql
sql
sql left join
sql right join example
sql right join w3schools
Post Comment