SQL RIGHT JOIN

RIGHT JOIN (or RIGHT OUTER JOIN) returns all rows from the right table and the matching rows from the left table. If there is no match, the left table columns will contain NULL.

Syntax

Syntax
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;

table1 → Left table
table2 → Right table (all rows kept)

Example:

SELECT Customers.name, Orders.order_id
FROM Customers
RIGHT JOIN Orders
ON Customers.customer_id = Orders.customer_id;

Visual Concept

LEFT TABLE (Customers)      RIGHT TABLE (Orders)

RIGHT JOIN RESULT:
All Orders + matching Customers


Important Note:
Many developers avoid RIGHT JOIN and instead rewrite it as LEFT JOIN by swapping table positions because it is easier to read.


Topics