OUTER JOIN - LEFT
There may be times when you want to return rows from one table even if there are no matching rows.
Selects every records from the first table
This is often abbreviated and just called a left join.
For this situation we use an outer join.
With an outer join, you get all the rows that satisfy the join conditions plus all the rows from the other table.
This may leave some of the field entries blank, or "Null."
Values in the column in one table must be equal to the column in the other table (relationship uses '=' operator). It frequently involves primary and foreign key components)
A left outer join is where all the rows in the "one" table (in the "one-to-many" relationship) are returned and only matching rows from the "many" table.
You can create a left outer join by using the keywords OUTER LEFT JOIN and ON keywords
This works in both Access and SQL Server
This is an example joining the EMPLOYEES table and the SUPPLIERS table
SELECT Employees.LastName
,Suppliers.CompanyName
FROM EMPLOYEES
LEFT OUTER JOIN SUPPLIERS
ON Employees.City = Suppliers.City
WHERE Employees.LastName IS NOT NULL
This is an example joining the CUSTOMERS table and the ORDERS table
SELECT Customers.CustomerID
,Customers.CompanyName
,Orders.OrderID
FROM CUSTOMERS
LEFT OUTER JOIN ORDERS
ON Customers.CustomerID = Orders.CustomerID
ORDER BY OrderID
Also referred to as a Left Join.
A left outer join is where all the rows in the "one" table (in the "one-to-many" relationship) are returned and only matching rows from the "many" table.
The OUTER keyword is optional
This displays all Customer Names irrespective of whether they have any orders. Everything from the Customers table.
SELECT Customers.CompanyName,
Orders.OrderID
FROM Customers
LEFT OUTER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
ORDER BY OrderID
Alternative (not supported in Access)
SELECT Customers.CompanyName,
Orders.OrderID
FROM Customers,
Orders
WHERE Customers.CustomerID = Orders.CustomerID(+)
ORDER BY OrderID
You can use LEFT JOIN instead of LEFT OUTER JOIN and it will have the same effect.
SELECT Employees.LastName
,Suppliers.CompanyName
FROM EMPLOYEES
LEFT JOIN SUPPLIERS
ON Employees.City = Suppliers.City
WHERE Employees.LastName IS NOT NULL
WHERE Alternative
This is the plus sign operator (+) and is used to do an outer join and also to see the rows that do not usually meet the join condition
This is not supported in Access
SELECT Customers.CustomerID
,Customers.CompanyName
,Orders.OrderID
FROM CUSTOMERS
,ORDERS
WHERE Customers.CustomerID = Orders.CustomerID(+)
ORDER BY OrderID
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext