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.
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.
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.
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.
The process is identical to LEFT JOIN but from the other direction:
- Read each row from the right table.
- Look for a matching row in the left table.
- If a match is found β combine both rows.
- If NO match is found β return the right-table row with
NULLfor 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.
-- 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 customersExample 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!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 JOINfeels 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 JOINexclusively, converting anyRIGHT JOINby swapping table order.
- 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 Bis NOT the same asA LEFT JOIN B. OnlyB LEFT JOIN Ais equivalent.
- House rule: If you find yourself writing a
RIGHT JOIN, try rewriting it as aLEFT JOINby swapping the table order. Your queries will be easier to read and maintain. - Use
RIGHT JOINonly when the right table is established much later in a query context and swapping would require restructuring many other clauses.
- Task 1: Rewrite this
RIGHT JOINquery using aLEFT JOINthat 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 allCoursesthat 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 JOINoverRIGHT JOIN?