Skip to content

Latest commit

Β 

History

History
124 lines (84 loc) Β· 4.87 KB

File metadata and controls

124 lines (84 loc) Β· 4.87 KB

➑️ Topic 10.4: RIGHT JOIN (The Mirror of LEFT JOIN)

RIGHT JOIN is the logical mirror image of LEFT JOIN. Instead of keeping all rows from the left table, it keeps all rows from the right table, filling NULL where the left table has no match.


1. Definition

RIGHT JOIN (RIGHT OUTER JOIN): Returns all rows from the right table (the table after the RIGHT JOIN keyword), and the matching rows from the left table. If there is no match in the left table, the left columns appear as NULL.


2. Why This Concept Exists

RIGHT JOIN exists for symmetry β€” to allow developers to choose which table is the "master" without being forced to reorder their FROM clause. In practice, any RIGHT JOIN can be rewritten as a LEFT JOIN by swapping the table order.


3. The Key Mathematical Relationship

A RIGHT JOIN B is always interchangeable with B LEFT JOIN A.

-- βœ… Using RIGHT JOIN
SELECT c.Name, o.Order_ID
FROM Customers c
RIGHT JOIN Orders o ON c.Customer_ID = o.Customer_ID;

-- βœ… Exactly equivalent using LEFT JOIN (just swap the tables):
SELECT c.Name, o.Order_ID
FROM Orders o
LEFT JOIN Customers c ON c.Customer_ID = o.Customer_ID;

Both queries return identical results. Which one to use is a matter of readability.


4. How It Works (NULL Padding in Reverse β€” PRO LEVEL)

The process is identical to LEFT JOIN but from the other direction:

  1. Read each row from the right table.
  2. Look for a matching row in the left table.
  3. If a match is found β†’ combine both rows.
  4. If NO match is found β†’ return the right-table row with NULL for all left-table columns.

Use Case: When the right table is your "master" list and you want to find orphaned records in the left table.


5. Syntax / Implementation

-- Basic: Show all orders, with customer info if available
SELECT c.Customer_Name, o.Order_ID, o.Total_Amount
FROM Customers c
RIGHT JOIN Orders o ON c.Customer_ID = o.Customer_ID;
-- Orders with no matching customer (orphan orders) will have NULL for Customer_Name

-- Finding orphaned orders (orders with no valid customer)
SELECT o.Order_ID, o.Total_Amount
FROM Customers c
RIGHT JOIN Orders o ON c.Customer_ID = o.Customer_ID
WHERE c.Customer_ID IS NULL;
-- These are data integrity issues: orders linked to deleted customers

6. Real-Life Examples

Example 1 β€” Finding Orphaned Orders: After a database migration, some orders may be linked to customer IDs that no longer exist. Finding them:

SELECT o.Order_ID, o.Customer_ID, o.Created_At
FROM Customers c
RIGHT JOIN Orders o ON c.Customer_ID = o.Customer_ID
WHERE c.Customer_ID IS NULL;
-- These orders reference a Customer_ID that doesn't exist β€” data quality alert!

Example 2 β€” Audit: Products received but not in catalog:

SELECT cat.Product_Name, recv.SKU, recv.Quantity_Received
FROM Product_Catalog cat
RIGHT JOIN Received_Stock recv ON cat.SKU = recv.SKU
WHERE cat.SKU IS NULL;
-- Items received in the warehouse that aren't in our catalog system yet!

7. Why Senior Developers Rarely Use RIGHT JOIN

In professional codebases, 99% of outer joins are written as LEFT JOINs. Here's why:

  • Human reading direction: We read left-to-right. In a query, the "main" table logically comes first (left). LEFT JOIN feels natural: "Start with this table, pull in related data."
  • Debugging ease: When tracing a complex 5-table query, it's easier to reason about if all outer joins are LEFT JOINs β€” the master table is always the first one listed.
  • Team convention: Most SQL style guides (Google, Airbnb, GitLab) recommend using LEFT JOIN exclusively, converting any RIGHT JOIN by swapping table order.

8. Common Mistakes

  • Mixing LEFT and RIGHT JOINs in one query: This creates a mental model nightmare. Trace which table is dominant at each step β€” very error-prone.
  • Expecting the same behavior as LEFT JOIN without swapping tables: A RIGHT JOIN B is NOT the same as A LEFT JOIN B. Only B LEFT JOIN A is equivalent.

9. Tips & Best Practices

  • House rule: If you find yourself writing a RIGHT JOIN, try rewriting it as a LEFT JOIN by swapping the table order. Your queries will be easier to read and maintain.
  • Use RIGHT JOIN only when the right table is established much later in a query context and swapping would require restructuring many other clauses.

10. Mini Practice Tasks

  • Task 1: Rewrite this RIGHT JOIN query using a LEFT JOIN that returns the same results: SELECT * FROM Employees e RIGHT JOIN Departments d ON e.Dept_ID = d.Dept_ID
  • Task 2: Using a RIGHT JOIN (or equivalent LEFT JOIN), find all Courses that have no students enrolled in them. Tables: Students(ID, Course_ID), Courses(Course_ID, Course_Name).
  • Task 3: Why do most professional SQL style guides recommend using LEFT JOIN over RIGHT JOIN?