SQL Server joining tables with null

2

I have the following tables:

Responsavel             |     Filho
Id      Nome            |     Id    Nome           ResponsavelId
1       Ana             |     1     Aninha         1
2       Maria           |     2     Ana Júlia      1
3       Pedro           |     3     Mariazinha     2

I would like to make a SELECT with INNER JOIN where it would display the following result:

Id    Responsavel     Filho
1     Ana             Aninha
1     Ana             Ana Júlia
2     Maria           Mariazinha
3     Pedro           NULL

I need this null value. can anybody help me? Should I use Left Join ?

    
asked by anonymous 04.08.2015 / 16:31

1 answer

2

You need to use LEFT JOIN , read more about .

SELECT 
    Responsavel.Id
    Responsavel.Nome,
    Filho.Nome
FROM Responsavel
LEFT JOIN Filho
    ON Responsavel.Id = Filho.ResponsavelId
    
04.08.2015 / 16:39