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