Doubt in sql exercise query

Asked

Viewed 128 times

1

I have the following relationships:

inserir a descrição da imagem aqui

The statement of the financial year is:

The average grade given by teachers per course discipline Geography in the first half of 2013. Name teacher, discipline and media. Sort by the name of the teacher.

I made the following consultation:

SELECT Professor.nome, Disciplina.nome, AVG(Aula.nota) as Media
FROM Aluno, Disciplina, Professor, Disciplina, DisciplinaCurso, Curso
WHERE DisciplinaCurso.NumDisp = Curso.NumDisp AND 
      DisciplinaCurso.NumDisp = Disciplina.NumDisp AND
      Aula.NumDisp = Disciplina.NumDisp AND Aula.NumFunc = Professor.NumFunc 
      AND Curso.Nome = 'Geografia' AND Aula.Semestre = '1º Semestre de 2013' 
GROUP BY Professor.nome, Disciplina.nome 
ORDER BY Professor.nome;

My question is: as my query is, she is grouping by name of teachers, and the exercise just wants me to group by course discipline, but how will I be able to return the name of these teachers without grouping? And instead of using INNER JOIN, these junctions with WHERE will work?

  • junctions with or with Inner Join , and condition in Where, give in the same, just changes the syntax. Particularly, I prefer to use Inner or left Join. I understand that your query is correct, because if asked to display the name of the teacher, the discipline and media, the group by will be by the teacher and by the discipline.

  • @Rovannlinhalis I understand man.... nice!

1 answer

1


Try this way:

SELECT PF.nome, DI.nome, AVG(AL.nota) as Media
FROM Aluno AL
LEFT OUTER JOIN Professor PF ON PF.NumFunc = AL.NumFunc
LEFT OUTER JOIN Disciplina DI ON DI.NumDisp = AL.NumDisp
LEFT OUTER JOIN DisciplinaCurso DC ON DC.NumDisp = DI.NumDisp
LEFT OUTER JOIN Curso CS ON CS.NumDisp = DC.NumDisp
WHERE CS.Nome = 'Geografia' AND AL.Semestre = '1º Semestre de 2013' 
GROUP BY DI.nome, PF.nome
ORDER BY DI.nome

(I didn’t test it because there’s no way here ! rs)

Explicando por linha:

-- Define os campos a serem exibidos, com cálculo da média do campo "nota"
SELECT PF.nome, DI.nome, AVG(AL.nota) as Media
-- Aqui é sua tabela de pesquisa (tabela LEFT, esquerda)
FROM Aluno AL
-- Esses JOINS trás TUDO da sua tabela da "esquerda" (Aluno), mais os registros da sua tabela da "direita" (ex.: primeiro LEFT é a tabela Professor) que TEM VÍNCULO (relação) com a tabela da "esquerda" (Alunos)
LEFT OUTER JOIN Professor PF ON PF.NumFunc = AL.NumFunc
LEFT OUTER JOIN Disciplina DI ON DI.NumDisp = AL.NumDisp
LEFT OUTER JOIN DisciplinaCurso DC ON DC.NumDisp = DI.NumDisp
LEFT OUTER JOIN Curso CS ON CS.NumDisp = DC.NumDisp
-- Aqui você faz seus filtros
WHERE CS.Nome = 'Geografia' AND AL.Semestre = '1º Semestre de 2013'
-- Aqui você está agrupando
GROUP BY DI.nome, PF.nome
ORDER BY DI.nome

Sometimes your confusion in understanding is because of the filters and "grouping", because you don’t see the lines.

  • you used Left for some reason, because if I use INNER JOIN it would not give in the same, but would solve right tbm?

  • No, INNER would do the same as you did for WHERE. In short: INNER brings you everything that is "TRUE" in the two tables. Already the LEFT it brings EVERYTHING of its main table (left = left) even if the other is empty. and RIGHT would only bring what is TRUE in the "right" table. Look at this link: https://i.stack.Imgur.com/1UKp7.png

  • In this link above, remember: always your query table (FROM table) will be "left" = left, and the others in JOIN will be "right" = right

  • A similar question: https://answall.com/questions/247921/diferen%C3%A7a-entre-Inner-Join-Join-e-where/247942#247942

  • so bro, but the way you do there, will come all the elements of the left.... that I don’t understand... because you want these elements to come from the left table... using JOIN.. will filter only those that are equal and this is not enough for me? or is it because I have to do several JOIN.. you used left Join

  • I’ll add explaining each line

  • @roanibz yes bro I understood... but is that in this case I specify whether to use the Inner Join or left Join.. It’s still going to be... the left Join can return more things you didn’t need.... pq with JOIN will return a table that only satisfies the condition, the intersection of them..

  • Yes, because of your GROUP and filters ! Dependendo da estrutura do seu banco, up to the RIGHT JOIN will bring equal, because you are pulling with the student table, which are probably the final records. Here’s what your system records look like. But in need, we’re here !

  • We’re together, bro. I’ve seen too much of you talking! :)

Show 4 more comments

Browser other questions tagged

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