Try OpenEdge Now
skip to main content
Programming Interfaces
Data Management : Database Access : Joining tables : Using left outer joins
 
Using left outer joins
A left outer join is useful where you want to see all the data on the left side, whether or not there is related data on the right. For example, you might want to see the proportion of Customers who are ordering close to their credit limit as against those who are not.
The query in i-join2.p is identical to the one in i-join1.p (see Using inner joins) except that all the joins are left outer joins instead of inner joins.
i-join2.p
DEFINE QUERY q1 FOR Customer, Order, OrderLine, Item.
DEFINE BROWSE b1 QUERY q1
  DISPLAY Customer.Name Customer.CreditLimit Order.OrderNum Item.ItemName
  WITH 10 DOWN.

OPEN QUERY q1 PRESELECT EACH Customer,
/*1*/ EACH Order OUTER-JOIN OF Customer,
/*2*/ EACH OrderLine OUTER-JOIN OF Order
WHERE (OrderLine.Price * OrderLine.Qty) >
(.667 * Customer.CreditLimit),
/*3*/ EACH Item OUTER-JOIN OF OrderLine.

ENABLE b1 WITH SIZE 68 BY 10.
WAIT-FOR WINDOW-CLOSE OF CURRENT-WINDOW.
Thus, you see all Customers, whether or not they order close to their credit limit. These three items correspond to the /*1*/,/*2*/, and /*3*/ that label the joins in the i-join2.p example:
1. To each Customer, join all related Order records, or join a null Order record if the Customer has no orders.
2. To each Order in the previous join, join each related OrderLine record whose total purchase is greater than two–thirds the Customer's credit limit, or join a null OrderLine record if all item purchases are less than or equal to two–thirds the Customer's credit limit. Also, join a null OrderLine record if the order is null (the Customer has no orders).
3. To each OrderLine in the previous join, join the related Item record to get the item name, or join a null Item record if the OrderLine is null (no Order or selected purchase for that Customer).
When executed, you get the output shown in the following figure. In this example, you see the same golf club order as in the Figure 5 figure along with many other orders that do not meet the selection criteria and some Customers who have no orders at all.
Figure 6. Left outer join example