SQL Join

Asked

Viewed 52 times

-1

I need to elaborate a query that brings the employee’s registration, employee’s name and employee’s city name using JOIN. But as I have no experience with this function yet, I am stuck!! For this I have the table funcionario and the table cidade. So far I have it:

select * from funcionario, cidade

select funcionario.matfunc, funcionario.nome, cidade.nome
from funcionario, cidade
where funcionario.nome = cidade.nome

Now I’m stuck because I didn’t get the whole JOIN.

  • There is a lot of content about joins here on the forum. Here is a topic that explains the subject very well. https://answall.com/questions/6441/qual-é-a-diferença-entre-inner-join-e-outer-join After reading, try to repeat your queries and ask any questions. Good luck!

  • What are the fields of the tables, which field stores the city code in the table funcionario?

  • Funcio matfunc name Cpf dtnasc CODCID codchef City CODCID name cep Uf

2 answers

1


You were almost there, the comparison has to be codcid table funcionario with codcid table cidade.

select funcionario.matfunc, funcionario.nome, cidade.nome
from funcionario, cidade
where funcionario.codcid = cidade.codcid

That above is already a JOIN, but you can make it more explicit and easier to understand in the following way:

select funcionario.matfunc, funcionario.nome, cidade.nome
from funcionario
JOIN cidade ON funcionario.codcid = cidade.codcid

In this question has quite interesting thing about all kinds of JOIN.

  • Bah.. I didn’t think the comparison should be the CODCID. I figured it would bring me only the code and not the name of the city.

  • That is, the comparison is one thing and the fields that will be returned is another. If the answer solved the problem, mark it as the solution!

-1

Your question is a bit confused, post as the structure of the two tables. and which fields you want to show.

the structure of the Inner Join would be something like this:

select * from funcionarios left outer join cidades on funcionarios.cid_cli = cidades.cod_cidade
  • Thanks for your attention. But now I got to catch this JOIN thing. Hugs!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.