SQL FULL JOIN

SQL FULL JOIN – A Complete Guide

SQL FULL JOIN

SQL FULL JOIN, also known as SQL FULL OUTER JOIN, is a powerful feature that combines the results of both LEFT JOIN and RIGHT JOIN. This means that the result includes all records from both tables, regardless of whether there is a match or not. If a match is found, the corresponding values are displayed. If there is no match, NULL values are inserted in the unmatched columns.

A FULL JOIN is useful when you need a complete dataset from two tables, ensuring that no data is lost due to missing relationships.

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

SQL FULL OUTER JOIN Syntax

To use a FULL OUTER JOIN, follow this SQL syntax:

SELECT *  
FROM table1  
FULL OUTER JOIN table2  
ON table1.column_name = table2.column_name;

Explanation:

  • table1 and table2 are the tables being joined.
  • column_name is the column used to establish the relationship between the two tables.
  • The result will include all records from both tables, filling unmatched columns with NULL values.

Example of SQL FULL OUTER JOIN

Let’s take two tables, Products and Orders, to demonstrate how FULL OUTER JOIN works.

Table: Products

Product_ID Product_Name
101 Laptop
102 Smartphone
103 Tablet
104 Monitor

Table: Orders

Order_ID Product_ID
201 102
202 103
203 105
204 106

Now, let’s execute a FULL OUTER JOIN on these tables based on the Product_ID column:

SELECT Products.Product_ID, Products.Product_Name, Orders.Order_ID  
FROM Products  
FULL OUTER JOIN Orders  
ON Products.Product_ID = Orders.Product_ID;

Resulting Table:

Product_ID Product_Name Order_ID
101 Laptop NULL
102 Smartphone 201
103 Tablet 202
104 Monitor NULL
NULL NULL 203
NULL NULL 204

Understanding the Result:

  • Rows with matching Product_ID values from both tables are combined.
  • If a product has no matching order, NULL is placed in the Order_ID column.
  • If an order does not match any product, NULL appears in the Product_ID and Product_Name columns.

Why Use FULL OUTER JOIN?

Using FULL JOIN is ideal when:

  1. You want to retain all records from both tables.
  2. You need to analyze data that may have missing relationships.
  3. You are working with datasets where a complete view is necessary for reporting or analysis.

Pictorial Representation:

        +-------------+              +-------------+
        |  Products   |              |   Orders    |
        +-------------+              +-------------+
                |                          |
                |                          |
                |        FULL JOIN         |
                +--------------------------+

This ensures that all records from both tables are part of the final output, even if they don’t have matching pairs.

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

Conclusion

SQL FULL OUTER JOIN is a powerful tool when working with relational databases. It ensures that no data is excluded, even when there is no match. This makes it highly useful for comprehensive reporting and data analysis.

Understanding SQL JOINs, especially FULL OUTER JOIN, allows developers and database administrators to make informed decisions when querying data.

For more SQL tutorials and database concepts, keep visiting UpdateGadh for the latest insights!


mysql full join
cross join in sql
self join in sql
sql joins
SQL FULL JOIN
sql full join vs full outer join
sql outer join
inner join in sql
sql join types
sql full form
sql full join full example
sql full join example
sql full join w3schools

Post Comment