Inner Join Icon

Inner Join

Left Join Icon

Left Join

Right Outer Join Icon

Right Outer Join

Full Outer Join Icon

Full Outer Join

Cross Join

Cross Join

Self Join Icon

Self Join

Anti Join Icon

Anti Join

Full Outer Join

Left Join Icon

A FULL OUTER JOIN

returns all records from both tables, matching rows where possible. If there’s no match, the result will show NULL for the columns from the missing table. This join ensures that no data is excluded, even if relationships between the tables are incomplete.

Task

Retrieve a comprehensive list of all customers, their orders, and any associated payments. Even if some customers have not placed orders or some orders have no payments, they should still appear in the result with NULL values where data is missing.


Tables: Customers, Orders, and Payments

CustomerIDCustomerName
1Angel
2Skyler
3Jamie
4Riley
5Morgan
OrderIDCustomerIDAmount
1011150
1022200
1031350
1043400
10599500
PaymentIDCustomerIDAmount
201101150
202102200
203103300

How Relationships Work in This Example

Customers Table (Left Table):

  • Each customer has a unique CustomerID. This table contains the primary data for customers, including those who may not have placed any orders.
  • Customers without orders appear in the result with NULL in the order and payment fields, showing that they have no matching records.

Orders Table (Middle Table):

  • Each order is linked to a customer through CustomerID and may have a corresponding payment through OrderID.
  • Orders without matching customers show NULL in the customer fields, and those without matching payments show NULL in the payment fields.

Payments Table (Right Table):

  • Each payment is associated with an order through OrderID.
  • Payments with no matching orders display NULL for both customer and order details, indicating orphaned payments.

SQL Query for Advanced Full Outer Join:

         
    SELECT
        Customers.CustomerName,
        Orders.OrderID,
        Orders.Amount AS OrderAmount,
        Payments.Amount AS PaymentAmount
    FROM
        Customers
    FULL OUTER JOIN 
        Orders ON Customers.CustomerID = Orders.CustomerID;
    FULL OUTER JOIN 
        Payments ON Orders.OrderID = Payments.OrderID;
                

Why This Query is Useful

  • Complete Data Visibility: It includes all customers, all orders, and all payments, helping identify any unmatched records (e.g., customers without orders or orders without payments).
  • Detecting Data Gaps: By showing NULL values for missing relationships, it highlights areas where data may be incomplete, such as orphaned orders or unpaid transactions.
  • Supports Data Integrity and Audits: This query is ideal for auditing or data validation, ensuring that every piece of data is accounted for and flagging potential inconsistencies across tables.
Result Table

Query Output: Complete Customer, Order, and Payment Details

CustomerName OrderID OrderAmount PaymentAmount
Angel 101 150 150
Angel 103 350 NULL
Skyler 102 200 200
Jamie 104 400 NULL
Riley NULL NULL NULL
Morgan NULL NULL NULL
NULL 105 500 NULL
NULL NULL NULL 300

Why are there NULL values in the CustomerName, OrderID, or PaymentAmount columns?

  • Unmatched Records: Understanding that NULL appears when there is no matching data in one of the tables. For example, an order with a non-existent CustomerID results in NULL for the customer fields.
  • Data Completeness: Recognizing that NULL values in a FULL OUTER JOIN don’t indicate errors but rather show that some relationships are missing.
  • How to Interpret NULL Values: Knowing that NULL highlights records that lack corresponding information, such as customers without orders or orders without payments.

Explanation

This setup allows us to see:

  • Customers without orders (like Morgan and Riley) with NULL for order and payment columns.
  • Orders without customers (Order 105) with NULL in the CustomerName column.
  • Payments without matching orders (Payment 203) with NULL for both customer and order details.

When to Use Full Outer Join

A FULL OUTER JOIN is useful when you need a complete view of data from both tables, regardless of whether all records have matching counterparts. This join is ideal for scenarios where some relationships may be incomplete, but it’s essential to retain all data from both sources, such as tracking all customers and all orders, even if some customers haven’t placed orders or some orders have no assigned customers. FULL OUTER JOIN helps identify gaps in data, such as orphaned records (records with no match in the other table), making it valuable for auditing, data validation, and reporting to ensure that nothing is excluded from the analysis.

Conclusion

A FULL OUTER JOIN allows for a comprehensive dataset by including all records from both tables, filling in NULL values where there are no matches. This join type is invaluable for scenarios requiring a complete view of all data, even if relationships are incomplete. By capturing unmatched records, a FULL OUTER JOIN is essential for tasks like data audits, integrity checks, and in-depth reporting, ensuring that no data is left out of the analysis.