-3

I have to table first one

type nbD
PC 2

second one

type nbF
PC 3
TAB 2

and I want to have something like that

type nbD nbF
PC 2 3
TAB null 2
jarlh
  • 42,561
  • 8
  • 45
  • 63
kahlaoui
  • 1
  • 1
  • 3
    Remember, with such a small sample size, many different guesses at the requirements may produce results that look identical. So it helps if you *explain* the required logic rather than expecting us to divine it from the sample. – Damien_The_Unbeliever Jun 10 '21 at 10:08
  • At least add another table1 row. one with no match in table2. And adjust the expected result accordingly. – jarlh Jun 10 '21 at 10:12
  • 1
    You have tagged your reqest with several join types. Have you read about these? Have you read about how outer joins work? In that case you should be able to answer your question yourself. – Thorsten Kettner Jun 10 '21 at 10:16

2 Answers2

2

You can achieve using below query.

Moreover, before asking for help from community, do some research of your own. I believe if you would have searched "SQL joins with example" in google, you would have get this in first or second result. By that way you learn more, and if you get stuck in the process then you have always have the option to ask for help.

Select a.type, b.ndb, a.ndf
from tab2 a 
left join tab1 b on a.type = b.type
PankajSanwal
  • 956
  • 7
  • 14
0

What you want to use here is a FULL OUTER JOIN:

SELECT st.type_, ft.nbd, st.nbf
FROM second_table st
FULL OUTER JOIN first_table ft ON ft.type_ = st.type_;

OUTPUT:

type_ nbd  nbf
----- ---  ---
PC    2    3
TAB   null 2

Feel free to refer to this DB Fiddle to check it yourself :)

Aleix CC
  • 1,601
  • 1
  • 7
  • 18