Open In App

SQL Full Outer Join Using Where Clause

Last Updated : 30 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

SQL (Structured Query Language) provides powerful tools to manage and query relational databases. One of its most flexible features is the JOIN operation, which allows combining data from two or more tables based on related columns. Among the various types of joins, the FULL OUTER JOIN is particularly useful for retrieving all rows from both tables, including unmatched rows, with NULL values filling in the gaps.

In this comprehensive guide, we will explain the concept of FULL OUTER JOIN in SQL, its syntax, and the use of the WHERE clause to filter results effectively. This article is designed to provide clear explanations and practical examples to help us master this important SQL feature.

What is a FULL OUTER JOIN in SQL?

Full Join provides result with concatenation of LEFT JOIN and RIGHT JOIN. The result will contain all the rows from both Table 1 and Table 2. It returns all rows from both tables, with NULL values in place where there is no match. This is useful for scenarios where we want a complete overview of all data, regardless of matches.

Syntax:

SELECT * FROM Table1
FULL OUTER JOIN Table2
ON Table1.column_match=Table2.column_match;

Key Terms:

  • Table 1: First Table in Database.
  • Table 2: Second Table in Database.
  • column_match: The column common to both the tables.

Examples of FULL OUTER JOIN

We have considered a Customer and Purchase Information of Mobile Phones from an E-Commerce site during Big Billion Days. The Database E-Commerce has two tables one has information about the Product and the other one have information about the Customer. Now, we will perform FULL OUTER JOIN between these two tables to concatenate them into a single table and get complete data about the customers and the products they  purchased from the site.

Table 1PURCHASE INFORMATION

Purchase_Information

Purchase_Information

Table 2: CUSTOMER INFORMATION

Customer_Information

Customer_Information

Query:

SELECT *
FROM PURCHASE_INFORMATION
FULL OUTER JOIN CUSTOMER_INFORMATION
ON PURCHASE_INFORMATION.Customer_Name = CUSTOMER_INFORMATION.Customer_Name;

Output:

Full-Outer-Join-

Full Outer Join

FULL OUTER JOIN using WHERE CLAUSE

Adding a WHERE clause to a FULL OUTER JOIN allows us to filter the results based on specific conditions. For example, we can retrieve only those rows where no match was found in either table, ensuring we focus on unmatched records. This is particularly useful for identifying discrepancies or gaps between datasets.

Query:

SELECT *
FROM PURCHASE_INFORMATION
FULL OUTER JOIN CUSTOMER_INFORMATION
ON PURCHASE_INFORMATION.Customer_Name = CUSTOMER_INFORMATION.Customer_Name
WHERE PURCHASE_INFORMATION.Customer_Name IS NULL
OR CUSTOMER_INFORMATION.Customer_Name IS NULL;

Output:

Product_ID Mobile_Brand Cost (INR) Customer_Name Customer_ID E_Mail Address
1 OnePlus Nord 5G 30,000 Rishabh NULL NULL
4 Samsung Galaxy S20 55,000 Harsh NULL NULL
5 Realme X50 Pro 40,000 Manjari NULL NULL
NULL NULL NULL Rajdeep 2 [email protected]
NULL NULL NULL Pooja 4 [email protected]

Explanation:

The above Query returns only those customer who bought mobile phones and don’t have any record saved in Customer Information Table as well as the customer information who didn’t buy any product.

Using FULL OUTER JOIN with AND Condition

An AND condition in the ON clause ensures that multiple conditions must be true simultaneously for rows to match. If either condition is not satisfied, the rows from both tables will still appear in the result but with NULL values for the unmatched columns. This approach is useful for performing more granular matches while still preserving unmatched data for analysis.

Query:

SELECT *FROM Table1
FULL OUTER JOIN Table2
ON Table1.column1 = Table2.column1
AND Table1.column2 = Table2.column2;

Conclusion

The FULL OUTER JOIN in SQL is a robust tool for combining datasets and ensuring no data is left out. By incorporating the WHERE clause, we can refine the results to focus on unmatched records, providing deeper insights. Mastering FULL OUTER JOIN and its variations will significantly enhance our SQL skills and help in building more comprehensive database queries.



Next Article
Article Tags :

Similar Reads