Skip to content

SQL INNER JOIN Keyword

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 columns
FROM table1
INNER JOIN table2 ON table1.column = table2.column;

INNER JOIN: only the rows that match in both tables

The examples use the tables from the example database. SQL Joins introduces joins in general.

To show each crew member with the title of the production they work on:

SELECT Crew.Name, Productions.Title
FROM Crew
INNER 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.

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 Accountant
FROM Invoices
INNER JOIN Productions ON Invoices.ProductionID = Productions.ProductionID
INNER 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.