Inner Join Icon

Inner Join

Left Outer Join Icon

Left Join

Right Outer Join Icon

Right Outer Join

Full Outer Join Icon

Full Outer Join

Cross Join

Cross Join

Self Join Icon

Self Join

Anti Join Icon

Anti Join

Right Join

Left Join Icon

A RIGHT JOIN

returns all records from the right table and the matching records from the left table. If there are no matches in the left table, the result will contain NULL values for columns from the left table. This join ensures that all data from the right table is retained, even if no corresponding rows exist in the left table.

Task

Retrieve a list of all orders along with the customers who placed them. If an order has no matching customer (e.g., due to missing or incorrect customer data), the customer information will appear as NULL. This will help identify: All orders, even those without valid customer information and Potential data issues, such as orders with missing or unmatched customers.


Tables: Customers and Orders

CustomerIDCustomerName
1Angel
2Skyler
3Jamie
4Riley
OrderIDCustomerIDAmount
1011150
1022200
1031350
1043400
10599500

How Relationships Work in This Example

In this example, we are working with three related tables: Customers and Order. The relationships between these tables help us understand how data flows and how RIGHT JOIN ensures that all orders are included, even if there are missing customer records.

Customers Table (Left Table):

  • Each customer has a unique CustomerID.
  • Not all customers may place orders, but since this is a RIGHT JOIN, customers without orders won't appear unless explicitly matched to an order.

Orders Table (Right Table for the First Join):

  • Each order is linked to a customer through the CustomerID
  • Since this is a RIGHT JOIN, all orders are included in the result, even those that don't have a matching customer.
  • Example: OrderID 105 has a CustomerID that doesn't exist in the Customers table, so the result will show NULL for the CustomerName field.

SQL Query for Inner Join:

         
    SELECT
        Customers.CustomerName,
        Orders.OrderID,
        Orders.Amount AS OrderAmount,
    FROM
        Customers
    RIGHT JOIN 
        Orders ON Customers.CustomerID = Orders.CustomerID;
   
                

Why This Query is Useful

  • Identifies orders without valid customer data by showing NULL in the CustomerName field, helping detect missing or incorrect customer records.
  • Ensures all orders are accounted for, even if some don’t have matching customers, which is useful for auditing and tracking.
  • Helps maintain data integrity by highlighting potential data issues, such as unassigned or orphaned orders, allowing for corrections.
Result Table

Query Output: Customer Orders

CustomerName OrderID OrderAmount
Angel 101 150
Angel 103 350
Skyler 102 200
Jamie 104 400
NULL 105 500

Why is there a NULL for CustomerName?

In a RIGHT JOIN, all rows from the right table (Orders) are included, even if there’s no Customers table (as in the case of OrderID 105 with CustomerID = 99), the CustomerName field will be NULL because no corresponding customer exists.

  • Order 105 has a CustomerID of 99, which doesn't exist in the Customers table.
  • Since there’s no match for CustomerID = 99, the RIGHT JOIN keeps the order but fills the CustomerName field with NULL to indicate that the customer information is missing.

Explanation

In this example, we used a RIGHT JOIN to retrieve all orders along with their corresponding customer information. Since the Orders table is the right table in the join, every order is included in the result, even if there is no matching customer in the Customers table. This example highlights how a RIGHT JOIN ensures all orders are included, even if customer information is incomplete or missing.

When to Use Right Join

Use a RIGHT JOIN when you need to ensure that all records from the right table are included in the result, even if there are no matching rows in the left table. This is useful for tracking incomplete relationships (e.g., orders without valid customers), auditing data to identify missing or orphaned records, and prioritizing the right table’s data for analysis, while accepting NULL values for unmatched entries from the left table.

Conclusion

A RIGHT JOIN ensures that all records from the right table are included in the result, with NULL values for any unmatched data from the left table. This makes it useful for scenarios where tracking, auditing, or prioritizing the right table’s data is essential, even if some relationships are incomplete or missing.