RIGHT (OUTER) JOIN

The SQL RIGHT JOIN

RIGHT JOIN returns all records from the right table (table2), and the matching records from the left table (table1). Unmatched left-side columns are NULL.

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
  ON table1.column_name = table2.column_name;

Example

Example

All employees and the orders they handled, including employees with no orders:

SELECT o.OrderID, e.FirstName, e.Department
FROM Orders o
RIGHT JOIN Employees e ON o.EmployeeID = e.EmployeeID
ORDER BY e.EmployeeID;

Result: 11 rows

OrderIDFirstNameDepartment
NULLRahulSales
101SaraSales
103SaraSales
106SaraSales
108SaraSales
102DavidSales
104DavidSales
107DavidSales
NULLAnanyaSupport
NULLJamesSupport
NULLFatimaFinance

Note: The code runner uses an older SQLite (3.27) that does not support RIGHT or FULL joins, so Run on this example fails there. The result shown is from SQLite 3.39+, which gives the same answer as PostgreSQL and SQL Server.

Support

DatabaseRIGHT JOIN
MySQL, PostgreSQL, SQL Server, Oraclesupported
SQLitefrom version 3.39 (2022)

Any RIGHT JOIN Can Be a LEFT JOIN

A RIGHT JOIN B returns exactly the same rows as B LEFT JOIN A. Only the column order differs, and you control that in SELECT.

Example

The previous example written as a LEFT JOIN. This one runs in the code runner:

SELECT o.OrderID, e.FirstName, e.Department
FROM Employees e
LEFT JOIN Orders o ON o.EmployeeID = e.EmployeeID
ORDER BY e.EmployeeID;

Result: 11 rows

OrderIDFirstNameDepartment
NULLRahulSales
101SaraSales
103SaraSales
106SaraSales
108SaraSales
102DavidSales
104DavidSales
107DavidSales
NULLAnanyaSupport
NULLJamesSupport
NULLFatimaFinance

Which Should You Use?

Most teams use LEFT JOIN only. Queries read left to right ("start from the table whose rows you want to keep"), and mixing LEFT and RIGHT in one query is hard to follow.

Tip: In interviews, you may be asked to explain that RIGHT JOIN exists mainly for symmetry: it never lets you write a query a LEFT JOIN cannot.