Chapter 11: Joins

Up until now, we’ve been working with one table at a time. But in real life, data is almost always spread across multiple tables (customers in one table, orders in another, products in a third…). Joins are how we combine these tables to answer meaningful questions like:

  • “Which customers ordered which books?”
  • “Show all authors and their books — even if some authors haven’t written any books yet.”
  • “Compare each book with the average price of books by the same author.”

Today we’re going to master all the major types of joins:

  • INNER JOIN (the most common)
  • LEFT / RIGHT / FULL OUTER JOIN (for including unmatched rows)
  • CROSS JOIN (Cartesian product — every combination)
  • SELF JOIN (joining a table to itself)

We’ll use our bookshop database and add a few more tables for realistic examples.

Step 1: Setup – Our Tables for This Chapter

SQL

1. INNER JOIN – Only Matching Rows from Both Tables

INNER JOIN returns only the rows where there is a match in both tables. If an author has no books → they won’t appear. If a book has no author → it won’t appear (but we have foreign key so this can’t happen).

Syntax:

SQL

Example – All books with their author names:

SQL

Result (only authors who have books):

book_title author_name price country
The Room on the Roof Ruskin Bond 250.00 India
2 States Chetan Bhagat 299.00 India
The White Tiger Jhumpa Lahiri 399.00 USA

Chitra Banerjee (author_id 8) and Vikram Seth (9) do NOT appear because they have no books.

2. LEFT JOIN (LEFT OUTER JOIN) – All from Left Table + Matching from Right

LEFT JOIN keeps all rows from the left table, and adds matching rows from the right table. If no match → right table columns get NULL.

Syntax:

SQL

Example – All authors, and their books (if any):

SQL

Result:

author_name country book_title price
Amish Tripathi India The Immortals of Meluha 349.00
Chetan Bhagat India 2 States 299.00
Chitra Banerjee USA NULL NULL
Jhumpa Lahiri USA The White Tiger 399.00
Khushwant Singh India Train to Pakistan 280.00
Ruskin Bond India The Room on the Roof 250.00
Vikram Seth India NULL NULL

Perfect for: “Show all authors, even those without books yet.”

RIGHT JOIN is the opposite: all from right table + matching from left. (Rarely used — most people just swap tables and use LEFT JOIN instead.)

3. FULL OUTER JOIN – All Rows from Both Tables

FULL OUTER JOIN = LEFT JOIN + RIGHT JOIN combined. You get all rows from both tables, with NULLs where no match.

Syntax:

SQL

Note: MySQL/MariaDB do not support FULL OUTER JOIN natively. You can simulate it with UNION of LEFT + RIGHT JOIN (excluding duplicates).

Example (simulated in MySQL):

SQL

Result: Every author appears (even without books) and every book appears (even if author missing — though not possible here).

4. CROSS JOIN – Every Possible Combination

CROSS JOIN combines every row from left with every row from right — no ON condition needed. Result: number of rows in table1 × number of rows in table2

Syntax:

SQL

Example – Suppose we have 9 authors and 10 books:

SQL

Result: 90 rows! Every author paired with every book. Use case: Generating all possible combinations (e.g., every customer × every product for a report).

Rarely used in production — but good to know.

5. SELF JOIN – Joining a Table to Itself

When a table has a relationship with itself (e.g., employees and their managers, books and sequels, categories and parent categories).

Example – Find authors who have written more than one book (using SELF JOIN):

SQL

Another classic self-join example (if we had a ‘sequel_to’ column in books):

SQL

Quick Comparison Table (2026 Style)

Join Type What it returns When to use Matching Rows?
INNER JOIN Only matching rows from both Common case: “books and their authors” Yes
LEFT JOIN All from left + matching from right (NULLs) “All customers, even those with no orders” Optional
RIGHT JOIN All from right + matching from left Rarely used (swap tables and use LEFT) Optional
FULL OUTER All from both (NULLs where no match) “All authors and all books, even unmatched” Optional
CROSS JOIN Every combination All possible pairs (rare) No condition
SELF JOIN Table joined to itself Hierarchical data, comparisons within table Depends

Full Realistic Report Example

SQL

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *