← Back to MySQL Course | Chapter 9: Joins | Lesson 4 of 10

RIGHT JOIN

What is a RIGHT JOIN?

A RIGHT JOIN keeps all rows from the right table regardless of whether a match exists on the left. It matches them with rows from the left table where possible. If there is no match, the left side returns NULL, mirroring how LEFT JOIN treats the right side.

Example: What is a RIGHT JOIN?

sql
CREATE TABLE customers (customer_id INT, name TEXT);
CREATE TABLE orders (order_id INT, customer_id INT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO orders VALUES (101, 1), (102, 2);
SELECT customers.name, orders.order_id
FROM customers RIGHT JOIN orders ON customers.customer_id = orders.customer_id;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

RIGHT JOIN vs LEFT JOIN Equivalence

Every RIGHT JOIN can be rewritten as an equivalent LEFT JOIN. You just swap the table order in your query. Most style guides prefer sticking to LEFT JOIN everywhere for consistency, since RIGHT JOIN is used far less often and can confuse readers.

Example: RIGHT JOIN vs LEFT JOIN Equivalence

sql
CREATE TABLE customers (customer_id INT, name TEXT);
CREATE TABLE orders (order_id INT, customer_id INT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO orders VALUES (101, 1);
-- RIGHT JOIN
SELECT customers.name FROM customers RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
-- Equivalent LEFT JOIN (tables swapped)
SELECT customers.name FROM orders LEFT JOIN customers ON customers.customer_id = orders.customer_id;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Identifying Orphans with RIGHT JOIN

You can find rows in the right table that have no matching records on the left side, such as products that have never been ordered. To do this, check for left-side NULLs after the join, the mirror image of the LEFT JOIN orphan-finding pattern.

Example: Identifying Orphans with RIGHT JOIN

sql
CREATE TABLE products (product_id INT, name TEXT);
CREATE TABLE orders (order_id INT, product_id INT);
INSERT INTO products VALUES (1, 'Keyboard'), (2, 'Mouse');
INSERT INTO orders VALUES (101, 1);
SELECT products.name
FROM orders RIGHT JOIN products ON orders.product_id = products.product_id
WHERE orders.order_id IS NULL;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Right Joining Multiple Tables

You can chain multiple RIGHT JOIN statements to gather information from several tables at once. This gathers information while preserving the rightmost table's records, ensuring nothing from that final table gets dropped.

Example: Right Joining Multiple Tables

sql
CREATE TABLE categories (category_id INT, name TEXT);
CREATE TABLE products (product_id INT, category_id INT, name TEXT);
CREATE TABLE orders (order_id INT, product_id INT);
INSERT INTO categories VALUES (1, 'Electronics');
INSERT INTO products VALUES (10, 1, 'Keyboard');
INSERT INTO orders VALUES (101, 10);
SELECT categories.name, products.name, orders.order_id
FROM orders
RIGHT JOIN products ON orders.product_id = products.product_id
RIGHT JOIN categories ON products.category_id = categories.category_id;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

RIGHT JOIN Aggregate Computations

Combining a RIGHT JOIN with aggregation allows you to display total figures, including zeroes for unmatched rows, such as showing every product's sales count even for products that have never sold.

Example: RIGHT JOIN Aggregate Computations

sql
CREATE TABLE products (product_id INT, name TEXT);
CREATE TABLE orders (order_id INT, product_id INT);
INSERT INTO products VALUES (1, 'Keyboard'), (2, 'Mouse');
INSERT INTO orders VALUES (101, 1);
SELECT products.name, COUNT(orders.order_id) AS sales_count
FROM orders RIGHT JOIN products ON orders.product_id = products.product_id
GROUP BY products.name;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.