Before you start, you need
Connect tables with keys and relationships · Use aliases and create calculated columns
Join the pairs that match
The title is in books and the author's name is in authors. INNER JOIN lets you read both together: for each book, it finds the author referenced by its author_id.
ON l.author_id = a.id says how to find the match: it compares the book's reference with the author's id. Joining l.id with a.id would be a different comparison: those numbers identify different things.
books AS l and authors AS a dan short names for the tables, called aliases. l.title means the title from books; a.name, the name from authors. They show where each value comes from.
Only the matching pairs appear. If a book has no matching author, it does not appear. Two books by the same author produce two rows, one per book, so it is normal for the author's name to repeat.
SELECT ...
FROM table_1 AS a
INNER JOIN table_2 AS b
ON a.key = b.reference;See it in action
We want each book next to its author's name.
| title | author_id |
|---|---|
| Nineteen Eighty-Four | 2 |
| Animal Farm | 2 |
| id | name |
|---|---|
| 2 | George Orwell |
SELECT l.title, a.name AS author
FROM books AS l
INNER JOIN authors AS a
ON l.author_id = a.id;| title | author |
|---|---|
| Pride and Prejudice | Jane Austen |
| Nineteen Eighty-Four | George Orwell |
| A Room of One’s Own | Virginia Woolf |
| The Hitchhiker’s Guide to the Galaxy | Douglas Adams |
| The Dispossessed | Ursula K. Le Guin |
| Animal Farm | George Orwell |
| The Left Hand of Darkness | Ursula K. Le Guin |
- l and a are table aliases. They show where each column comes from without repeating the full table name.
- ON compares the book's reference with the author's identity. Only the matching pairs are kept.
- Two books by one author produce two rows: each output row is still one book. That is an expected relationship.
- Do not join l.id with a.id: those ids represent different things. Use the relationship you have just learned.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Show each order with its customer
Write a query that returns the order id, customer name and status for every order, together with its customer.
Expected result: 18 rows with the columns id, name, status, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The customer reference is in orders.customer_id and points to customers.id.
Match p.customer_id with c.id.
Select columns from both tables.
Solution explained
SELECT p.id, c.name, p.status FROM orders AS p INNER JOIN customers AS c ON p.customer_id = c.id;The ON condition states the real relationship; the aliases save you writing long names.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Relate products and categories
Show the name of each product with the alias product and the name of its category with the alias category. Join products.category_id with categories.id.
Expected result: 9 rows with columns product, category, in that order. The order of the rows does not matter.
View tables and data
Open this panel to see the full data.
The foreign key is in products and points to categories.
Both tables have a name column: show which one each comes from with p. and c.
Relate p.category_id with c.id and use the aliases product and category.
Solution explained
SELECT p.name AS product, c.name AS category FROM products AS p INNER JOIN categories AS c ON p.category_id = c.id;Each row is a product with its matching category. Both tables have a name column: table and column aliases avoid the ambiguity.
Key takeaways
- INNER JOIN keeps the matching pairs.
- ON states the relationship.
- Aliases show which table each column comes from.