Query Joins in general connect two or more tables so that the query results present data as though it is from a single table. This is done in a coherent manner whereby corresponding records from separate tables are presented with each row of data in the query results matching. In the previous post on Inner Joins we used the example of a Customer Table and an Order Table to produce a single row of data which displayed a Customer Name with his or her Order. For a record to appear in the query results the Inner Joine required each customer (in tblCustomer) to have at least one order (in tblOrder) and vice versa. It is at this point that Inner Join and Outer Join Queries differ.
Suppose for example, we had a number of customer records with no corresponding orders in the orders table (lets say the orders were accidentally deleted at some point). If we run an Inner Join Query, those customers without order records would not show in the results. However, if we run an Left Outer Join Query all customers would appear regardless of whether or not they had corresponding records in the order table (provided, of course, the customer met any query criteria which was set). If a customer had no corresponding order records his or her query results row will just contain the customer's details with empty fields in place of order details.
This brings me to one last point: we need to specify whether the Join is a Left Join or Right Join. So what does this mean? Basically by selecting whether the Outer Join Query is left or right, we are letting Access know which table in the Join is going to display all the rows matching a given criteria, and which side will only display data corresponding to that chosen table.
Lets now have a go at creating an Outer Join Query. The exercise below uses two tables, tblCustomer and tblOrder. We are going to use the FIRSTNAME, SURNAME and CITY fields from the customer table, and ITEMORDERED and DATE from the orders table. Our query criteria will filter out all customers who live in Bolton. You can download an Access Database with these tables here, or enter them manually from the screenshots below:
|Above: Date for tblCustomer.|
Below: Data for tblOrder.
|NB There is a One to Manly Relationship between the two tables |
between tblCustomer.ID and tblOrder.CustomerId.
How to Create an Outer Join Query
We are going to do this exercise in two stages. The first stage is to create a standard Inner Join query using the Access Query Design Grid. In the second stage with will modify the query so it becomes an Outer Join Query.
Stage One - Create an Inner Join Query.
- Open the database you downloaded above.
- Select the CREATE tab on the Access Ribbon.
- Click the QUERY DESIGN icon in the OTHER group.
- Select tblCustomer and tblOrder from the SHOW TABLE dialogue box. Then close the dialogue box. Both tables should now have appeared in the top section of the QUERY DESIGN GRID. Since there is already a one to many relationship between the two tables, this is represented in the Query Design Grid as an Inner Join.
- Select fields FirstName, Surname, and City from tblCustomer. Then select fields ItemOrdered and Date from tblOrder.
- Enter the query criteria ="bolton" in the CRITERIA row of the CITY column in the lower section of the grid.
If you run the query now (before we change it to an Outer Join Query) you should get this result shown below:
|Above: the query run as an Inner Join before modification.|
You may notice that although our customer table contained two records with Bolton as the City (our criteria), only one record was displayed. This is due to the fact that we ran the query as an Inner Join and the excluded record that has no corresponding order in the tblOrder table.
Lets see what happens when we change the query to an Outer Join type:
Stage Two - Modify the Query to an Outer Join.
- Open the Inner Join query we just created in DESIGN VIEW. The QUERY DESIGN GRID opens.
- Double click the Join between the two tables to bring up the JOIN PROPERTIES dialogue box.
The JOIN PROPERTIES dialogue box.
Notice how the Join Properties box shows the Left Table Name (tblCustomer) and the Right Table Name (tblOrder). This ties in with what we discussed above regarding Left and Right Joins. We want all customers to be displayed regardless of whether they have a corresponding record in the orders table. As tblCustomer is the Left Table shown in the JOIN PROPERTIES BOX we need to modify our query to a Left Outer Join. To do this we need to click Option Two where it says "Include ALL records from 'tblCustomer' and only those records from 'tblOrder' where the joined fields are equal".
- Click Option 2 in the JOIN PROPERTIES dialogue box and click OK.
|Representation of the Left Outer Join in |
the Query Design Window.
- Opening the Query Design Window.
- Double clicking the existing Join line between the tables to open the JOIN PROPERTIES dialogue box.
- Select Option Three where it says "Include all records from 'tblOrder' and only those orders from 'tblCustomer' where the join fields are equal"
|Query results when from a Right Outer Join.|