JOIN

INNER JOIN

SQLite in your browser
Lesson concept What you'll do
1 / 5

Show each loan with the title or the member it belongs to.

JOIN another table ON the column the two share, like a lookup.

The database
9 rows
Click a name to type it
The database
5 rows
Click a name to type it
Your query
 
Hint: click the green edit button on the board, or type it yourself
Query output Ready to run

Run your query to see the result table.

Press the green Run button or Ctrl+Enter.

Study notes

How JOIN, ON works

JOIN (INNER) keeps only the rows that match in both tables.

members

member_idname
101Ava
102Ben
103Cara
104Dev

loans

loan_idmember_idloan_date
11012026-01-12
21032026-02-03
31012026-03-20
41022026-04-08
5105(deleted)2026-05-14
membersloans

query

SELECT members.name, loans.loan_date
FROM members JOIN loans
  ON members.member_id = loans.member_id;

result: 4 rows

nameloan_date
Ava2026-01-12
Ava2026-03-20
Ben2026-04-08
Cara2026-02-03

Keeps only rows that match in both tables. Dev (no loans) and loan 5 (no member) are left out.

Recap: every JOIN side by side
  • Why is the data in two tables?

    So each fact is stored once. A loan only holds the book's id; the title lives in books. Change a title once and every loan shows the new one.

  • What does JOIN do?

    FROM loans JOIN books ON loans.book_id = books.book_id

    It pairs each loan with the book whose id matches, so one row has columns from both tables.

  • Why write loans.book_id?

    Both tables have a book_id. Writing the table name first says which one you mean. Without it, SQL says the name is ambiguous.

  • What happens to rows with no match?

    JOIN (also written INNER JOIN) keeps only rows that match in both tables. A book nobody has borrowed is left out. The next lesson keeps them.

  • How is this like Excel?

    It's XLOOKUP for every row at once: look up each loan's book_id in the books table and bring back the title.

Other databases

How other databases do it

The lessons run on SQLite. Most of SQL is the same everywhere; these are the places where this lesson's SQL differs.

The older way to write a join
DatabaseHowNote
SQLite (these lessons)FROM loans, books WHERE loans.book_id = books.book_idWorks everywhere, but JOIN ... ON keeps the link apart from the filters.
PostgreSQLFROM loans, books WHERE ...Works. JOIN ... ON is the usual style.
MySQL / MariaDBFROM loans, books WHERE ...Works. JOIN ... ON is the usual style.
SQL ServerFROM loans, books WHERE ...Works. JOIN ... ON is the usual style.
OracleFROM loans, books WHERE ...Common in older Oracle code; JOIN ... ON works since Oracle 9i.
Sybase ASEFROM loans, books WHERE ...Common in older Sybase code; JOIN ... ON works since ASE 12.

Know someone fighting with a spreadsheet? Send them this page.