SQL Joins Explained: Complete Guide with Examples

    SQL Joins Explained: Complete Guide with Examples

    20/10/2025

    Introduction

    If you've ever worked with databases, you've almost certainly encountered situations where you need to retrieve data from multiple tables at once. That's where SQL JOIN clauses come in. At first, the different types of joins can seem confusing, but they are a fundamental concept for anyone working with relational databases.

    In this guide, we'll break down the most common types of SQL joins—INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN—using simple examples and visual aids to make the concepts crystal clear.

    Related: Learn about SQL Common Table Expressions for advanced query techniques.

    Setting Up Our Example Tables

    To understand joins, we need some data to work with. Let's imagine we have a small company with two tables: Employees and Departments.

    Our Employees table stores information about each employee, including which department they belong to. Notice that some employees might not be assigned to a department yet (DepartmentID is NULL).

    Employees Table:

    EmployeeIDNameDepartmentID
    1Alice101
    2Bob102
    3Charlie101
    4DavidNULL

    Our Departments table lists the available departments. Notice that the "Marketing" department has no employees assigned to it yet.

    Departments Table:

    DepartmentIDDepartmentName
    101Engineering
    102HR
    103Marketing

    Now, let's use these tables to explore the different types of joins.


    1. INNER JOIN

    An INNER JOIN returns only the rows where the join condition is met in both tables. It's the most common type of join. Think of it as finding the intersection of two sets.

    Goal: Get a list of all employees who are assigned to a department.

    SELECT E.Name, D.DepartmentName FROM Employees AS E INNER JOIN Departments AS D ON E.DepartmentID = D.DepartmentID;
    Intersection of Employees and Departments Result: only overlapping rows Employees (left) Departments (right)

    Result:

    NameDepartmentName
    AliceEngineering
    BobHR
    CharlieEngineering

    Explanation:

    • Alice, Bob, and Charlie are in the result because their DepartmentID exists in the Departments table.
    • David is excluded because his DepartmentID is NULL and doesn't match any department.
    • The "Marketing" department is excluded because no employee is assigned to DepartmentID 103.

    2. LEFT JOIN (or LEFT OUTER JOIN)

    A LEFT JOIN returns all rows from the left table (Employees in our case) and the matched rows from the right table (Departments). If there is no match in the right table, NULL is returned for the columns from the right table.

    Goal: Get a list of all employees and their department, even if they aren't assigned to one yet.

    SELECT E.Name, D.DepartmentName FROM Employees AS E LEFT JOIN Departments AS D ON E.DepartmentID = D.DepartmentID;
    All Employees + matches from Departments Result: entire left circle highlighted Employees (left) Departments (right)

    Result:

    NameDepartmentName
    AliceEngineering
    BobHR
    CharlieEngineering
    DavidNULL

    Explanation:

    • All employees from the Employees table are included in the result.
    • David, who has a NULL DepartmentID, is included, but his DepartmentName is NULL because there was no match in the Departments table.

    3. RIGHT JOIN (or RIGHT OUTER JOIN)

    A RIGHT JOIN is the opposite of a LEFT JOIN. It returns all rows from the right table (Departments) and the matched rows from the left table (Employees). If there is no match in the left table, NULL is returned for the columns from the left table.

    Goal: Get a list of all departments and the employees in them, including departments that have no employees.

    SELECT E.Name, D.DepartmentName FROM Employees AS E RIGHT JOIN Departments AS D ON E.DepartmentID = D.DepartmentID;
    All Departments + matches from Employees Result: entire right circle highlighted Employees (left) Departments (right)

    Result:

    NameDepartmentName
    AliceEngineering
    CharlieEngineering
    BobHR
    NULLMarketing

    Explanation:

    • All departments from the Departments table are included.
    • The "Marketing" department is in the result, but the Name is NULL because no employee is assigned to it.

    Note: You can achieve the same result as a RIGHT JOIN by swapping the tables and using a LEFT JOIN. Many developers prefer to use LEFT JOIN exclusively for consistency.


    4. FULL OUTER JOIN

    A FULL OUTER JOIN returns all rows when there is a match in either the left or the right table. It combines the functionality of both LEFT JOIN and RIGHT JOIN. If there's no match, the respective columns will be NULL.

    Goal: Get a complete list of all employees and all departments, matching them where possible.

    SELECT E.Name, D.DepartmentName FROM Employees AS E FULL OUTER JOIN Departments AS D ON E.DepartmentID = D.DepartmentID;
    All rows from both tables Result: union of both circles Employees (left) Departments (right)

    Result:

    NameDepartmentName
    AliceEngineering
    BobHR
    CharlieEngineering
    DavidNULL
    NULLMarketing

    Explanation:

    • This join includes every employee and every department.
    • David is included with a NULL department name (like in the LEFT JOIN).
    • The "Marketing" department is included with a NULL employee name (like in the RIGHT JOIN).

    Note: Some database systems like MySQL do not support FULL OUTER JOIN directly. You can emulate it by combining a LEFT JOIN and a RIGHT JOIN with a UNION.

    Conclusion

    Understanding SQL JOINs is essential for effectively querying relational databases. Here's a quick summary:

    • INNER JOIN: Returns only the matching rows from both tables.
    • LEFT JOIN: Returns all rows from the left table, and matched rows from the right table.
    • RIGHT JOIN: Returns all rows from the right table, and matched rows from the left table.
    • FULL OUTER JOIN: Returns all rows from both tables, matching them where possible.

    By choosing the right join for your needs, you can combine data from multiple tables in powerful and flexible ways. Happy querying!

    Summarise

    Transform Your Learning

    Get instant AI-powered summaries of YouTube videos and websites. Save time while enhancing your learning experience.

    Instant video summaries
    Smart insights extraction
    Channel tracking