Two Results in a Query

Asked

Viewed 65 times

0

I have the following example, two related tables

Tabela SETOR
SetorID [chave]
Setor
Vagas



Tabela FUNCIONARIOS
FuncionarioID[chave]
SetorID[chave estrangeira]
Nome
Funcao
CargaHoraria

I would like to make an SQL query filtering by Sector ID, where the result is as follows:

Display Sector data and employees linked to this sector.

  • 1

    Which database? what have you tried to do?

  • Please specify which database you are using, despite similar concepts, there may be syntax differences from one to the other

2 answers

2

SELECT * FROM SETOR NATURAL JOIN FUNCIONARIOS;

This way, all employees will be shown with their sector on the side, if you need to filter the columns just exchange the * for the columns you need.

Since you have the Setorid column in the two tables, you can use a NATURAL JOIN, which simplifies SQL but is the same as you did FUNCIONARIOS.Setorid = SECTOR.Setorid

  • I don’t think I was clear on my question. I would like to make a filter in the query, a Where with Setorid, where the result is , the filtered sector and the employees linked to this sector.

  • @Erisonnicodemos I don’t think you understand very well how SQL works.. The results of an SQL query are always rows of a table (which form a new table), but for didactic purposes we will think of them as lines. The result of the query that I put there, would be a set of rows of your table, with the list of employees and the side of your sector, Voce can filter to appear only the employee name and the sector (in SELECT) but the result is a table, It is not a column with all the names, and another column with all the names. Give a study in SQL that you will understand

-1

SELECT A.SETORID, SETOR, VAGAS, B.NOME FROM SETOR A 
INNER JOIN FUNCIONARIOS B ON A.SETORID=B.SETORID
WHERE A.SETORID = XXX

This way you join the two tables by SETORID and bring the information you need both in one query. Remembering that INNER is used when we want to bring information that contains in both tables.

Browser other questions tagged

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