INNER JOIN pairs each row of one table with the rows of another that match it, and keeps only the pairs it finds. A row with no partner on the other side is left out.
SELECT columnsFROM table1INNER JOIN table2 ON table1.column = table2.column;The examples use the tables from the example database. SQL Joins introduces joins in general.
Two tables
Section titled “Two tables”To show each crew member with the title of the production they work on:
SELECT Crew.Name, Productions.TitleFROM CrewINNER JOIN Productions ON Crew.ProductionID = Productions.ProductionID;| Name | Title |
|---|---|
| Maya Okafor | The Lantern Keepers |
| Tom Hale | The Lantern Keepers |
| Priya Nair | The Lantern Keepers |
| Owen Price | The Lantern Keepers |
| Lucy Brennan | Quarry Lane |
| Sam Ellis | Quarry Lane |
The three productions with no crew listed, Saltmarsh, Six Bells and Fennick’s Return, do not appear. That is the defining behaviour of an inner join; a LEFT JOIN would keep them.
JOIN on its own means INNER JOIN, so the word INNER is optional.
Three tables
Section titled “Three tables”Joins chain: each INNER JOIN adds one more table. To show each invoice with its production and the production accountant to send queries to:
SELECT Invoices.InvoiceID, Productions.Title, Crew.Name AS AccountantFROM InvoicesINNER JOIN Productions ON Invoices.ProductionID = Productions.ProductionIDINNER JOIN Crew ON Crew.ProductionID = Productions.ProductionID AND Crew.Role = 'Production Accountant'ORDER BY Invoices.InvoiceID;| InvoiceID | Title | Accountant |
|---|---|---|
| 1001 | The Lantern Keepers | Tom Hale |
| 1002 | Quarry Lane | Lucy Brennan |
| 1003 | The Lantern Keepers | Tom Hale |
Invoice 1004 is missing. It belongs to Six Bells, which has no production accountant in the Crew table, so the second join finds no partner and drops the row. When a joined result is shorter than you expect, an inner join dropping unmatched rows is the usual reason.