Inner Join Vs Outer Join

 

INNER JOIN

An INNER JOIN is the basic and most commonly used form of join in relational databases. It combines tuples from two relations only when they satisfy the join condition. Tuples that do not have matching values in the other relation are eliminated from the result.

Key Characteristics of INNER JOIN

  • Only matching tuples from both relations appear in the result

  • Tuples with no matching counterpart are discarded

  • Tuples with NULL values in join attributes do not participate in the join

  • All JOINs discussed in previous sessions (theta join, equijoin, natural join) are inner joins

Conceptual Definition

For relations R and S, an inner join produces tuples where:

  • A tuple from R matches a tuple from S

  • The specified join condition evaluates to TRUE

If no such match exists, the tuple is not included.

Example (Conceptual)

Suppose we join DEPARTMENT and EMPLOYEE to find department managers:

DEPT_MGR ← DEPARTMENT Mgr_ssn = Ssn EMPLOYEE

Only departments that have a valid manager (matching employee) appear in the result. If a department has no manager assigned, it is excluded.

TEMP ← EMPLOYEE ⟕ Ssn = Mgr_ssn DEPARTMENT
RESULT ← π Fname, Minit, Lname, Dname (TEMP)



Information Loss in INNER JOIN

Because nonmatching tuples are discarded, inner joins may cause loss of information. This is acceptable when we are interested only in related data, but problematic when we want to preserve all tuples from one or both relations.


OUTER JOIN

An OUTER JOIN is an extension of the join operation designed to preserve tuples that do not have matching counterparts in the other relation. Instead of discarding such tuples, outer joins pad the missing attribute values with NULLs.

Outer joins are especially useful in business and reporting applications, where complete data is required even when some relationships are missing.

Motivation for OUTER JOIN

  • Prevent loss of information

  • Show all tuples from one or both relations

  • Explicitly represent missing relationships using NULL values


Types of OUTER JOIN

1. LEFT OUTER JOIN

A LEFT OUTER JOIN preserves all tuples from the left relation (R).

  • Matching tuples from the right relation (S) are included

  • If no match exists, attributes of S are filled with NULLs

Definition

R ⟕ S

Example

Retrieve all employees and the departments they manage (if any):

TEMP ← EMPLOYEE Ssn = Mgr_ssn DEPARTMENT RESULT ← πFname, Minit, Lname, Dname(TEMP)
  • Every employee appears in the result

  • Employees who do not manage a department have NULL in Dname


2. RIGHT OUTER JOIN

A RIGHT OUTER JOIN preserves all tuples from the right relation (S).

  • Matching tuples from the left relation (R) are included

  • Nonmatching tuples from R are replaced with NULL values

Definition

R ⟖ S

Use Case

When it is more important to preserve all tuples from the second relation, such as listing all departments even if they have no manager.


3. FULL OUTER JOIN

A FULL OUTER JOIN preserves all tuples from both relations.

  • Matching tuples are combined normally

  • Nonmatching tuples from either relation are included with NULL padding

Definition

R ⟗ S

Characteristics

  • No information is lost

  • Result contains:

    • Matching tuples

    • Nonmatching tuples from R

    • Nonmatching tuples from S


INNER JOIN vs OUTER JOIN: Comparison

Aspect    INNER JOINOUTER JOIN
Matching required        Yes            No
Unmatched tuples        Discarded            Preserved
NULL padding        No            Yes
Information loss        Possible            Avoided
Common use        Relationship queries            Reporting and analysis

SQL Perspective

  • INNER JOIN is the default JOIN in SQL

  • OUTER JOINs were added in SQL2 to support practical business needs

  • SQL explicitly supports:

    • LEFT OUTER JOIN

    • RIGHT OUTER JOIN

    • FULL OUTER JOIN


 Summary

  • INNER JOIN returns only tuples that satisfy the join condition in both relations.

  • OUTER JOIN retains unmatched tuples by padding missing values with NULLs.

  • OUTER JOINs are essential when completeness of information is required.

Comments

Popular posts from this blog

Database Management Systems DBMS PCCST402 Semester 4 KTU CS 2024 Scheme

Data Models, Schemas and Instances

Introduction to Database Management System -DBMS