RIGHT JOIN is the mirror image of LEFT JOIN. It keeps every row of the second table, the one on the right, and fills the first table’s columns with NULL where nothing matches.
SELECT columnsFROM table1RIGHT JOIN table2 ON table1.column = table2.column;RIGHT OUTER JOIN is another name for the same thing.
The examples use the Productions and Invoices tables from the example database.
Every production, from the right
Section titled “Every production, from the right”With Productions on the right, every production is kept, whether or not it has invoices:
SELECT Invoices.InvoiceID, Productions.TitleFROM InvoicesRIGHT JOIN Productions ON Invoices.ProductionID = Productions.ProductionIDORDER BY Productions.Title;| InvoiceID | Title |
|---|---|
| Fennick’s Return | |
| 1002 | Quarry Lane |
| Saltmarsh | |
| 1004 | Six Bells |
| 1001 | The Lantern Keepers |
| 1003 | The Lantern Keepers |
These are the same rows that Productions LEFT JOIN Invoices returns. Any right join can be written as a left join by swapping the two tables, and most people do exactly that, because a query that always keeps “the table I started from” is easier to follow.