Combine Left and Right Join

Posted on 2006-11-17
Last Modified: 2008-02-26
Hello Experts,

I need some help with a query.   My db design currently contains 3 levels of entities (following principle of grandparent, parent, child).

#1. "GrandParent" and "Parent" are linked via Junction Table
#2. "Parent" and "Grandchild" are linked via Junction Table

In most cases, I have "complete records" where all 3 levels are linked top to bottom.   However, in some instances, I may only have records of "scenario" #1... while in other cases I only have those of #2.

To find the missing Grandparent, I use a Left join query... starting from the parent level.
To find the missing Grandchild, I use a Right join query... also starting the the parent level.

Not sure if it's possible, but I'd like to combine both Right and Left join query in a single query (vs. using two queries like right now).

I tried the UNION but that doesn't seem to work.   Hopefully I provided enough info in order to get some general ideas as to how I can accomplish it.


Question by:ExpExchHelp
  • 3
  • 2
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17965008
yes, both left and right join are combinable. however, I find the usage of right join difficult to handle.

anyhow   A LEFT JOIN B  is the same as B RIGHT JOIN A
LVL 12

Expert Comment

ID: 17965236
To summarize the syntax, it would be something like

select field_names FROM (ParentTable LEFT JOIN GrandChildTable ON ParentTable.PK_Field = GrandChildTable.FK_Field) LEFT JOIN GrandParentTable ON ParentTable.FK_Field = GrandParentTable.PK_Field

I have just noticed that you use junction tables (assuming n to n relationships between tables?). So that would make it just slightly more complicated:

select field_names FROM (ParentTable LEFT JOIN (JunctionTableParentGrandChild INNER JOIN GrandChildTable ON JunctionTableGrandChild.GrandChildPK_Field = GrandChildTable.PK_Field) ON ParentTable.PK_Field = JunctionTableParentGrandChild.ParentPK_Field)
   LEFT JOIN  (JunctionTableGrandParent INNER JOIN GrandParentTable ON JunctionTableGrandParent.GrandParentPK_Field =  GrandParentTable.PK_Field) ON ParentTable.PK_Field = JunctionTableGrandParent.ParentPK_Field


Author Comment

ID: 17967326
Okay, I kinda expected that it might be challenging to come to a solution via just explaining it.  So, I've uploaded a
sample db at (I forgot how to get to the EE upload site).  If the one below doesn't work, please tell me how to get to EE uploads (thanks!).

If you feel comfortable downloading it, here are the steps to make sense of the problem:

1. Background Info:

Again, I have 3 entity levels (grandparent, parent, child).  The db's equivalent are:

a. PAR = grandparent
b. WL  = parent
c. Issue = child

2. Opening up the Sample db:

The frmLogin is my default form when opening the db.  I'm sure you know, but just in case... to get "behind the scene" please press/hold the SHIFT key.

3. Viewing the Db:

a. now, the frmLogin has 2 listboxes:  "Data Entry" and "Create Linkages"

b. first, if you were to look at each of the three data entry forms you'd find the following records counts:

-- "Create PAR": contains 4 records (PAR 1, PAR 2, PAR 3, PAR 4)
-- "Create WL":  contains 6 recors (WL 1, WL 2, ..., WL 6)
-- "Create Issue": contains 9 records (Issue 1, Issue 2, ..., Issue 9)

c. then, the 2nd listbox create two forms to "make the connection" between >> Parent and Grandparent (WL to PAR) << as well as >> "Grandchild to Parent (Issue to WL)" <<

- More specifically, if you select on form "Link WL to PAR", you'll see the 4 PARs in the dropdown.
- also, you find all "available" (unassigned) WL in the left listbox.  All WLs that are linked to a PAR is shown in the
right listbox.
- the same concept is applied for the 2nd form "Link Issue to WL".  Except, in this case the WLs are in the drop-down
menu and the Issues are shown in the 2 listboxes.

Make sense so far???   I hope...

4. Now, viewing the report "View all Data"... THIS IS WHERE THE ACTUAL PROBLEM LIES...

- at first look, the report appears fine.  It shows the 3-tier entities in top-down (left-right) view

- however, having a closer look (in conjunction with the 2 forms where I created the linkages), some records are not
shown.  This is where the combination of joining the LEFT/RIGHT join will come handy.

- okay, what's the problem:

-- first, I suggest to print out the report "View All Data"
-- now, open form "Link WL to PAR"
-- by default, "PAR 1" is shown.  It has one child record... "WL 2"... comparing it w/ my report, it looks fine
-- now, I select "PAR 2" from the drop-down menu... it has 2 children... "WL 1" and "WL 3"... again, comparing it to
the report, it appears fine.
-- finally, if I'd select "PAR 3" and/or "PAR 4"... I don't see any children.   Okay, here's the first problem, my report doesn't show me "PAR 3" and "PAR 4".  While these two grandparent-level records don't have any children (WLs), I still want to show them in the top portion of the report.   So, that's where I'd apply the Right-Join in my query.

-- now, I'd do a similar review of the data shown in form "Link Issue to PAR".  I won't go into details, but you get the idea... I'm simply comparing (with my report) which WLs have which children (Issues).   I'm using a Right-Join to show the "---" (Issue level) for the "WL 3".

Okay, I may already have provided too much details for you to make sense of... hopefully you didn't mind and didn't cause any great confusion.

5. Here's what it bulls down to:

The report "View All Data" should show me all existing records... whether linked or not linked.   Currently, it doesn't... for instance, "PAR 2" and "PAR 4" are missing.  

How can I fix it?

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

LVL 12

Accepted Solution

koutny earned 500 total points
ID: 17967710
Now, I have only had a quick look at your file (I am based in the UK and it's getting towards 7pm here on a Friday evening so it's about time I leave the office).
As far as I can tell, the query which is the record source for your report (qryQuickSnapshot) is doing what you would except it to do.

That is, show all the WL records and only those records from PAR or Issue tables for which there is a link from a WL record.
So naturally you can't except to see records "PAR 3" or "PAR 4" because they are not linked to any WL record. I didn't check whether there are any missing Issues records but they would be if there are any Issues without a link to a WL record.

The easy situation for you would be if a WL record always had an associated PAR record, and similarly if all Issues records had a WL record.
Clearly this is not the case. So my first thoughts are that you create 3 queries.
Your existing query would be the first one.
The second query would list all PAR records without any linked WL records.
The last query would list all Issues records without any linked WL records.

Then I would create a union query which would just merge all these three queries together. This query then would be used as the record source for your report.

To give you an idea (while perhaps wasting my breath) how to construct the second query:

SELECT [PAR] as PARIfNul, "---" as Watchlist,"---" as IssueIfNull, "---"   AS RecommendationIfNull

This query shows all PAR records which don't have a link to a WL record and is useable in a union query together with the qryQuickSnapshot query.

That's off hand suggestion. Perhaps some other guy might beat me to it.

Have a nice weekend.


Author Comment

ID: 17969438

that works absolutely PERFECT!!!

Thanks so much for that idea.   I truly appreciate it.

LVL 12

Expert Comment

ID: 17971027
Well, I am glad that I could have been of some help.
Thanks for the points!

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question