· SQL Guroo · SQL Tutorial  · 3 min read

Mastering SQL Joins - Understanding FULL OUTER JOIN

Learn how to use FULL OUTER JOIN in SQL with simple, practical examples.

Learn how to use FULL OUTER JOIN in SQL with simple, practical examples.

Introduction

FULL OUTER JOIN shows all records from both tables, whether they match or not. Think of it like comparing two lists and wanting to see everything from both lists, even items that don’t match up.

Why FULL OUTER JOIN Matters?

It’s useful when you need to:

  • Compare two lists completely
  • Find items that exist in either list
  • Check for mismatches between lists
  • See the complete picture of two datasets

Main Content: Understanding FULL OUTER JOIN

Let’s use these simple tables:

-- Create two simple tables
CREATE TABLE class_a (
    student_id INT,
    student_name VARCHAR(50)
);

CREATE TABLE class_b (
    student_id INT,
    student_name VARCHAR(50)
);

-- Insert sample data
INSERT INTO class_a VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie');

INSERT INTO class_b VALUES
(2, 'Bob'),
(3, 'Charlie'),
(4, 'David');

Example 1: Basic FULL OUTER JOIN

Let’s see all students from both classes:

SELECT 
    a.student_name as class_a_student,
    b.student_name as class_b_student
FROM class_a a
FULL OUTER JOIN class_b b 
    ON a.student_id = b.student_id;

This simple query:

  1. Shows all students from both classes
  2. Matches students who are in both classes
  3. Shows NULL when a student is only in one class

Results:

class_a_student | class_b_student
----------------|----------------
Alice           | NULL
Bob             | Bob
Charlie         | Charlie
NULL            | David

Example 2: Finding Students in Only One Class

Let’s find which students are unique to each class:

SELECT 
    a.student_name as only_in_class_a,
    b.student_name as only_in_class_b
FROM class_a a
FULL OUTER JOIN class_b b 
    ON a.student_id = b.student_id
WHERE a.student_id IS NULL 
   OR b.student_id IS NULL;

This query:

  1. Joins both class lists
  2. Uses WHERE to find unmatched students
  3. Shows which students are in only one class

Results:

only_in_class_a | only_in_class_b
----------------|----------------
Alice           | NULL
NULL            | David

Example 3: Simple Student Status

Let’s see each student’s enrollment status:

SELECT 
    COALESCE(a.student_name, b.student_name) as student_name,
    CASE 
        WHEN a.student_id IS NOT NULL AND b.student_id IS NOT NULL THEN 'In Both Classes'
        WHEN a.student_id IS NOT NULL THEN 'Only in Class A'
        ELSE 'Only in Class B'
    END as enrollment_status
FROM class_a a
FULL OUTER JOIN class_b b 
    ON a.student_id = b.student_id;

This query:

  1. Lists all students
  2. Shows which class(es) they’re in
  3. Uses COALESCE to show the name once

Results:

student_name | enrollment_status
-------------|------------------
Alice        | Only in Class A
Bob          | In Both Classes
Charlie      | In Both Classes
David        | Only in Class B

Best Practices

  1. Table Names

    • Use clear, meaningful table names
    • Use short but descriptive aliases
    • Example: class_a as a, class_b as b
  2. Join Conditions

    • Use the correct columns for joining
    • Make sure the data types match
    • Example: ON a.student_id = b.student_id
  3. Column Selection

    • Be clear which table each column comes from
    • Use table aliases to avoid ambiguity
    • Example: a.student_name, b.student_name
  4. NULL Understanding

    • Remember that unmatched records show as NULL
    • Use IS NULL to find records unique to one table
    • Use COALESCE when you need a single non-NULL value
  5. Query Organization

    • Keep the join condition simple
    • List the more important table first (though order doesn’t affect results)
    • Use proper indentation for readability

Remember:

  • FULL OUTER JOIN shows ALL records from BOTH tables
  • Matching records appear together
  • Non-matching records show NULL for the other table
  • It’s perfect for comparing complete lists

Happy querying! 🚀

Back to Blog

Related Posts

View All Posts »