What Are MySQL Joins?

In MySQL, joins are used to combine data from two or more tables based on a related column between them.

For example, imagine you have:

  • A users table
  • An orders table

You can use a JOIN to connect each user to their orders.

Example Tables

users

user_idname
1Ada
2Ben
3Chika

orders

order_iduser_iditem
1011Laptop
1021Keyboard
1032Mouse

Types of Joins in MySQL

There are four main types of joins you’ll use:

1. INNER JOIN

Returns only the rows that have matching values in both tables.

Query:

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

Result:

nameitem
AdaLaptop
AdaKeyboard
BenMouse

Explanation:

  • Only users who have placed orders appear (Chika is excluded).

2. LEFT JOIN (or LEFT OUTER JOIN)

Returns all rows from the left table, and matching rows from the right table.
If there’s no match, the result is NULL for columns from the right table.

Query:

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

Result:

nameitem
AdaLaptop
AdaKeyboard
BenMouse
ChikaNULL

Explanation:

  • All users are shown.
  • Chika appears even though she has no orders.

3. RIGHT JOIN (or RIGHT OUTER JOIN)

Returns all rows from the right table, and matching rows from the left table.
If no match exists, you’ll get NULL from the left table.

Query:

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

Result:

nameitem
AdaLaptop
AdaKeyboard
BenMouse

Explanation:

  • Returns all orders, even if the user table had missing data (though not in this example).

  • 4. FULL JOIN (or FULL OUTER JOIN)

    Returns all rows from both tables, with NULL where there’s no match.

    Note: MySQL doesn’t support FULL OUTER JOIN directly,
    but you can simulate it using a UNION of LEFT JOIN and RIGHT JOIN.

    SELECT users.name, orders.item
    FROM users
    LEFT JOIN orders ON users.user_id = orders.user_id
    UNION
    SELECT users.name, orders.item
    FROM users
    RIGHT JOIN orders ON users.user_id = orders.user_id;

    5. CROSS JOIN

    Creates a Cartesian product — every row from the first table joins with every row from the second table.

    Query:

    SELECT users.name, orders.item
    FROM users
    CROSS JOIN orders;

    Result:
    If there are 3 users and 3 orders, you’ll get 9 rows (3 × 3).

    Use case: Rare — typically used for generating combinations or test data.

    Summary Table

    Join TypeReturnsExample Use
    INNER JOINOnly matching rowsUsers who made purchases
    LEFT JOINAll left table rows, plus matchesAll users + their orders
    RIGHT JOINAll right table rows, plus matchesAll orders + user details
    FULL JOINAll rows from both tablesCombine all users and all orders
    CROSS JOINEvery possible pairGenerate combinations

    Best Practices

    Always use aliases for cleaner queries:

    SELECT u.name, o.item
    FROM users u
    LEFT JOIN orders o
    ON u.user_id = o.user_id;

    Use JOINs with indexes to improve performance.

    Avoid unnecessary joins; each one adds computational cost.

    Test with small datasets before scaling up.

    Recent Posts

    Perks of Being a Copy-Paste Developer

    Why borrowing code is a skill—when you understand what you're copying. For years, "copy-paste developer"…

    1 week ago

    Conditionally Disable an Input Field Using React Hook Form

    Interactive forms rarely keep every field active all the time. Sometimes an input should only…

    1 week ago

    JavaScript Temporal API

    A modern JavaScript API for working with dates, times, time zones, and calendars without the…

    1 week ago

    Node.js Under the Hood

    Understanding What Happens Behind the Scenes Node.js looks simple from the outside—you write JavaScript, call…

    2 weeks ago

    This is How You Cultivate Negative Capability

    The need for absolute certainty is the greatest disease the engineering mind faces. The moment…

    2 weeks ago

    You Don’t have to become the World’s Greatest Programmer.

    The software industry is one of the most competitive places to build a career. Every…

    2 weeks ago