Right Join

A RIGHT JOIN (or RIGHT OUTER JOIN) returns all rows from the right table, along with matching rows from the left table - if there's no match, the left table's columns are filled with NULL. It is the mirror image of a LEFT JOIN.

Basic Syntax

SELECT c.first_name, o.amount
FROM customers c
RIGHT JOIN orders o ON c.customer_id = o.customer_id;

This returns every order, even if (due to a data issue) an order somehow has no matching customer - in that case, c.first_name would be NULL.

Right Join vs Left Join

A RIGHT JOIN can always be rewritten as a LEFT JOIN by simply swapping the table order:

-- These two produce the same result
SELECT c.first_name, o.amount
FROM customers c RIGHT JOIN orders o ON c.customer_id = o.customer_id;

SELECT c.first_name, o.amount
FROM orders o LEFT JOIN customers c ON c.customer_id = o.customer_id;
Because a RIGHT JOIN can always be expressed as a LEFT JOIN, many teams standardize on LEFT JOIN for consistency and readability - RIGHT JOIN is less commonly used in practice.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course