The SQL RIGHT JOIN, also known as RIGHT OUTER JOIN, retrieves all records from the right table (table2) and the matched records from the left table (table1). If there is no match, the result set will contain NULL values for the columns from the left table.
Consider two tables, Orders and Customers, with columns as follows:
- Orders: OrderID, CustomerID,
OrderDate
- Customers: CustomerID,
CustomerName, City
We want to retrieve all customers along with their orders, including
customers without any orders.
SELECT Orders.OrderID,
Orders.OrderDate, Customers.CustomerName, Customers.City FROM Orders RIGHT JOIN Customers ON
Orders.CustomerID = Customers.CustomerID;
In this example, the RIGHT JOIN retrieves all records from the Customers table, and only matching records from
the Orders table based on the CustomerID. If there is no match for a customer
in the Orders table, NULL values
will be returned for OrderID and OrderDate. This ensures that all customers are included in the result set,
regardless of whether they have any corresponding orders.
Comments
Post a Comment