1
I have two tables in SQL one of EMPLOYEE and another of Dependents and I need to bring the name of all employees, regardless of whether or not they have dependents and for those who have, I need to bring the names of dependents as well. For those who do not have, I have to treat the field with the information "does not have dependents". The problem that so far my query is returning each employee name with all dependents. If anyone can help me I thank you.
SELECT EMP.NOME AS NOME_FUNCIONARIO,
DEP.NOME AS NOME_DEPENDENTE
FROM TB_EMPREGADO AS EMP
FULL OUTER JOIN TB_DEPENDENTE AS DEP
ON EMP.COD_DEPTO = COD_DEPTO
Here the image of the two tables
As I do not believe that its application allows the registration of dependents who are not linked to an employee a TB_EMPREGADO LEFT OUTER JOIN TB_DEPENDENTE would be more appropriate. As for the message, just use
COALESCE(DEP.NOME, 'Não poussui dependentes') AS NOME_DEPENDENTE
.– anonimo
In addition, use the CODFUN columns of the tables at the junction (left Join in the case)
– imex