Joining a table to itself
Pair up books by the same author or in the same series.
Join the table to itself, giving each copy a short name like a and b.
Show every pair of books in the same series, once each, older book first: first and second.
| book_id | title | author | genre | year | pages | series |
|---|---|---|---|---|---|---|
| 1 | Frankenstein | Mary Shelley | Horror | 1818 | 280 | NULL |
| 2 | Angels & Demons | Dan Brown | Thriller | 2000 | 616 | Robert Langdon |
| 3 | The Time Machine | H.G. Wells | Science Fiction | 1895 | 118 | NULL |
| 4 | Pride and Prejudice | Jane Austen | Romance | 1813 | 432 | NULL |
| 5 | Charlotte's Web | E.B. White | Children | 1952 | 184 | NULL |
| 6 | Catching Fire | Suzanne Collins | Science Fiction | 2009 | 391 | The Hunger Games |
| 7 | The Fellowship of the Ring | J.R.R. Tolkien | Fantasy | 1954 | 423 | The Lord of the Rings |
| 8 | The Fault in Our Stars | John Green | Romance | 2012 | 313 | NULL |
| 9 | The Hunger Games | Suzanne Collins | Science Fiction | 2008 | 374 | The Hunger Games |
| 10 | Mockingjay | Suzanne Collins | Science Fiction | 2010 | 390 | The Hunger Games |
Run your query to see the result table.
Press the green Run button or Ctrl+Enter.
How JOIN works
ON a.author = b.author pairs every book with every book by the same author, itself included, and each pair both ways round.
books a
| title | author | year |
|---|---|---|
| Pride and Prejudice | Jane Austen | 1813 |
| Emma | Jane Austen | 1815 |
| Dracula | Bram Stoker | 1897 |
| Carrie | Stephen King | 1974 |
| The Shining | Stephen King | 1977 |
books b
| title | author | year |
|---|---|---|
| Pride and Prejudice | Jane Austen | 1813 |
| Emma | Jane Austen | 1815 |
| Dracula | Bram Stoker | 1897 |
| Carrie | Stephen King | 1974 |
| The Shining | Stephen King | 1977 |
query
SELECT a.title AS first, b.title AS second FROM books a JOIN books b ON a.author = b.author;
result: 9 rows
| first | second |
|---|---|
| Pride and Prejudice | Pride and Prejudice (itself) |
| Pride and Prejudice | Emma |
| Emma | Pride and Prejudice (again, other way) |
| Emma | Emma (itself) |
| Dracula | Dracula (itself) |
| Carrie | Carrie (itself) |
| Carrie | The Shining |
| The Shining | Carrie (again, other way) |
| The Shining | The Shining (itself) |
What is a self-join?
A table joined to itself:
FROM books a JOIN books b. It compares rows of the same table with each other, such as books by the same author.Why the short names?
Both copies are called books, so each needs its own name (an alias). Then
a.titleandb.titlesay which copy you mean.Why does a book pair with itself?
A book has the same author as itself, so
a.author = b.authormatches it. And Emma pairs with Pride and Prejudice both ways round.How do I keep each pair once?
Add
AND a.year < b.year(ora.book_id < b.book_id). Only one order passes, and a row is never less than itself.How is this like Excel?
There's no easy Excel version: you'd need a lookup of the table against itself, or a PivotTable with author in Rows and title twice.
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.
| Database | How | Note |
|---|---|---|
| SQLite (these lessons) | books AS a or books a | |
| PostgreSQL | books AS a or books a | |
| MySQL / MariaDB | books AS a or books a | |
| SQL Server | books AS a or books a | |
| Oracle | books a | No AS before a table name: books AS a is an error. Column names can have AS. |
| Sybase ASE | books a | AS before a table name is not accepted in older versions; leave it out. |
Know someone fighting with a spreadsheet? Send them this page.