JOIN

Joining a table to itself

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

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.

The database
10 rows
book_idtitleauthorgenreyearpagesseries
1FrankensteinMary ShelleyHorror1818280NULL
2Angels & DemonsDan BrownThriller2000616Robert Langdon
3The Time MachineH.G. WellsScience Fiction1895118NULL
4Pride and PrejudiceJane AustenRomance1813432NULL
5Charlotte's WebE.B. WhiteChildren1952184NULL
6Catching FireSuzanne CollinsScience Fiction2009391The Hunger Games
7The Fellowship of the RingJ.R.R. TolkienFantasy1954423The Lord of the Rings
8The Fault in Our StarsJohn GreenRomance2012313NULL
9The Hunger GamesSuzanne CollinsScience Fiction2008374The Hunger Games
10MockingjaySuzanne CollinsScience Fiction2010390The Hunger Games
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 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

titleauthoryear
Pride and PrejudiceJane Austen1813
EmmaJane Austen1815
DraculaBram Stoker1897
CarrieStephen King1974
The ShiningStephen King1977

books b

titleauthoryear
Pride and PrejudiceJane Austen1813
EmmaJane Austen1815
DraculaBram Stoker1897
CarrieStephen King1974
The ShiningStephen King1977

query

SELECT a.title AS first, b.title AS second
FROM books a JOIN books b
  ON a.author = b.author;

result: 9 rows

firstsecond
Pride and PrejudicePride and Prejudice (itself)
Pride and PrejudiceEmma
EmmaPride and Prejudice (again, other way)
EmmaEmma (itself)
DraculaDracula (itself)
CarrieCarrie (itself)
CarrieThe Shining
The ShiningCarrie (again, other way)
The ShiningThe Shining (itself)
Recap: every JOIN side by side
  • 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.title and b.title say which copy you mean.

  • Why does a book pair with itself?

    A book has the same author as itself, so a.author = b.author matches it. And Emma pairs with Pride and Prejudice both ways round.

  • How do I keep each pair once?

    Add AND a.year < b.year (or a.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.

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.

Naming a table: books AS a
DatabaseHowNote
SQLite (these lessons)books AS a or books a
PostgreSQLbooks AS a or books a
MySQL / MariaDBbooks AS a or books a
SQL Serverbooks AS a or books a
Oraclebooks aNo AS before a table name: books AS a is an error. Column names can have AS.
Sybase ASEbooks aAS before a table name is not accepted in older versions; leave it out.

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