So far, we've been working with single tables to retrieve, filter, and sort data. But in real-world databases, information is often split across multiple related tables. This is where the power of SQL really begins to shine!
Imagine you're building a library system. Instead of storing all book information in one massive table, you might have separate tables for Authors and Books. This approach has several advantages, but it also means we need to learn how to combine data from multiple tables to answer meaningful questions.
Benefits of using multiple related tables:
This process of organizing data into separate, related tables is called database normalization.
When tables are related, they share common information through keys:
author_id
in the Authors table)author_id
in the Books table)Let's look at our example tables:
Authors Table:
author_id | first_name | last_name | birth_year | country
----------|------------|-----------|------------|--------
1 | J.K. | Rowling | 1965 | UK
2 | Stephen | King | 1947 | USA
3 | Agatha | Christie | 1890 | UK
Books Table:
book_id | title | author_id | publication_year | pages
--------|------------------------------------------|-----------|------------------|------
1 | Harry Potter and the Philosopher's Stone | 1 | 1997 | 223
2 | The Shining | 2 | 1977 | 447
3 | Murder on the Orient Express | 3 | 1934 | 256
4 | Harry Potter and the Chamber of Secrets | 1 | 1998 | 251
💡 Key Point: Notice how the author_id
in the Books table matches the author_id
in the Authors table? This is how we connect the related data.
To combine data from multiple tables, we use the JOIN
clause. The most common type is the INNER JOIN
, which returns only rows that have matching values in both tables.
Basic INNER JOIN Syntax:
SELECT column1, column2, column3
FROM first_table
INNER JOIN second_table
ON first_table.common_column = second_table.common_column;
Real Example:
SELECT books.title, authors.first_name, authors.last_name
FROM books
INNER JOIN authors
ON books.author_id = authors.author_id;
This query would return:
title | first_name | last_name
-----------------------------------------|------------|----------
Harry Potter and the Philosopher's Stone | J.K. | Rowling
The Shining | Stephen | King
Murder on the Orient Express | Agatha | Christie
Harry Potter and the Chamber of Secrets | J.K. | Rowling
Here's what happens step by step when you run an INNER JOIN:
Table Prefixes: Use table_name.column_name
to specify which table a column comes from. This is especially important when both tables have columns with the same name.
-- Clear and explicit
SELECT books.title, authors.first_name
FROM books
INNER JOIN authors ON books.author_id = authors.author_id;
ON Clause: Defines how the tables are related (usually primary key = foreign key).
-- The ON clause creates the connection
ON books.author_id = authors.author_id
INNER JOIN vs JOIN: These are equivalent – INNER JOIN
is more explicit and clearer.
Result Size: INNER JOIN
only returns rows where there's a match in both tables.
You can combine JOINs with all the clauses you've learned:
JOIN with WHERE:
SELECT books.title, authors.first_name, authors.last_name
FROM books
INNER JOIN authors ON books.author_id = authors.author_id
WHERE authors.country = 'UK';
JOIN with ORDER BY:
SELECT books.title, authors.last_name, books.publication_year
FROM books
INNER JOIN authors ON books.author_id = authors.author_id
ORDER BY books.publication_year DESC;
JOIN with LIMIT:
SELECT books.title, authors.first_name, authors.last_name
FROM books
INNER JOIN authors ON books.author_id = authors.author_id
ORDER BY books.publication_year DESC
LIMIT 3;
💡 Pro Tip: Always use table prefixes when working with JOINs. It makes your queries clearer and prevents errors when both tables have columns with the same name.
In this lesson's exercises, you'll work with our Authors and Books tables to practice INNER JOINs. You'll learn how to:
The exercises build from simple joins to more complex queries that combine multiple SQL concepts. Take your time to understand how the tables connect – this foundation will be crucial as you work with more complex database relationships!
Ready to practice SQL? Try our coding challenges →