Difference Between LEFT JOIN and RIGHT JOIN in MySQL

Understanding the difference between a LEFT JOIN and a RIGHT JOIN in MySQL is fundamental for retrieving data accurately from multiple tables. While both are outer joins used to combine rows based on a related column, they differ in which table is treated as the primary source of truth. This article explains the behavioral differences between these two joins, how they handle unmatched data, and how to apply them in your SQL queries.

The Core Difference

The primary distinction between LEFT JOIN and RIGHT JOIN lies in the direction of the table dependency.

How They Behave in Practice

To visualize the behavior, consider two database tables: Customers (left table) and Orders (right table).

Using LEFT JOIN

SELECT Customers.customer_name, Orders.order_id
FROM Customers
LEFT JOIN Orders ON Customers.customer_id = Orders.customer_id;

Using RIGHT JOIN

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

Syntactic Equivalence

From a functional standpoint, any RIGHT JOIN can be rewritten as a LEFT JOIN simply by reversing the order of the tables in the SQL statement.

The following two queries yield identical results:

/* Query A */
SELECT * FROM Table_A LEFT JOIN Table_B ON Table_A.id = Table_B.id;

/* Query B (Equivalent to Query A) */
SELECT * FROM Table_B RIGHT JOIN Table_A ON Table_B.id = Table_A.id;

Which One Should You Use?

In the database development community, LEFT JOIN is almost universally preferred over RIGHT JOIN.

Because Western languages are read from left to right, structuring queries using LEFT JOIN allows developers to easily follow the logic of starting with a primary table and bringing in optional, supplementary data. Furthermore, because the MySQL query optimizer automatically translates RIGHT JOIN queries into equivalent LEFT JOIN structures under the hood, there is no performance benefit to choosing one over the other. Stick to LEFT JOIN for consistency and readability.