Compare three SQL Server columns

20 pts.
Tags:
SQL
SQL Server
I have 3 tables table1 has the column id1 with three values in it '1','2','3' and table2 has column id2 with three values '3','4','5' and table3 has column id3 with three values '5','6','7'. How do I join all the three tables, and I also want the null value to be displayed. Please go through the example below:
table1 | table2 | table3
----------------------------------------------------
id1 | id2 | id3
----------------------------------------------------
1 | 3 | 5
2 | 4 | 6
3 | 5 | 7
the output which i expect is
id1 | id2 | id3
----------------------
1 | null | null
2 | null | null
3 | 3 | null
null | 4 | null
null | 5 | 5
null | null | 6
null | null | 7
Someone please do help me. I am confused.
1

Answer Wiki

Thanks. We'll let you know when a new response is added.

You need to FULL OUTER JOIN your tables.

First FULL OUTER JOIN table1 to table2, and then FULL OUTER JOIN that to table3.

Since this might be your homework, I’m not going to give you the code.

Try to do it, and if you get stuck, feel free to post your attempt, and we will give you our feedback.

Discuss This Question: 3  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • harish9789

    Please have a look into the following question

    I have 3 tables table1 has the column id1 with three values in it '1','2','3' and table2 has column id2 with three values '3','4','5' and table3 has column id3 with three values '5','6','7'  how do i join all the three tables, and i also want the null value to be displayed.pls go through the example below.

    table1        |       table2        |      table3

    ----------------------------------------------------  

     id1          |       id2           |      id3

    ----------------------------------------------------

      1           |        3            |       5

      2           |        4            |       6

      3           |        5            |       7

    the output which i expect is

    id1  |  id2   |  id3

    ----------------------

     1   |  null  | null

     2   |  null  | null

     3   |   3    | null

    null |   4    | null

    null |   5    | 5

    null |  null  | 6

    null |  null  | 7

     

    some1 pls do help me am confused

    20 pointsBadges:
    report
  • Subhendu Sen
    Assuming this query is based on home work/ course related, For proper answer, you can follow other examples in your study materials/ references. This can help you to gather solid knowledge.
    137,090 pointsBadges:
    report
  • ToddN2000
    What code have you tried? What were your errors?
    131,645 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: