Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Joins Explained: Types, Examples, and Syntax

· 7 min read · Home Tech

In this article
  1. Why Joins Matter in Relational Databases
  2. Setting Up Example Tables
  3. INNER JOIN: Only Matching Rows
  4. LEFT JOIN (or LEFT OUTER JOIN): All from Left, Matches from Right
  5. RIGHT JOIN (or RIGHT OUTER JOIN): All from Right, Matches from Left
  6. FULL OUTER JOIN: Everything from Both Sides
  7. CROSS JOIN: Cartesian Product
  8. How to Choose the Right Join
  9. Common Pitfalls and Performance Tips
  10. Frequently Asked Questions

SQL joins combine rows from two or more tables based on a related column, and the main types are INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, and CROSS JOIN. This article explains each join type with clear examples, syntax, and practical guidance on when to use them.

Why Joins Matter in Relational Databases

Relational databases store data in separate tables to avoid redundancy and keep information consistent. For example, a typical e-commerce database might have a customers table and an orders table. Each customer’s name and address appear once in the customers table, while orders reference the customer via a foreign key (like customer_id). To answer questions like “which customers placed orders in the last month?”, you need to combine rows from both tables—that’s exactly what a join does.

Without joins, you would have to manually match rows or store everything in one giant table, which leads to duplicate data and update anomalies. Joins let you query across tables as if they were a single dataset, while keeping the underlying storage normalized.

Setting Up Example Tables

To illustrate each join, we’ll use two simple tables:

customers

  • customer_id (primary key)
  • name

orders

  • order_id (primary key)
  • customer_id (foreign key referencing customers)
  • amount

Sample data:

customers:

1 | Alice
2 | Bob
3 | Carol

orders:

101 | 1 | 250
102 | 2 | 400
103 | 1 | 150
104 | 4 | 300

Note that Carol (customer_id 3) has no orders, and order 104 references customer 4, who is not in the customers table. These gaps help show how different joins handle missing matches.

INNER JOIN: Only Matching Rows

The INNER JOIN returns only rows where the join condition is satisfied in both tables. It’s the most common join type and the default in many systems.

Syntax:

SELECT *
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;

Result:

1 | Alice | 101 | 1 | 250
1 | Alice | 103 | 1 | 150
2 | Bob | 102 | 2 | 400

Alice and Bob appear because they have matching orders. Carol is omitted (no orders), and order 104 is omitted (no matching customer). If a customer has multiple orders, that customer’s row repeats for each order—this is normal and expected.

LEFT JOIN (or LEFT OUTER JOIN): All from Left, Matches from Right

A LEFT JOIN returns every row from the left table (the one before the JOIN keyword), and matching rows from the right table. If no match exists, the right-side columns are filled with NULL.

Syntax:

SELECT *
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;

Result:

1 | Alice | 101 | 1 | 250
1 | Alice | 103 | 1 | 150
2 | Bob | 102 | 2 | 400
3 | Carol | NULL | NULL | NULL

Carol is included with NULLs for order data. This is perfect for “list all customers and their orders, even if they have none.”

RIGHT JOIN (or RIGHT OUTER JOIN): All from Right, Matches from Left

The RIGHT JOIN is the mirror image of LEFT JOIN: it returns all rows from the right table, with matching rows from the left. Unmatched left rows become NULL.

Syntax:

SELECT *
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;

Result:

1 | Alice | 101 | 1 | 250
1 | Alice | 103 | 1 | 150
2 | Bob | 102 | 2 | 400
NULL | NULL | 104 | 4 | 300

Order 104 appears even though customer 4 is missing. RIGHT JOIN is less common because you can usually rewrite it as a LEFT JOIN by swapping the table order, but it’s still available in most databases.

FULL OUTER JOIN: Everything from Both Sides

A FULL OUTER JOIN returns all rows from both tables. When a match exists, the rows are combined; when not, missing side gets NULLs.

Syntax (note: MySQL does not support FULL OUTER JOIN directly, but it can be simulated with UNION of LEFT and RIGHT joins):

SELECT *
FROM customers
FULL OUTER JOIN orders ON customers.customer_id = orders.customer_id;

Result:

1 | Alice | 101 | 1 | 250
1 | Alice | 103 | 1 | 150
2 | Bob | 102 | 2 | 400
3 | Carol | NULL | NULL | NULL
NULL | NULL | 104 | 4 | 300

This gives a complete picture of both tables, showing orphaned records on either side. It’s useful for data reconciliation and finding mismatches.

CROSS JOIN: Cartesian Product

A CROSS JOIN combines every row from the first table with every row from the second table, producing a Cartesian product. No ON condition is used.

Syntax:

SELECT *
FROM customers
CROSS JOIN orders;

Result (3 customers × 4 orders = 12 rows):

1 | Alice | 101 | 1 | 250
1 | Alice | 102 | 2 | 400
1 | Alice | 103 | 1 | 150
1 | Alice | 104 | 4 | 300
2 | Bob | 101 | 1 | 250
... (all combinations)

CROSS JOINs are rarely used in production because they can generate huge result sets, but they’re handy for generating combinations (e.g., all possible product-size pairs) or testing.

How to Choose the Right Join

  • Need only rows that exist in both tables? Use INNER JOIN.
  • Need all rows from the primary table, with optional matches? Use LEFT JOIN (or RIGHT JOIN if the primary table is on the right).
  • Need all rows from both tables, including orphans? Use FULL OUTER JOIN.
  • Need every combination of rows? Use CROSS JOIN.

A common rule of thumb: start with INNER JOIN, then switch to LEFT JOIN if you need to preserve unmatched rows from the main table. RIGHT JOIN is often avoided for readability—just swap the table order.

Common Pitfalls and Performance Tips

  • Duplicate rows: If the join condition matches multiple rows on either side, you get duplicate rows. This is correct but can inflate results—use SELECT DISTINCT if you need unique rows, but be careful it doesn’t hide real duplicates.
  • NULL handling: When using OUTER JOINs, columns from the non-matching side are NULL. Filtering with WHERE on those columns (e.g., WHERE orders.amount > 100) can inadvertently exclude NULL rows, turning your LEFT JOIN into an INNER JOIN. Use WHERE orders.customer_id IS NULL to find unmatched rows.
  • Performance: Joins are faster when the join columns are indexed. Primary keys are indexed automatically, but foreign keys often need explicit indexes. Also, be mindful of joining on non-unique columns—it can multiply rows unexpectedly.
  • Ambiguous column names: When both tables have a column with the same name (like customer_id), always qualify it with the table name or alias to avoid errors.

Frequently Asked Questions

What is the difference between INNER JOIN and OUTER JOIN?

INNER JOIN returns only rows that have matching values in both tables. OUTER JOIN (LEFT, RIGHT, FULL) returns all rows from one or both tables, filling in NULLs where no match exists. Think of INNER as “intersection” and OUTER as “union plus extras.”

Can I join more than two tables?

Yes, you can chain joins: FROM table1 JOIN table2 ON ... JOIN table3 ON .... Each join combines the result of the previous join with the next table. The order matters for OUTER JOINs, so plan carefully.

What is a self-join?

A self-join is when a table is joined with itself. It’s used to compare rows within the same table, such as finding employees who earn more than their manager. You must use table aliases to distinguish the two copies.

Does SQL join always need an ON clause?

Most joins require an ON condition to specify how rows are matched. The exception is CROSS JOIN, which intentionally has no condition. Some databases also allow USING (column) as shorthand when the column names are identical.

What is the difference between ON and WHERE in a join?

ON defines the join condition (how tables are combined). WHERE filters the result set after the join. In INNER JOIN, both work similarly, but in OUTER JOIN, putting conditions in WHERE can change the result by removing NULL rows, so it’s important to use ON for join logic and WHERE for filtering.

How do I find unmatched rows with a join?

Use a LEFT JOIN and then add a WHERE clause that checks for NULL in the right table’s primary key. For example: SELECT customers.* FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id WHERE orders.order_id IS NULL; This returns customers with no orders.