Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6755409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:23:47+00:00 2026-05-26T13:23:47+00:00

I need to full join two tables with multiple keys As an example, I

  • 0

I need to full join two tables with multiple keys

As an example, I have two tables, one looks like:

id1 | foreignerkey | name | value
 1         5         name1    1
 2         6         name2    2
 3         7         name4    3

as t01; the other looks like:

id2 | foreignerkey | name | value
 1         5         name1  1
 2         7         name2  2
 3         8         name2  3

as t02.

I need “select t01.* , to2.* from t01 full join t02 on t01.name = t02.name“,
but I need also “select t01.* , t02.* from t01 join t02 where t01.foreignerkey = t02.foreignerkey“, the result I need is like this:

 id1 | foreignerkey | name | value | id2 | foreignerkey | name | value
  1         5         name1    1      1         5         name1    1
  2         6         name2    2     null     null        null    null
  3         7         name4    3     null     null        null    null
 null     null        null    null    2         7         name2    2
 null     null        null    null    3         8         name2    3

The problem is, as you can see, at first, I put my query string like this,
“select t01.* , t02.* from t01 full join t02 on t01.name = t02.name where
t01.foreignerkey = t02.foreignerkey
“, but then when t01.name = "name4" (which is not in t02), it won’t show up in the results.

So, my final query string:

select a.* from
    (select t01.id1, t01.foreignerkey as foreignerkey1, t01.name, t01.value,
            t02.id2, t02.foreignerkey as foreignerkey2, t02.name, t02.value 
    from t01 
    full join t02 
    on t01.name = t02.name) a
where a.foreignerkey1 = a.foreignerkey2 
      or a.foreignerkey1 is null 
      or a.foreignerkey2 is null

It costs too much to get the results, is there a better solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:23:48+00:00Added an answer on May 26, 2026 at 1:23 pm

    I can’t verify that this is faster as your original SQL doesn’t work on the version of Oracle I’m using but you could try this:

    SELECT *
      FROM t01, t02
     WHERE t01.NAME = t02.NAME
       AND t01.foreignerkey = t02.foreignerkey
    UNION ALL
    SELECT t01.*, NULL, NULL, '', ''
      FROM t01
     WHERE NOT EXISTS (SELECT NULL
              FROM t02
             WHERE t01.NAME = t02.NAME
               AND t01.foreignerkey = t02.foreignerkey)
    UNION ALL
    SELECT NULL, NULL, '', '', t02.*
      FROM t02
     WHERE NOT EXISTS (SELECT NULL
              FROM t01
             WHERE t01.NAME = t02.NAME
               AND t01.foreignerkey = t02.foreignerkey)
    
    • Use an inner join to t01.NAME = t02.NAME AND t01.foreignerkey = t02.foreignerkey
    • Use NOT EXISTS to get the records from t01 that dont have a match in t01
    • Use NOT EXISTS to get the records from t02 that dont have a match in t01
    • Use UNION to join the result sets in this case ALL is safe as we know that the three result sets are mutually exclusive

    Or you can use an outer join as @kedar kamthe has sugested:

    SELECT *
      FROM t01, t02
     WHERE t01.NAME = t02.NAME(+)
       AND t01.foreignerkey = t02.foreignerkey(+)
    UNION
    SELECT NULL, NULL, '', '', t02.*
      FROM t02
     WHERE NOT EXISTS (SELECT NULL FROM t01 WHERE t01.NAME = t02.NAME  AND  t01.foreignerkey = t02.foreignerkey)
    

    But you still need the second query to return the results from t02

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables with a common key field, and I need to join
I have two tables: **Product** ID Name SKU **Brand** ID Name Product table has
I have two tables which I would like to place side-by-side exactly as they
I have 3 tables that I need to join, these join together fine using
I have combined two different tables together, one side is named DynDom and the
I vaguely remember that I need to join the two weird tables to get
We have two tables like so: Event id type ... a bunch of other
I have a database full of recipes, one recipe per row. I need to
I have two tables that make up a full text index of article content
I need to add full web search to my site. I need something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.