Course contentsShow
← Data Science
Lesson 930 of 2,14524. SQL JoinsFree lesson

Converting Between LEFT and RIGHT JOINs

How LEFT and RIGHT JOINs are logically equivalent by swapping table order, and when to prefer one over the other.

Converting Between LEFT and RIGHT JOINs

You'll learn: How LEFT and RIGHT JOINs are interchangeable by simply swapping table order, and when to choose one over the other.

The Core Insight

LEFT and RIGHT JOINs are two sides of the same coin. Any LEFT JOIN can be rewritten as a RIGHT JOIN (and vice versa) by reversing the table order. They're logically equivalent—just viewed from different angles.

Think of it like giving directions: "The library is to the left of the park" means exactly the same as "The park is to the right of the library." Same relationship, different perspective.

How the Conversion Works

When you have:

SELECT *
FROM customers
LEFT JOIN orders ON customers.id = orders.customer_id;

This returns all customers, even those without orders. You can rewrite it as:

SELECT *
FROM orders
RIGHT JOIN customers ON customers.id = orders.customer_id;

The result? Identical. You've simply swapped which table comes first and flipped LEFT to RIGHT.

When to Prefer LEFT JOIN

In practice, LEFT JOIN is far more common in the SQL community. Most developers:

  • Write queries with the "main" or "base" table first
  • Find LEFT JOIN more intuitive to read
  • Avoid RIGHT JOIN to maintain consistency across codebases

Use LEFT JOIN when you want all records from your starting table, regardless of matches.

When RIGHT JOIN Appears

RIGHT JOIN occasionally surfaces when:

  • Modifying legacy queries where repositioning tables is risky
  • Following strict organizational style guides
  • The logical flow reads more naturally right-to-left (rare)

Key Takeaway: LEFT and RIGHT JOINs are interchangeable through table reordering; standardize on LEFT JOIN for clarity and consistency unless you have a specific reason to do otherwise.