Module 4 · Relating tables

Match rows with INNER JOIN

By the end you will be able to show together the rows that match between two tables.

About 14 min

0 of 52 lessons

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.

Minimal syntax
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.

Sample data: 2 rows
titleauthor_id
Nineteen Eighty-Four2
Animal Farm2
Sample data: 1 rows
idname
2George Orwell
SQL
SELECT l.title, a.name AS author
FROM books AS l
INNER JOIN authors AS a
  ON l.author_id = a.id;
Example result: 7 rows
titleauthor
Pride and PrejudiceJane Austen
Nineteen Eighty-FourGeorge Orwell
A Room of One’s OwnVirginia Woolf
The Hitchhiker’s Guide to the GalaxyDouglas Adams
The DispossessedUrsula K. Le Guin
Animal FarmGeorge Orwell
The Left Hand of DarknessUrsula K. Le Guin
  1. l and a are table aliases. They show where each column comes from without repeating the full table name.
  2. ON compares the book's reference with the author's identity. Only the matching pairs are kept.
  3. Two books by one author produce two rows: each output row is still one book. That is an expected relationship.
  4. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Relate products and categories

Not started

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.

Key takeaways

  • INNER JOIN keeps the matching pairs.
  • ON states the relationship.
  • Aliases show which table each column comes from.

Next: Keep all rows with LEFT JOIN