SQL OUTER JOIN

SQL OUTER JOIN: A Comprehensive Guide

SQL OUTER JOIN: A Comprehensive Guide

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

SQL OUTER JOIN

All of the data from both tables is combined in SQL using the OUTER JOIN. Whether the records from both tables match or not, the result includes both matching and non-matching records. Three kinds of outside joins are supported by SQL:

We shall examine several kinds of outer joins in this guide using examples from MySQL.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

Understanding Outer Joins with Sample Tables

We have example data in the following tables:

Table 1: Employee

EmployeeIDEmployee_NameEmployee_Salary
1Arun Tiwari50000
2Sachin Rathi64000
3Harshal Pathak48000
4Arjun Kuwar46000
5Sarthak Gada62000
6Saurabh Sheik53000
7Shubham Singh29000
8Shivam Dixit54000
9Vicky Gujral39000
10Vijay Bose28000

Table 2: Department

DepartmentIDDepartment_NameEmployee_ID
1Production1
2Sales3
3Marketing4
4Accounts5
5Development7
6HR9
7Sales10

See also  SQL INSERT Multiple Rows: A Time-Saving Technique

Table 3: Loan

LoanIDBranchAmount
1B115000
2B210000
3B320000
4B4100000
5B5150000
6B650000
7B735000
8B885000

Table 4: Borrower

CustIDCustNameLoanID
1Sonakshi Dixit1
2Shital Garg4
3Swara Joshi5
4Isha Deshmukh2
5Swati Bose7
6Asha Kapoor10
7Nandini Shah9

1. Left Outer Join

Only matched records from the right table are retrieved by a left outer join, which retrieves all records from the left table. NULL values are returned in the event that no match is discovered.

Syntax:

SELECT Table1.columnName1, Table2.columnName2
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ColumnName = Table2.ColumnName;

Example 1: Employee and Department Tables

SELECT e.EmployeeID, e.Employee_Name, e.Employee_Salary, d.DepartmentID, d.Department_Name
FROM employee e
LEFT OUTER JOIN department d
ON e.EmployeeID = d.Employee_ID;

Example 2: Loan and Borrower Tables

SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.CustName
FROM Loan l
LEFT OUTER JOIN Borrower b
ON l.LoanID = b.LoanID;

2. Right Outer Join

Only matched records from the left table are retrieved by a right outer join, which retrieves all entries from the right table. NULL values are returned in the event that no match is discovered.

See also  SQL DELETE VIEW

Syntax:

SELECT Table1.columnName1, Table2.columnName2
FROM Table1
RIGHT OUTER JOIN Table2
ON Table1.ColumnName = Table2.ColumnName;

Example 1: Employee and Department Tables

SELECT e.EmployeeID, e.Employee_Name, e.Employee_Salary, d.DepartmentID, d.Department_Name
FROM employee e
RIGHT OUTER JOIN department d
ON e.EmployeeID = d.Employee_ID;

Example 2: Loan and Borrower Tables

SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.CustName
FROM Loan l
RIGHT OUTER JOIN Borrower b
ON l.LoanID = b.LoanID;

3. Full Outer Join

All records, matching and non-matching, are retrieved from both tables using a full outer join. Although FULL OUTER JOIN is not natively supported by MySQL, it can be accomplished with UNION.

Workaround for FULL OUTER JOIN in MySQL:

SELECT Table1.columnName1, Table2.columnName2
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ColumnName = Table2.ColumnName
UNION
SELECT Table1.columnName1, Table2.columnName2
FROM Table1
RIGHT OUTER JOIN Table2
ON Table1.ColumnName = Table2.ColumnName;

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Conclusion

SQL OUTER JOINs facilitate the retrieval of data, including unmatched records, from several tables. Database administration requires an understanding of Left Outer Join, Right Outer Join, and workarounds for Full Outer Join in MySQL.

See also  SQL UPDATE with JOIN

Would you like to explore more SQL queries? Let us know in the comments!


sql left outer join
sql outer join
oracle sql outer join
proc sql outer join
my sql outer join
spark sql outer join
pl sql outer join
mysql outer join
sql outer join example
sql full outer join
sql joins
sql join types
right outer join
self join in sql
sql inner join
left outer join

🎓 Need Complete Final Year Project?

Get Source Code + Report + PPT + Viva Questions (Instant Access)

🛒 Visit UpdateGadh Store →
💬 Chat Now