SQL Right Join
SQL RIGHT JOIN
RIGHT JOIN returns all rows from the right table and the matching rows from the left table. If there is no match, NULL values are returned for the left table columns.
Basic Syntax
SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
Example: All Orders and Their Customers
This query returns all orders (right table) and customer information when available:
SELECT c.name, o.order_date FROM customers AS c RIGHT JOIN orders AS o ON c.id = o.customer_id;
If an order references a customer that does not exist (or the join key does not match), the customer columns will be NULL.
LEFT JOIN vs RIGHT JOIN
- LEFT JOIN keeps all rows from the left table
- RIGHT JOIN keeps all rows from the right table
RIGHT JOIN as a LEFT JOIN
In many cases, you can avoid RIGHT JOIN by swapping table order and using LEFT JOIN instead. This is often preferred for readability.
-- Equivalent using LEFT JOIN SELECT c.name, o.order_date FROM orders AS o LEFT JOIN customers AS c ON c.id = o.customer_id;
NULL Results
If there is no match, the columns from the left table will be NULL.
Filtering Considerations
As with LEFT JOIN, be careful when filtering on the “optional” side. Conditions in WHERE can remove NULL rows and change the behavior.
Common Mistakes
- Confusing which table is preserved (right table is preserved in RIGHT JOIN)
- Filtering in WHERE in a way that removes NULL rows unexpectedly
- Using RIGHT JOIN when a clearer LEFT JOIN version exists
Next Step
Continue with SQL FULL JOIN to learn how to return rows when there is a match in either table.