Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Full outer join in a many-to-many relationship, part 1

I have two tables, Table A and Table C, that are related and have a middle table, B, in between. Basically, I am...

trying to show all the records in both tables A and C, even if some of the records do not relate to each other. At the moment, the only records that are being shown are those that are in the intersection table that shows the many-to-many relationship (Table B).

I have tried right and left joins, but they don't seem to be successful. How can I write a query to show all records, related and unrelated?

A very interesting question, indeed. Let's explore it a little, starting with some sample data:

``` tableA tableB tableC pkA Name BfkA BfkC pkC Colour 1 tom 1 35 34 red 2 bob 1 36 35 green 3 amy 2 34 36 gray 2 35 37 blue 38 white
```

Your requirement to "show all the records in both tables even if some of the records do not relate to each other" sounds a lot like what is called a full outer join. However, a join always operates on only two tables at a time:

tableA join tableB join tableC

How do we achieve an "outer" result when there are three tables involved? The answer is to use an outer join for one of the two joins, and an inner join for the other.

"Left outer" three-way query

``` select pkA, Name, PkC, Colour from tableA left outer join tableB on pkA = BfkA inner join tableC on BfkC = pkC
query result: 1 tom 35 green 1 tom 36 gray 2 bob 34 red 2 bob 35 green 3 amy -- -----
```

A few observations are in order. Every row from tableA, the left table, is represented in the result at least once. If a tableA row is related to more than one tableC row, then there are that many rows in the result. If a tableA row is related to no tableC row, then there's only one row in the result, and its tableC columns are null.

"Right outer" three-way query

``` select pkA, Name, PkC, Colour from tableA inner join tableB on pkA = BfkA right outer join tableC on BfkC = pkC
query result: 2 bob 34 red 1 tom 35 green 2 bob 35 green 1 tom 36 gray - --- 37 blue - --- 38 white
```

The right outer three-way query works the same as the left (except in the other direction, as it were). Every row of tableC, the right table, is represented in the result set at least once. If a tableC row is related to more than one tableA row, then there are that many rows in the result. If a tableC row is related to no tableA row, then there's only one row in the result, and its tableA columns are null.

In both the examples above, one of the two joins is an outer join, and the other is an inner. The reason we can use an inner join for the other join in the many-to-many relationship is because of foreign keys. If a row in the right or left table has a matching row in tableB, the intersection table, then that row in tableB must have a matching row in the other table. It's not a question of nulls, or anything -- it's a matter of relational integrity. If a row of tableB exists, it must have matching primary rows in both tables.

"Full outer" three-way query

Uh oh, trouble.

We cannot do a full outer join between, say, tableA and tableB, because there are no rows of tableB that don't have a match in tableA, as previously explained. Similarly, on the other side, we cannot do a full outer join between tableB and tableC, because there are no rows of tableB that don't have a match in tableC.

This response is continued.

This was last published in February 2003

## Content

Find more PRO+ content and other member only offers, here.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Kubernetes gains momentum in big data implementation process

Big data vendors and users are looking to Kubernetes-managed containers to help accelerate system and application deployments and...

• ### IT, others focus efforts as GDPR implementation date looms

The EU's General Data Protection Regulation is meant to bring better data privacy to bear in the age of big data. How GDPR ...

• ### Streaming tool from StreamSets eyes data in motion for GDPR

StreamSets software for inspecting big data brings governance to data in motion. Such capabilities may find more use as the ...

• ### Cambridge Analytica-Facebook case shows need for ethical data mining

Ethical data collection practices are becoming even more important, as cases like Cambridge Analytica's misuse of Facebook data ...

• ### Rethinking analytics processes spurs enterprise innovation

By taking a fresh look at the makeup of their analytics organizations, enterprises can innovate their business models and take ...

• ### Diversified data sets for analytics deliver top results

Analytics teams should focus on data diversity to ensure that their projects deliver the most meaningful insights -- but they ...

## SearchSAP

• ### Software license management tools can help with SAP indirect usage

Companies can use third-party license management tools to audit their SAP environment to address indirect access issues, but the ...

• ### New leadership and redesign may spark SAP Community revival

SAP Community Network was once the best forum for SAP developers to share ideas, but user activity declined significantly. Can ...

• ### SAP Ariba Live focuses on procurement for purpose

SAP Ariba Live 2018 focused on the idea that businesses can use procurement technology to do good in the world; for example, by ...

## SearchSQLServer

• ### ObjectRocket launches Azure MongoDB service

Count ObjectRocket among those pursuing Azure MongoDB deployments. This open source NoSQL database continues to find traction on ...

SQL Operations Studio simplifies routine administration of SQL Server and Azure SQL databases, making database development and ...

• ### Meltdown and Spectre fixes eyed for SQL Server performance issues

Microsoft has responded to the Spectre and Meltdown chip vulnerabilities with patches and other fixes. But IT teams need to sort ...

## TheServerSide.com

• ### IBM fiddles with Apple on new AI services

IBM and Apple extended their existing enterprise mobility partnership with tools to help developers build Watson-based AI ...

• ### A side-by-side comparison of MongoDB and Cassandra databases

How do you choose one NoSQL database over another? Expert Christopher Tozzi explains the difference between MongoDB and Cassandra.

• ### Rust and Python top list of most popular development languages

A new Stack Overflow survey of over 100,000 developers indicated Java and JavaScript are not as popular as they had been before. ...

## SearchDataCenter

• ### IBM Cloud services to secure mainframes out to the edge

Big Blue will introduce IBM Cloud services that use blockchain, containers and its z14 mainframes to deliver improved security ...

• ### Four disadvantages of hyper-converged infrastructure systems

Problems with scalability and unexpected licensing costs can create problems for organizations that deploy hyper-converged ...

• ### IBM Power9 servers seek market inroads to AI, cloud

IBM follows up its first Power9 server with a raft of systems designed to appeal to a wider array of markets -- most notably, AI ...

## SearchContentManagement

• ### Box CEO weighs in on Dropbox IPO, AI in content management

In a wide-ranging conversation, Box CEO talks about Dropbox's public offering, AI's next application for content, Salesforce ...

• ### Scrivito unveils serverless CMS product

By building the CMS with ReactJS, Scrivito gained attraction with development community, according to an analyst.

• ### Content personalization tools sharpen focus on customers

Content personalization isn't new; Amazon weaponized it, and Jeff Bezos is the world's richest man. New tools are putting it ...

## SearchHRSoftware

• ### Wurk delivers tools to help cannabis industry manage business

A growing startup, Wurk, markets an HR software platform for the cannabis industry that handles the complex payroll, compliance ...

• ### Oracle HCM Cloud gets 'big' upgrade

Oracle is bringing consumerlike approaches to its HCM cloud applications. That includes using virtual assistants and intelligent ...

• ### Why the talent management revolution pivots on AI for HR

In this Q&A, HR analyst Jeanne Meister explains how AI will transform the way companies deal with talent and how HR departments ...

Close