- This topic is empty.
-
AuthorPosts
-
March 7, 2026 at 2:01 pm #6181

While learning SQL joins, many students notice that INNER JOIN behaves symmetrically — meaning the order of tables does not affect the result.
This raises an interesting question:
If we swap the tables in a LEFT JOIN and replace it with a RIGHT JOIN, will the result stay the same?
The short answer is yes, in many cases it will. Understanding why helps build a deeper understanding of how joins actually work.
Understanding LEFT JOIN
A LEFT JOIN guarantees that all rows from the left table appear in the result.
Example:
SELECT districts.name, expenditures.pupils FROM districts LEFT JOIN expenditures ON districts.id = expenditures.district_id;Meaning:
Return all districts. If a district has no matching expenditure record, the expenditure columns will be NULL.Example result:
district pupils Cambridge 1200 Boston 3000 Lowell NULL Lowell appears even though it has no matching row in the expenditures table.
Swapping Tables with RIGHT JOIN
Now consider this query:
SELECT districts.name, expenditures.pupils FROM expenditures RIGHT JOIN districts ON districts.id = expenditures.district_id;Here the join type changed from LEFT JOIN to RIGHT JOIN, and the tables were swapped.
What does this query mean?
Return all rows from the RIGHT table (districts). If a district has no matching expenditure record, the expenditure values will be NULL.Result:
district pupils Cambridge 1200 Boston 3000 Lowell NULL The output is the same as the previous query.
Why the Result Is the Same
The key idea is which table’s rows must always appear.
Query Table Guaranteed to Appear districts LEFT JOIN expenditures districts expenditures RIGHT JOIN districts districts Both queries preserve the districts table.
Because the preserved table is the same, the final result is the same.
A General Transformation Rule
You can rewrite:
A LEFT JOIN Bas
B RIGHT JOIN Aas long as the join condition remains the same.
Example transformation:
Original query:
SELECT * FROM A LEFT JOIN B ON A.id = B.a_id;Equivalent query:
SELECT * FROM B RIGHT JOIN A ON A.id = B.a_id;Both queries return the same rows.
Why Many Developers Avoid RIGHT JOIN
In practice, most SQL developers prefer using LEFT JOIN instead of RIGHT JOIN.
Instead of writing:
FROM expenditures RIGHT JOIN districtsthey usually write:
FROM districts LEFT JOIN expendituresReasons include:
- Queries read more naturally from left to right
- It reduces cognitive complexity
- Many SQL style guides recommend using LEFT JOIN consistently
Because of this, RIGHT JOIN appears less often in real-world SQL code.
Key Takeaways
- A LEFT JOIN keeps all rows from the left table.
- A RIGHT JOIN keeps all rows from the right table.
- Swapping the tables and switching join type preserves the same table’s rows.
- Therefore:
A LEFT JOIN B ≡ B RIGHT JOIN A- In practice, developers often prefer LEFT JOIN for readability.
Understanding this equivalence helps demystify SQL joins and strengthens your ability to reason about how relational databases combine data across tables.
-
AuthorPosts
- You must be logged in to reply to this topic.

