DB2

OUTER JOIN – step by step walkthrough with examples

The SQL JOIN operation enables combining rows from two or more tables based on a related column. Among these, the OUTER JOIN category includes LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. These joins allow retrieving unmatched rows, which INNER JOIN discards when no match is found.

When using OUTER JOINS, rows from one or both tables are returned even if there is no corresponding match in the other. This is particularly useful for identifying missing relationships and for performing comprehensive data analysis where completeness is key.

Key Benefit: OUTER JOIN returns all rows from at least one table, ensuring that even records with no match are not excluded from the result set. This makes it valuable for reporting, auditing, and full-spectrum data views.

Types of SQL JOINs

  • INNER JOIN: Returns only rows with matching values in both tables.
  • LEFT OUTER JOIN: Returns all rows from the left table, and the matched rows from the right table. NULL where no match exists.
  • RIGHT OUTER JOIN: Returns all rows from the right table, and the matched rows from the left table. NULL where no match exists.
  • FULL OUTER JOIN: Returns rows when there is a match in either table, including unmatched rows from both sides with NULLs where appropriate.

Inner Join vs Outer Join

Unlike INNER JOIN, which excludes unmatched rows, OUTER JOIN ensures all relevant data is included. It returns every row from one or both tables, even if no match exists. This helps provide a full data picture, especially when analyzing optional relationships or missing data scenarios.

Example SQL OUTER JOIN Syntax

SELECT ColumnList
FROM EmployeeName AS L
LEFT/RIGHT/FULL OUTER JOIN Position AS R
ON L.Column = R.Column;

Understanding the Output

  • Returns all matching rows as per the join condition defined using the ON clause.
  • Includes non-matching rows from the specified table(s) with NULLs filled in for missing values.
  • Enables combining partial datasets for unified reporting.

Features of JOINS

Feature Inner Join Left Outer Join / Right Outer Join / Full Outer Join
Purpose Returns rows where the join condition is met in both tables. Left Outer Join: Returns all rows from the left table, even if there’s no match in the right table.
Right Outer Join: Returns all rows from the right table, even if there’s no match in the left table.
Full Outer Join: Returns all rows from both tables, regardless of matches.
Join Condition WHERE clause ON clause
Matching Rows Only rows with matching values in join columns from both tables are returned. Left Outer Join: Includes all left table rows. If no match in the right, NULLs are shown.
Right Outer Join: Includes all right table rows. If no match in the left, NULLs are shown.
Full Outer Join: Includes all rows from both tables. Unmatched rows have NULLs in non-matching columns.
Result Size Usually the smallest subset of matching rows from both tables. Left Outer Join: Same as left table.
Right Outer Join: Same as right table.
Full Outer Join: Potentially sum of both tables.
Example SELECT * FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;
Left Outer Join:
SELECT * FROM Customers LEFT OUTER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;


Right Outer Join:
SELECT * FROM Customers RIGHT OUTER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;


Full Outer Join:
SELECT * FROM Customers FULL OUTER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;

Additional Notes:

    • Inner joins are the most common type of join and are used when you only want to include rows where there is a match in both tables.
    • Left outer joins are useful when you want to see all the data from the left table, even if there is no matching row in the right table.
    • Right outer joins are useful when you want to see all the data from the right table, even if there is no matching row in the left table.
    • Full outer joins are useful when you want to see all the data from both tables, regardless of whether there is a matching row in the other table.

    Optimize Outer Join Queries in the COBOL DB2 Program – Click Here

    OUTER JOIN Queries Common Errors and Resolutions – Click Here

    At its core, a left outer join is a method for combining rows from two or more tables based on a related column between them. Unlike inner joins, which only return rows that have matching values in both tables, a left outer join returns all rows from the left table (referred to as the “left” or “first” table) and matching rows from the right table (referred to as the “right” or “second” table). If there is no match in the right table, NULL values are returned for the columns from the right table.

    SELECT column_names
    FROM table1
    LEFT OUTER JOIN table2
    ON table1.column_name = table2.column_name;
    Input Data
     EmployeeName Table-(T1)Position Table-(T2)         
      ID     NAME           ID     TITLE         TEAMSIZE    
      10     Sandy          20     Sales Mgr        5
      20     Sam            30     Clerk           10 
      30     Cindy          30     Manager          2
                            40     Sales Rep        7
                            50     Sr. Manager.    11
     
     Query
     SELECT T1.NAME, 
            T1.ID, 
            T2.ID, 
            T2.TITLE,
            T2.TEAMSIZE
     FROM EmployeeName T1
     LEFT OUTER JOIN Position T2 
     ON T1.ID = T2.ID
     ORDER BY T1.ID
            , T2.TITLE; 
    
     Result         
     ID   NAME     ID     TITLE        TEAMSIZE
     10   Sandy    --     ---------    ---------
     20   Sam      20     Sales Mgr     5
     30   Cindy    30     Clerk        10
     30   Cindy    30     Manager.      2 

    Suppose we want to return the employee with no title. To do that, add a WHERE clause to include only rows with nulls from the Position table.

    Query using ON or WHERE

     SELECT T1.NAME, 
            T1.ID, 
            T2.ID, 
            T2.TITLE,
            T2.TEAMSIZE
     FROM EmployeeName T1
     LEFT OUTER JOIN Position T2 
     ON T1.ID = T2.ID
     AND T2.TITLE IS NULL
     
     SELECTT1.NAME, 
            T1.ID, 
            T2.ID, 
            T2.TITLE,
            T2.TEAMSIZE
     FROMEmployeeName T1
     LEFT OUTER JOINPosition T2 
     ONT1.ID=T2.ID
     WHERE T2.TITLE IS NULL
     
    Both the above queries will give the same result.
    Result         
     ID   NAME     ID     TITLE       TEAMSIZE
     10   Sandy    --     ---------   --------- 

    Right Outer Join

    A right outer join, also known as a right join, merges rows from two or more tables based on a related column between them. Unlike inner joins, right joins return all rows from the right table and only matching rows from the left table. If there is no match in the left table, NULL values are returned for those columns.

    SQL Syntax Example
    SELECT column_names
    FROM table1
    RIGHT OUTER JOIN table2
    ON table1.column_name = table2.column_name;
    
    Sample Tables
    EmployeeName Table (T1)           Position Table (T2)
    ID    NAME             |     ID    TITLE           TEAM
    10    Sandy            |     20    Sales Mgr       5
    20    Sam              |     30    Clerk           10
    30    Cindy            |     30    Manager         2
                          |     40    Sales Rep       7
                          |     50    Sr. Manager     11
    
    Query
    SELECT T1.NAME,  
           T1.ID,  
           T2.ID,  
           T2.TITLE
    FROM EmployeeName T1
    RIGHT OUTER JOIN Position T2  
    ON T1.ID = T2.ID
    ORDER BY T1.ID, T2.TITLE;
    
    Result
    ID    NAME     ID    TITLE
    20    Sam      20    Sales Mgr
    30    Cindy    30    Clerk
    30    Cindy    30    Manager
    --    -----    40    Sales Rep
    --    -----    50    Sr. Manager
    

    Suppose we want to return only the employees with no title. To achieve this, we can add a WHERE clause to filter only NULLs from the EmployeeName table.

    Query using ON or WHERE
    -- Using AND in ON clause
    SELECT T1.NAME,  
           T1.ID,  
           T2.ID,  
           T2.TITLE
    FROM EmployeeName T1
    RIGHT OUTER JOIN Position T2  
    ON T1.ID = T2.ID
    AND T1.NAME IS NULL;
    
    -- Using WHERE clause
    SELECT T1.NAME,  
           T1.ID,  
           T2.ID,  
           T2.TITLE
    FROM EmployeeName T1
    LEFT OUTER JOIN Position T2  
    ON T1.ID = T2.ID
    WHERE T1.NAME IS NULL;
    
    Result
    ID    NAME     ID    TITLE
    --    -----    40    Sales Rep
    --    -----    50    Sr. Manager
    

    Full Outer Join

    A full outer join, also known simply as a full join, merges rows from two tables based on a related column. Unlike inner joins, it returns all rows from both tables. Inner joins only return matching rows, while outer joins (left and right) include all rows from one table and matching ones from the other. A full outer join returns NULL values where no match is found in either table.

    
    SELECT column_names
    FROM table1
    FULL OUTER JOIN table2
    ON table1.column_name = table2.column_name;
    
    Input Data
    EmployeeName Table-(T1)       Position Table-(T2)       
    ID   NAME             ID   TITLE         TEAM
    10   Sandy            20   Sales Mgr     5
    20   Sam              30   Clerk         10
    30   Cindy            30   Manager       2
                         40   Sales Rep     7
                         50   Sr. Manager   11
    
    Query
    SELECT T1.NAME,
           T1.ID,
           T2.ID,
           T2.TITLE
    FROM EmployeeName T1
    FULL OUTER JOIN Position T2
    ON T1.ID = T2.ID
    ORDER BY T1.ID,
             T2.ID,
             T2.TITLE;
    
    Result
    ID   NAME     ID   TITLE
    10   Sandy    --   ---------
    20   Sam      20   Sales Mgr
    30   Cindy    30   Clerk
    30   Cindy    30   Manager
    --   -----    40   Sales Rep
    --   -----    50   Manager
    

    Suppose we want to return only employees with ID < 30. To do that, we can add a WHERE clause to filter rows from the EmployeeName table.

    Query using ON or WHERE
    
    SELECT T1.NAME,
           T1.ID,
           T2.ID,
           T2.TITLE
    FROM EmployeeName T1
    FULL OUTER JOIN Position T2
    ON T1.ID = T2.ID
    AND T1.ID < 30
    ORDER BY T1.ID,
             T2.ID,
             T2.TITLE;
    
    Result
    ID   NAME     ID   TITLE
    10   Sandy    --   ---------
    20   Sam      20   Sales Mgr
    

    In this example, the condition T1.ID < 30 is used within the JOIN clause. It does not eliminate rows but limits which records are considered matching.

    
    SELECT T1.NAME,
           T1.ID,
           T2.ID,
           T2.TITLE
    FROM EmployeeName T1
    FULL OUTER JOIN Position T2
    ON T1.ID = T2.ID
    WHERE T1.ID < 30
    ORDER BY T1.ID,
             T2.ID,
             T2.TITLE;
    
    Result
    ID   NAME     ID   TITLE
    10   Sandy    --   ---------
    20   Sam      20   Sales Mgr
    30   Cindy    --   ---------
    --   -----    30   Clerk
    --   -----    30   Manager
    --   -----    40   Sales Rep
    --   -----    50   Manager
    

    Performance Considerations

    OUTER JOINs are generally slower than INNER JOINs because they need to process more rows. An INNER JOIN eliminates non-matching rows early, while OUTER JOINs must preserve all rows from at least one table, resulting in larger result sets to process.

    Optimization Tips

    1. Use proper indexing on join columns
    2. Filter early using WHERE clauses when possible
    3. Consider alternatives like UNION operations for specific scenarios
    4. Avoid FULL OUTER JOINs on very large tables without proper indexing

    Example of alternative approach using UNION:

    
    -- Instead of FULL OUTER JOIN, sometimes UNION is faster
    SELECT c.customer_id, c.first_name, o.order_id, o.product
    FROM Customers c
    LEFT JOIN Orders o ON c.customer_id = o.customer_id
    UNION
    SELECT c.customer_id, c.first_name, o.order_id, o.product
    FROM Customers c
    RIGHT JOIN Orders o ON c.customer_id = o.customer_id
    WHERE c.customer_id IS NULL;
      
    Common Pitfalls and Best Practices

    Handling NULL Values

    
    -- Use COALESCE or ISNULL to handle NULLs
    SELECT c.first_name, 
           COALESCE(o.product, 'No orders') as product_info
    FROM Customers c
    LEFT OUTER JOIN Orders o ON c.customer_id = o.customer_id;
      

    Understanding the Difference

    • LEFT JOIN: Use when you need all records from the primary table
    • RIGHT JOIN: Use when you need all records from the secondary table (less common)
    • FULL OUTER JOIN: Use when you need complete data from both tables

    Performance Monitoring

    FULL OUTER JOINs can potentially return very large result sets since they include all rows from both tables. Monitor performance and consider alternatives for large datasets.

    When to Use Each Type

    Use LEFT OUTER JOIN when:
    • You have a primary dataset that must be complete (like all customers)
    • You want to include optional related data (like orders)
    • You need to identify missing relationships (customers without orders)
    Use RIGHT OUTER JOIN when:
    • You need all records from the secondary table
    • You’re building queries where changing table order would be complex
    • You want to find orphaned records in the right table
    Use FULL OUTER JOIN when:
    • You need a complete dataset from both tables
    • You’re performing data comparison or validation
    • You want to identify all unmatched records from both sides
    • You’re merging datasets from different sources

    Summary

    OUTER JOINs are essential tools for comprehensive data analysis, providing the ability to preserve all data even when relationships are incomplete. LEFT OUTER JOIN is the most commonly used for including all records from a primary table, while FULL OUTER JOIN provides the most complete view by including all records from both tables.

    Understanding when and how to use each type of OUTER JOIN will help you write more effective SQL queries, perform better data analysis, and ensure you don’t miss important information in your database operations. Remember to consider performance implications and always test your queries with representative data volumes to ensure optimal performance.

    Additional Notes on OUTER JOIN

    • It can return both inner rows and outer rows. Inner rows are similar to INNER JOIN results. Outer rows are unmatched values from one side with NULLs from the other.
    • Types include LEFT, RIGHT, and FULL OUTER JOINs — examples are provided for each.
    • Outer rows are useful in identifying incomplete or orphaned data, merging datasets, and more.

    In summary, LEFT OUTER JOIN retrieves unmatched rows from the left table. RIGHT OUTER JOIN retrieves unmatched rows from the right table. FULL OUTER JOIN retrieves unmatched rows from both tables. These operations enhance your ability to analyze data relationships and handle scenarios where not every record has a match in the related table.

    INNER JOIN: Click Here IBM DB2 Manual: Click Here

    Admin

    Share
    Published by
    Admin

    Recent Posts

    AI Product Owner : Use Cases with Examples and Tools

    The Product Owner role has shifted from just being a requirements proxy to a strategic,…

    7 months ago

    Business Value: Crafting User Stories for Measurable Impact

    Business Value: In the world of Agile development, the user story has long been the…

    8 months ago

    SAFe Scrum Master (SSM) Certification with FAQ and Tips

    The SAFe Scrum Master certification has become one of the most sought-after credentials for Agile…

    12 months ago

    PSK Practice Exam Practice Mode Questions

    The Professional Scrum with Kanban (PSK) course enhances your organization's ability to deliver value efficiently…

    1 year ago

    Effective User Interviews in Scrum Framework

    Effective User interviews play a crucial role in Scrum methodology, helping Product Owners and Scrum…

    1 year ago

    User Research Tools and Techniques for Product Owners

    Product Owners should be well-versed in various user research tools and techniques to effectively understand…

    1 year ago