Introduction to MySQL Outer Join
MySQL Outer Join is considered to be three types: –
- FULL OUTER Join
- LEFT OUTER Join – same as left join.
- RIGHT OUTER Join – same as right join.
FULL OUTER JOIN
Full Join gets all the rows from both tables. In this section, let’s discuss more FULL join which is used mostly.
Example:
Below represents the Venn diagram of the FULL join.
Syntax:
SELECT * FROM TABLE_A A
FULL OUTER JOIN TABLE_B B
ON A. Common_COLUMN =B. Common_COLUMN
WHERE <Condition>
The result set contains NULL set values. Below syntax can be used to neglect the NULL values: –
SELECT * FROM TABLE_A A
FULL OUTER JOIN TABLE B B
ON A. Common_COLUMN =B. Common_COLUMN
WHERE A.Common_COLUMN IS NULL
AND A.Common_COLUMN IS NULL
Key Differences of Full Outer Join vs Right Join
Let us discuss the main differences of Full Outer Join and Right Join.
FULL OUTER JOIN | RIGHT JOIN |
Consider all the rows on both the table. | Consider all rows from the right table and common from both tables. |
All rows from both tables | INNER Join + all rows from the right table |
Joins based on a condition | Joins based on a condition |
ON keyword is used to specify the condition and join the tables. | ON keyword is used to specify the condition and join the tables. |
The result set contains NULL set values. Below syntax can be used to neglect the NULL values: –
SELECT * FROM TABLE_A A FULL OUTER JOIN TABLE B B ON A. Common_COLUMN =B. Common_COLUMN WHERE B.Common_COLUMN IS NULL |
The result set contains NULL set values. Below syntax can be used to neglect the NULL values: –
SELECT * FROM TABLE_A A RIGHT JOIN TABLE B B ON A. Common_COLUMN =B. Common_COLUMN WHERE A.Common_COLUMN IS NULL |
SELECT * FROM TABLE_A
FULL OUTER JOIN TABLE B ON A. Common_COLUMN =B. Common_COLUMN |
SELECT * FROM TABLE_A
RIGHT JOIN TABLE B ON A. Common_COLUMN =B. Common_COLUMN |
How to use FULL Outer Join in MySQL?
- FULL Outer Join = All rows from both tables
- Consider all rows from both tables. Unmatched rows get null values
- Joins based on a condition
- ON keyword is used to specify the condition and join the tables.
Examples
Here are the following examples mention below
Example #1
Let us consider two tables and apply FULL outer join on the tables:
Loan Table:
Borrower Table:
Query to get the loan_no, status, and borrower date from two tables.
Query:
SELECT L.LOAN_NO, L.LOAN_STATUS, B.BORROWER_DATE
FROM LOAN L FULL OUTER JOIN BORROWER B
ON L.LOAN_NO=B.LOAN_NO
Let’s check the output of the above table after applying the right join on them.
Output:
The result set contains NULL set values.
Below syntax can be used to neglect the NULL values: –
SELECT * FROM TABLE_A A
FULL OUTER JOIN TABLE B B
ON A. Common_COLUMN =B. Common_COLUMN
WHERE A.Common_COLUMN IS NULL
Let us consider two tables and apply FULL Outer join on the tables:
Query to get the loan_no, status, loan_aount and borrower date from two tables.
Query:
SELECT L.LOAN_NO, L.LOAN_STATUS,L.LOAN_AMOUNT, B.BORROWER_DATE
FROM LOAN L FULL OUTER JOIN BORROWER B
ON L.LOAN_NO=B.LOAN_NO
Let’s check the output of the above table after applying the Full outer join on them.
Output:
In the above table, LOAN is the right table and the Borrower is the left table. As in FULL OUTER join, we get all rows from both tables. Here we get all the rows from the LOAN table and the Borrower table. Values not present will be NULL.
Example #2
Let us consider two tables and apply FULL OUTER join on the tables:
Query to get the loan_no, status, loan_aount and borrower date from two tables.
Query:
SELECT L.LOAN_NO, L.LOAN_STATUS,L.LOAN_AMOUNT, B.BANK_ID
FROM BORROWER B FULL OUTER JOIN LOAN L
ON L.LOAN_NO=B.LOAN_NO
Let’s check the output of the above table after applying the FULL OUTER join on them.
Output:
In the above table, LOAN is the right table and the Borrower is the left table. As in FULL OUTER join, we get all rows from both tables. Here we get all the rows from the LOAN table and the Borrower table. Values not present will be NULL.
Conclusion
To fetch data relevant to the customer requirement we might need to join tables which will be fulfilled by joins. As mentioned earlier joins are used to get data from more than one table. To join more than one table we need at least one column common in both tables. Tables get joined based on the condition specified.
Recommended Articles
This is a guide to MySQL Outer Join. Here we discuss how to use FULL Outer Join in MySQL along with the key differences between full outer join vs right join and examples. You may also have a look at the following articles to learn more –
- Table in MySQL
- MySQL Aggregate Function
- LEFT OUTER JOIN in PostgreSQL | Examples
- LEFT OUTER JOIN in SQL | How to Works?
16 Online Courses | 11 Hands-on Projects | 70+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses