SQL Full Outer Join Using Where Clause
Last Updated :
30 Dec, 2024
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 1: PURCHASE INFORMATION

Purchase_Information
Table 2: 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 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.
Similar Reads
SQL Full Outer Join Using Union Clause
In this article, we will discuss the overview of SQL, and our main focus will be on how to perform Full Outer Join Using Union Clause in SQL. Let's discuss it one by one. Overview :To manage a relational database, SQL is a Structured Query Language to perform operations like creating, maintaining da
3 min read
INNER JOIN ON vs WHERE clause in SQL Server
In SQL Server, joining tables and filtering data are essential for retrieving meaningful information. The INNER JOIN operation is used to combine rows from multiple tables based on a matching condition, while the WHERE clause allows for further filtering of results. In this article, we will PL/SQL S
7 min read
FULL OUTER JOIN in SQLite
In the area of data querying and manipulation, the ability to combine information from different sources is important. SQLite, a popular embedded database engine, offers a range of join operations to fast process. One such operation FULL OUTER JOIN is particularly powerful as it allows us to merge d
4 min read
Having vs Where Clause in SQL
In SQL, filtering data is important for extracting meaningful insights from large datasets. While both the WHERE and HAVING clauses allow us to filter data, they serve distinct purposes and operate at different stages of the query execution process. Understanding the difference between these clauses
4 min read
Where clause in MS SQL Server
In this article, where clause will be discussed alongside example. Introduction : To extract the data at times, we need a particular conditions to satisfy. 'where' is a clause used to write the condition in the query. Syntax : select select_list from table_name where condition A example is given bel
1 min read
INNER JOIN ON vs WHERE clause in MySQL
When working with MySQL queries that involve multiple tables, understanding how to effectively use INNER JOIN ON versus the WHERE clause can significantly impact query performance and clarity. These two SQL constructs serve distinct purposes in combining data from different tables based on specific
5 min read
PL/SQL WHERE Clause
The WHERE clause in PL/SQL is essential for filtering records based on specified conditions. It is used in SELECT, UPDATE, and DELETE statements to limit the rows affected or retrieved, allowing precise control over data manipulation and retrieval. In this article, We will learn about the WHERE Clau
4 min read
SQL Server FULL OUTER JOIN
Joins in SQL are used to retrieve data from multiple tables based on a related column (or common column) between them. In this article, we will learn how to use FULL OUTER JOIN, which returns all rows from both tables being joined. It combines the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN
6 min read
PostgreSQL - FULL OUTER JOIN
In PostgreSQL, the FULL OUTER JOIN is a powerful feature that combines the effects of both LEFT JOIN and RIGHT JOIN. This join operation retrieves all rows from both tables involved in the join, including unmatched rows from each table. For any unmatched rows, PostgreSQL fills the result with NULL v
4 min read
SQL | Sub queries in From Clause
From clause can be used to specify a sub-query expression in SQL. The relation produced by the sub-query is then used as a new relation on which the outer query is applied. Sub queries in the from clause are supported by most of the SQL implementations.The correlation variables from the relations in
2 min read