Skip to main content

RIGHT JOIN

The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the left table (table1). If there is no match, the result is NULL on the left side.

Syntax

Below is the common syntax used for writing a RIGHT JOIN:

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

Example

Consider two tables:

Table "Orders":

OrderIDCustomerIDOrderDate
132017/11/11
212017/10/23
322017/9/15
442017/9/03

Table "Customers":

CustomerIDCustomerNameContactNameCountry
1Alfreds FutterkisteMaria AndersGermany
2Ana Trujillo Emparedados y heladosAna TrujilloMexico
3Antonio Moreno TaqueríaAntonio MorenoMexico
5Berglunds snabbköpChristina BerglundSweden

Now, we want to select all customers and any matching records in orders table. If there is no match, the result is null in order table:

SELECT 
Customers.CustomerName,
Orders.OrderID
FROM
Orders
RIGHT JOIN
Customers
ON
Orders.CustomerID = Customers.CustomerID;

Result:

CustomerNameOrderID
Alfreds Futterkiste2
Ana Trujillo Emparedados y helados3
Antonio Moreno Taquería1
Berglunds snabbköpNULL
Around the HornNULL
Bottom-Dollar MarketsNULL

As you can see, the RIGHT JOIN keyword returned all the records from the Customers table and all matched records from the Orders table. For those customers who have no orders (like "Berglunds snabbköp"), the result is NULL.