Question

JOIN, INNER JOIN, OUTER JOIN, RIGHT JOIN, LEFT JOIN

Asked by: mcrmg

Hi,

Can EE give me some examples?  Thanks

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-04-11 at 18:01:17ID20950529
Tags

join

,

outer

Topic

Microsoft Access Database

Participating Experts
10
Points
220
Comments
18

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. security of inner and outer firewalls
    hi, suppose your firm has two concentric firewalls. One outer firewall and one inner firewall.behind the outer firewall is the inner firewall.Behind the inner firewall is your firm's secret critical data.considering only security, realtively speaking:How secure should the inn...
  2. Outer join !!
    Dear Experts, I've a typical problem in formulating a query with outer joins. Putting it straight, I need to have 2 outer joins from the same table in the where clause, which its not allowing. (I get an error saying - A table can't be outer joined more than once). But I woul...
  3. inner join and an outer join in one query
    Folks i have the following table 1 table 2 table 3 between table 2 and 3 there must be an inner join table 1 must have a left outer join to table 2 how do i get this to work fine without duplicates

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: alanwarrenPosted on 2004-04-11 at 18:14:18ID: 10803034

HI mcrmg

Using Northwind.mdb tables Orders and Customers


Inner Join eg:

SELECT DISTINCTROW Orders.OrderID, Orders.CustomerID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, Orders.ShipVia, Orders.Freight, Orders.ShipName, Orders.ShipAddress, Orders.ShipCity, Orders.ShipRegion, Orders.ShipPostalCode, Orders.ShipCountry, Customers.CompanyName, Customers.Address, Customers.City, Customers.Region, Customers.PostalCode, Customers.Country
FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Left Join eg:

SELECT DISTINCTROW Orders.OrderID, Orders.CustomerID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, Orders.ShipVia, Orders.Freight, Orders.ShipName, Orders.ShipAddress, Orders.ShipCity, Orders.ShipRegion, Orders.ShipPostalCode, Orders.ShipCountry, Customers.CompanyName, Customers.Address, Customers.City, Customers.Region, Customers.PostalCode, Customers.Country
FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Right Join eg:

SELECT DISTINCTROW Orders.OrderID, Orders.CustomerID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, Orders.ShipVia, Orders.Freight, Orders.ShipName, Orders.ShipAddress, Orders.ShipCity, Orders.ShipRegion, Orders.ShipPostalCode, Orders.ShipCountry, Customers.CompanyName, Customers.Address, Customers.City, Customers.Region, Customers.PostalCode, Customers.Country
FROM Customers RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Alan

 

by: sirbountyPosted on 2004-04-11 at 18:15:13ID: 10803042

>>table {LEFT | RIGHT | FULL} [OUTER] JOIN table ON condition


The LEFT JOIN, RIGHT JOIN, and FULL JOIN operators have these parts:


Part
 Description
 
table
 The names of the tables from which records are combined.
 
condition
 A Boolean expression restricting the records in the resulting table.

Remarks

Use a LEFT JOIN operator to create a left outer join. Left outer joins include all of the records from the first (left) of two tables, even if there are no matching values for records in the second (right) table.

Use a RIGHT JOIN operator to create a right outer join. Right outer joins include all of the records from the second (right) of two tables, even if there are no matching values for records in the first (left) table.

Use a FULL JOIN operator to create a full outer join. Full outer joins include all of the records from both tables, even if there are no matching values for records in the other table.

The following example shows how we could join the "Categories" and "Products" tables using the "Category ID" column to produce a list of all categories, including those that contain no products. The GROUP BY clause and the First aggregate function are used to collapse multiple records for products sharing the same category):

SELECT [Category Name], First([Product Name]) FROM [Categories] LEFT JOIN [Products] ON [Categories].[Category ID] = [Products].[Category ID] GROUP BY [Category Name];

The following example shows how we could join the "Categories" and "Products" tables using the "Category ID" column to produce a list of all products, including those that contain no categories:

SELECT [Category Name], [Product Name] FROM [Categories] RIGHT JOIN [Products] ON [Categories].[Category ID] = [Products].[Category ID];

To combine records disallowing NULL values on any side of a join, use an INNER JOIN operator.

We can use more than one column in a join condition.

When nesting any of the LEFT JOIN, RIGHT JOIN, and FULL JOIN operators with one or more of the INNER JOIN operators, for performance purposes it is recommended that we put LEFT JOIN, RIGHT JOIN, and FULL JOIN operators inside the INNER JOIN operators.

Examples

This example uses the LEFT JOIN operator and the GROUP BY clause to select employees and the number of orders they processed in June, 1991:

SELECT [First Name] & " " & [Last Name] AS [Name], Count([Order Date]) AS [Order Count] FROM [Employees] LEFT JOIN [Orders] ON [Orders].[Employee ID] = [Employees].[Employee ID] AND [Order Date] >= #6/1/1991# AND [Order Date] < #7/1/1991# GROUP BY [First Name] & " " & [Last Name];

This example uses the LEFT JOIN operator, several INNER JOIN operators, and the GROUP BY clause to show whether a given product has or has not been processed at least once by Andrew Fuller:

SELECT [Product Name], "Yes" AS [Served] FROM [Products] LEFT JOIN ([Order Details] INNER JOIN ([Orders] INNER JOIN [Employees] ON [Orders].[Employee ID] = [Employees].[Employee ID] AND [First Name] = "Andrew" AND [Last Name] = "Fuller") ON [Order Details].[Order ID] = [Orders].[Order ID]) ON [Order Details].[Product ID] = [Products].[Product ID] GROUP BY [Product Name];
<<ref: http://exchange.manifold.net/manifold/manuals/5_userman/mfd50OUTER_LEFT_RIGHT_FULL_JOIN_Opera.htm

 

by: alanwarrenPosted on 2004-04-11 at 18:16:48ID: 10803051

 

by: mcrmgPosted on 2004-04-11 at 18:21:43ID: 10803066

RIGHT JOIN, LEFT JOIN
I got this part.  Thanks




INNER JOIN, OUTER JOIN,

Are they the same as full join??

 

by: alanwarrenPosted on 2004-04-11 at 18:25:06ID: 10803076


The INNER JOIN returns all rows from both tables where there is a match. If there are rows in Employees that do not have matches in Orders, those rows will not be listed.

Alan

 

by: mcrmgPosted on 2004-04-11 at 18:27:54ID: 10803081

How about OUTTER??  Thx

 

by: alanwarrenPosted on 2004-04-11 at 18:33:27ID: 10803093

Hi mcrmg,

Straight from sql books on line
=======================================================
Using Outer Joins
Inner joins return rows only when there is at least one row from both tables that matches the join condition. Inner joins eliminate the rows that do not match with a row from the other table. Outer joins, however, return all rows from at least one of the tables or views mentioned in the FROM clause, as long as those rows meet any WHERE or HAVING search conditions. All rows are retrieved from the left table referenced with a left outer join, and all rows from the right table referenced in a right outer join. All rows from both tables are returned in a full outer join

Microsoft® SQL Server™ uses these SQL-92 keywords for outer joins specified in a FROM clause:

LEFT OUTER JOIN or LEFT JOIN
RIGHT OUTER JOIN or RIGHT JOIN
FULL OUTER JOIN or FULL JOIN
SQL Server supports both the SQL-92 outer join syntax and a legacy syntax for specifying outer joins based on using the *= and =* operators in the WHERE clause. The SQL-92 syntax is recommended because it is not subject to the ambiguity that sometimes results from the legacy Transact-SQL outer joins.

Using Left Outer Joins
Consider a join of the authors table and the publishers table on their city columns. The results show only the authors who live in cities in which a publisher is located (in this case, Abraham Bennet and Cheryl Carson).

To include all authors in the results, regardless of whether a publisher is located in the same city, use an SQL-92 left outer join. Here is the query and results of the Transact-SQL left outer join:

USE pubs

SELECT a.au_fname, a.au_lname, p.pub_name

FROM authors a LEFT OUTER JOIN publishers p

    ON a.city = p.city

ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC


The LEFT OUTER JOIN includes all rows in the authors table in the results, whether or not there is a match on the city column in the publishers table. Notice that in the results there is no matching data for most of the authors listed; therefore, these rows contain null values in the pub_name column.

Using Right Outer Joins
Consider a join of the authors table and the publishers table on their city columns. The results show only the authors who live in cities where a publisher is located (in this case, Abraham Bennet and Cheryl Carson). The SQL-92 right outer join operator, RIGHT OUTER JOIN, indicates all rows in the second table are to be included in the results, regardless of whether there is matching data in the first table.

To include all publishers in the results, regardless of whether a city has a publisher located in the same city, use an SQL-92 right outer join. Here is the Transact-SQL query and results of the right outer join:

USE pubs

SELECT a.au_fname, a.au_lname, p.pub_name

FROM authors AS a RIGHT OUTER JOIN publishers AS p
    ON a.city = p.city

ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC


An outer join can be further restricted by using a predicate (such as comparing the join to a constant). This example contains the same right outer join, but eliminates all titles that have sold less than 50 copies:

USE pubs

SELECT s.stor_id, s.qty, t.title

FROM sales s RIGHT OUTER JOIN titles t

    ON s.title_id = t.title_id

    AND s.qty > 50

ORDER BY s.stor_id ASC

For more information about predicates, see WHERE.

Using Full Outer Joins
To retain the nonmatching information by including nonmatching rows in the results of a join, use a full outer join. Microsoft® SQL Server™ provides the full outer join operator, FULL OUTER JOIN, which includes all rows from both tables, regardless of whether or not the other table has a matching value.

Consider a join of the authors table and the publishers table on their city columns. The results show only the authors who live in cities in which a publisher is located (in this case, Abraham Bennet and Cheryl Carson). The SQL-92 FULL OUTER JOIN operator indicates that all rows from both tables are to be included in the results, regardless of whether there is matching data in the tables.

To include all publishers and all authors in the results, regardless of whether a city has a publisher located in the same city, or whether a publisher is located in the same city, use a full outer join. Here is the query and results of the Transact-SQL full outer join:

USE pubs

SELECT a.au_fname, a.au_lname, p.pub_name

FROM authors a FULL OUTER JOIN publishers p
    ON a.city = p.city

ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC

 

by: alanwarrenPosted on 2004-04-11 at 18:42:25ID: 10803117

Hi mcrmg,

You should find all you need to know about SQL Joins here:
This is the link you want, save me posting all the contents.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/acdata/ac_8_qd_09_0zqr.asp

Alan

 

by: Arthur_WoodPosted on 2004-04-12 at 04:30:12ID: 10804560

Access DOES NOT support the Key words OUTER and FULL.  so this example from alanwarren:

USE pubs

SELECT a.au_fname, a.au_lname, p.pub_name

FROM authors a FULL OUTER JOIN publishers p
    ON a.city = p.city

ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC

is not relevant to Access,but does apply to SQL Server.

AW

 

by: v-strom2002Posted on 2004-05-10 at 07:15:43ID: 11031323

Hi,

I have to work with MS Access 2000. This version doesn't support (full) outer join.
How can I substitute this with a combination of left/right/inner join. Performance isn't a problem (small tables). Also temporary tables are possible.

 

by: sherman6789Posted on 2004-06-14 at 16:35:31ID: 11311265

Hello,

This entire thread contain very valuable information.  I am adding a printout of it to my "Hall of Access Information" which includes info that may need later.

Can the joins be used for two table when one is not an Access table?  For example, if one is an Excel tables to be joined withe the Access table.  Both have the same field names in the same order.

WRS

 

by: dcruzdfPosted on 2004-10-17 at 18:33:26ID: 12335455

If you need to use a combination of LEFT and RIGHT JOINs to yield a FULL JOIN, here's what I did

Firstly use a UNION ALL between the RIGHT and LEFT, then encapsulate it with a DISTINCT * on the resulting table.  Suffice to say the only difference between the two queries in the UNION should be the LEFT or RIGHT JOIN operater

Here's an example on what I was working on

SELECT DISTINCT * FROM (

      SELECT       s.CountryName AS CountryNme,
            s.[Sampled Date] AS SampledReceiptedDate,
            s.WgtSampled,
            s.ItemsSampled,
            s.IPK,
            s.CountOfBag,
            r.sum_bags,
            r.sum_weight
      FROM qryMailSampledSep AS s
      RIGHT JOIN qryMailReceiptedSep AS r
      ON (s.[Sampled Date] = r.[receipt Date])
      AND (s.CountryName = r.Country)

      UNION ALL

      SELECT       s.CountryName AS CountryNme,
            s.[Sampled Date] AS SampledReceiptedDate,
            s.WgtSampled,
            s.ItemsSampled,
            s.IPK,
            s.CountOfBag,
            r.sum_bags,
            r.sum_weight
      FROM qryMailSampledSep AS s
      LEFT JOIN qryMailReceiptedSep AS r
      ON (s.[Sampled Date] = r.[receipt Date])
      AND (s.CountryName = r.Country);

) as tbl

ORDER BY SampledReceiptedDate, CountryNme

Lemme know how you go

 

by: sherman6789Posted on 2004-10-17 at 18:37:14ID: 12335471

October 17, 2004


Thanks  dcruzdf.  

Your information is very helpful to me and will probably be useful to others also.

WRSherman

 

by: billcutePosted on 2004-10-17 at 19:33:54ID: 12335783

Question well addressed.

 

by: TriangleILPosted on 2005-08-11 at 09:04:54ID: 14652811

I have tried the above mentioned exercise to get an OUTER join to work in Access JET sql.  This expression gives the following error "Join expression not supported"


SELECT DISTINCT * FROM (
SELECT * FROM (gp LEFT JOIN lp ON gp.name = lp.name) LEFT JOIN temp ON gp.name = temp.name UNION ALL

 ( SELECT * FROM (gp RIGHT JOIN temp ON gp.name = temp.name) RIGHT JOIN lp ON gp.name = lp.name) );

Any ideas?  Thanks

 

by: dudesmashPosted on 2008-02-11 at 13:36:17ID: 20870482

Thanks for all the explanations

 

by: LukeChung-FMSPosted on 2009-11-14 at 13:22:24ID: 25822474

Here's a paper I wrote on Microsoft Access Query Tips and Techniques that you may find useful: http://www.fmsinc.com/tpapers/queries/index.html

There are several examples of multi-table joins including implied joins using criteria. It also has links to other papers on Update Queries, Non-Updateable Queries, Crosstab Queries, etc.

Hope this helps.

 

by: LukeChung-FMSPosted on 2009-12-13 at 11:24:31ID: 26040265

Here's a recent paper we published on the Not-In query and using LEFT JOIN and RIGHT JOIN to get them. Hope it's helpful:

http://www.fmsinc.com/MicrosoftAccess/query/outer-join/index.html

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...