SQL Joins Explained With Examples

    Master INNER, LEFT, RIGHT, and FULL joins with practical SQL examples and visual explanations.

    10 min read
    SQL
    Database
    Joins
    Tutorial

    Introduction

    If you're learning SQL, one of the most important things you'll ever understand is JOINS. They're the backbone of combining data across multiple tables — which is how real databases actually work.

    This guide breaks down SQL joins in a simple, visual, example-driven way so you can finally understand:

    • ✔ How each JOIN works
    • ✔ When to use which JOIN
    • ✔ How LEFT, RIGHT, INNER, and FULL JOIN differ
    • ✔ Real-world examples from business databases

    Let's make SQL joins easy.

    1. What Are SQL Joins?

    A JOIN allows you to combine rows from two or more tables based on a related column.

    For example:

    • Users table
    • Orders table

    joined by user_id.

    Without joins, each table is isolated.

    With joins, tables become powerful relational data sources.

    2. Example Tables Used in This Guide

    Table: users

    user_idname
    1Alice
    2Bob
    3Charlie

    Table: orders

    order_iduser_idproduct
    1011Laptop
    1021Mouse
    1032Keyboard

    3. INNER JOIN (Most Common)

    Returns matching rows from both tables.

    SELECT users.name, orders.product
    FROM users
    INNER JOIN orders
    ON users.user_id = orders.user_id;

    Result:

    nameproduct
    AliceLaptop
    AliceMouse
    BobKeyboard

    🧠 Charlie has no orders → not included.

    Use when:

    You only want records that exist in both tables.

    4. LEFT JOIN (LEFT OUTER JOIN)

    Returns all rows from LEFT table + matching rows from RIGHT. Missing matches become NULL.

    SELECT users.name, orders.product
    FROM users
    LEFT JOIN orders
    ON users.user_id = orders.user_id;

    Result:

    nameproduct
    AliceLaptop
    AliceMouse
    BobKeyboard
    CharlieNULL

    🧠 LEFT = users, so all users appear, even those with no orders.

    Use when:

    You want the "full list" of the primary table.

    Examples:

    • Show all customers, even if they haven't ordered
    • Show all products, even if none were sold

    5. RIGHT JOIN (RIGHT OUTER JOIN)

    Opposite of LEFT JOIN. Returns ALL rows from the RIGHT table + matches from LEFT.

    SELECT users.name, orders.product
    FROM users
    RIGHT JOIN orders
    ON users.user_id = orders.user_id;

    Result:

    Same data as INNER JOIN in this example, because all orders have matching users:

    nameproduct
    AliceLaptop
    AliceMouse
    BobKeyboard

    Use when:

    Right table is the "main" table.

    Examples:

    • Show all orders, even if the user was deleted
    • Show all shipped items, even if not linked to a customer yet

    6. FULL OUTER JOIN

    Returns ALL rows from both tables. Where no match exists → NULLs appear.

    SELECT users.name, orders.product
    FROM users
    FULL OUTER JOIN orders
    ON users.user_id = orders.user_id;

    Result:

    nameproduct
    AliceLaptop
    AliceMouse
    BobKeyboard
    CharlieNULL
    NULLNULL

    (The last NULL row only appears if orders exist with no matching user.)

    Use when:

    You want everything from both sides.

    👉 Great for auditing or data cleanup.

    7. CROSS JOIN

    Produces the Cartesian product of both tables. Every row in table A combines with every row in table B.

    Example:

    SELECT *
    FROM users
    CROSS JOIN orders;

    If 3 users × 3 orders → 9 rows.

    Use when:

    • Generating combinations
    • Time series grids
    • Matrix-style reporting

    Avoid for large tables — can explode rows.

    8. SELF JOIN

    A table joined with itself.

    Useful for hierarchies like:

    • Employees → Managers
    • Categories → Subcategories
    • Friends → Mutual friends

    Example:

    SELECT A.name AS employee, B.name AS manager
    FROM employees A
    LEFT JOIN employees B
    ON A.manager_id = B.id;

    9. Real-World JOIN Examples

    📌 Example 1: Get all users and their latest order

    SELECT u.name, o.product
    FROM users u
    LEFT JOIN orders o
    ON u.user_id = o.user_id
    ORDER BY u.name, o.order_id DESC;

    📌 Example 2: Find users with no orders

    SELECT u.name
    FROM users u
    LEFT JOIN orders o
    ON u.user_id = o.user_id
    WHERE o.order_id IS NULL;

    📌 Example 3: Count orders per user

    SELECT u.name, COUNT(o.order_id) AS total_orders
    FROM users u
    LEFT JOIN orders o
    ON u.user_id = o.user_id
    GROUP BY u.name;

    10. When to Use Which JOIN (Quick Guide)

    JOINWhen to Use
    INNER JOINKeep only matches
    LEFT JOINKeep everything on left
    RIGHT JOINKeep everything on right
    FULL JOINKeep everything from both
    CROSS JOINAll combinations
    SELF JOINHierarchies or relationships

    Conclusion

    SQL joins are the foundation of working with relational databases. Once you understand how each join behaves — and when to use them — you unlock the ability to:

    • ✔ Build better queries
    • ✔ Analyse data faster
    • ✔ Solve real business problems
    • ✔ Ace SQL interview questions
    • ✔ Work confidently as a database developer

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service