Exercicio SQL Gabarito PUC-Rio

6
PONTIFÍCIA UNIVERSIDADE CATÓLICA DO RIO DE JANEIRO Departamento de Engenharia Industrial ENG 1518 - Sistemas de Informação Gerenciais SQL – Exercícios - Gabarito De acordo com o modelo relacional abaixo, utilize comandos SQL para extrair cada uma das informações solicitadas. Alunos (MATRICULA , nome, endereço, cidade) Disciplinas (COD_DISC , nome_disciplina, carga_horaria) Professores (COD_PROF , nome, endereço, cidade) Turma (@COD_DISC, COD_TURMA, @COD_PROF, ANO,SEMESTRE , horário) FKs: COD_DISC -> Disciplinas(COD_DISC) COD_PROF -> Professores (COD_PROF) Histórico (@MATRICULA, @COD_DISC,@ COD_TURMA, @COD_PROF, @ANO,@SEMESTRE , freqüência, nota) FKs: MATRICULA -> Alunos(MATRICULA) COD_DISC, COD_TURMA, COD_PROF, ANO,SEMESTRE -> Turma(COD_DISC, COD_TURMA,COD_PROF, ANO,SEMESTRE) 1. Encontre a matrícula dos alunos com nota em SIG em 2012.1 menor que 5 (SIG = código da disciplina) SELECT matricula FROM HISTORICO WHERE cod_disc='SIG' AND ano=2012 and semestre = 1 AND nota <5 2. Forneça a matrícula, nome e nota dos alunos com nota menor que 5 em SIG em 2012.1. SELECT a.matricula, a.NOME, a.NOME from HISTORICO h, ALUNOS a where cod_disc='SIG' and ano=2012 and semestre = 1 and nota <5 and a.matricula=h.matricula 3. Forneça o nome dos professores de SIG em 2012.1. SELECT distinct(p.NOME) from PROFESSORES p, TURMA t where p.COD_PROF=t.COD_PROF and t.ANO=2012 and t.semestre = 1 and t.COD_DISC='SIG' 4. Encontre o nome, endereço, cidade dos alunos e código das disciplinas onde os alunos tiveram nota menor que 5 em 2012.1. SELECT a.nome, a.ENDERECO, a.CIDADE, h.COD_DISC from ALUNOS a, HISTORICO h where a.matricula = h. matricula and h.NOTA<5 and h.ano=2012 and semestre = 1

description

Disciplina Sistemas de Informação Gerenciais

Transcript of Exercicio SQL Gabarito PUC-Rio

Page 1: Exercicio SQL Gabarito PUC-Rio

PONTIFÍCIA UNIVERSIDADE CATÓLICA DO RIO DE JANEIRO

Departamento de Engenharia Industrial

ENG 1518 - Sistemas de Informação Gerenciais SQL – Exercícios - Gabarito

De acordo com o modelo relacional abaixo, utilize comandos SQL para extrair cada

uma das informações solicitadas.

Alunos (MATRICULA, nome, endereço, cidade)

Disciplinas (COD_DISC, nome_disciplina, carga_horaria)

Professores (COD_PROF, nome, endereço, cidade)

Turma (@COD_DISC, COD_TURMA, @COD_PROF, ANO,SEMESTRE, horário)

FKs:

COD_DISC -> Disciplinas(COD_DISC)

COD_PROF -> Professores (COD_PROF)

Histórico (@MATRICULA, @COD_DISC,@ COD_TURMA, @COD_PROF, @ANO,@SEMESTRE,

freqüência, nota)

FKs:

MATRICULA -> Alunos(MATRICULA)

COD_DISC, COD_TURMA, COD_PROF, ANO,SEMESTRE -> Turma(COD_DISC,

COD_TURMA,COD_PROF, ANO,SEMESTRE)

1. Encontre a matrícula dos alunos com nota em SIG em 2012.1 menor que 5 (SIG =

código da disciplina) SELECT matricula FROM HISTORICO WHERE cod_disc='SIG' AND ano=2012 and semestre = 1 AND nota <5

2. Forneça a matrícula, nome e nota dos alunos com nota menor que 5 em SIG em

2012.1. SELECT a.matricula, a.NOME, a.NOME from HISTORICO h, ALUNOS a where cod_disc='SIG' and ano=2012 and semestre = 1 and nota <5 and a.matricula=h.matricula

3. Forneça o nome dos professores de SIG em 2012.1.

SELECT distinct(p.NOME)

from PROFESSORES p, TURMA t

where p.COD_PROF=t.COD_PROF and t.ANO=2012 and t.semestre = 1

and t.COD_DISC='SIG'

4. Encontre o nome, endereço, cidade dos alunos e código das disciplinas onde os alunos

tiveram nota menor que 5 em 2012.1.

SELECT a.nome, a.ENDERECO, a.CIDADE, h.COD_DISC

from ALUNOS a, HISTORICO h

where a.matricula = h. matricula and h.NOTA<5 and h.ano=2012 and semestre = 1

Page 2: Exercicio SQL Gabarito PUC-Rio

5. Obtenha o nome e matrícula dos alunos do professor GUSTAVO em 2012.1.

SELECT distinct a.NOME, a.matricula

from ALUNOS a, PROFESSORES p, HISTORICO h

where h.matricula=a.matricula and h.COD_PROF = p.COD_PROF

and p.NOME like '%GUSTAVO%'

and h.ANO = 2012 and semestre = 1

6. Localize o nome e matrícula dos alunos do professor JOSÉ em 2010 ou 2009 que

tiveram aulas com o professor GUSTAVO em algum período.

SELECT distinct a.NOME, a.matricula

from ALUNOS a, PROFESSORES p, HISTORICO h

where h. matricula =a. matricula and h.COD_PROF = p.COD_PROF

and p.NOME like '%JOSE%'

and (h.ANO = 2010 or h.ANO = 2009)

AND A. matricula in

(select H. matricula

from HISTORICO h, PROFESSORES p

where h.COD_PROF = p.COD_PROF

and p.nome like '%GUSTAVO%')

7. Forneça o histórico escolar do aluno de nome Alex, ou seja, sua matrícula, nome, a

lista de disciplinas que ele já cursou contendo o código e nome da disciplina,

frequência e nota e ano/semestre que o aluno a cursou.

select a.matricula, a.NOME,h.COD_DISC, d.NOME_DISC,h.ANO, h.FREQUENCIA, h.NOTA

from HISTORICO h, ALUNOS a, DISCIPLINAS d

where a. matricula = h. matricula

and h.COD_DISC = d.COD_DISC

and a.NOME like '%ALEX%'

8. Encontre o nome e endereço dos alunos e professores de Niterói.

select a.NOME ,a.ENDERECO, 'aluno' as tipo

from ALUNOS a

where a.CIDADE='NITEROI'

union

select p.NOME ,p.ENDERECO, 'prof' as tipo

from PROFESSORES p

where p.CIDADE=' NITEROI '

9. Forneça o nome dos alunos que cursaram disciplinas com carga horária menor que 60

horas, bem como os respectivos professores que as lecionaram.

select a.NOME, p.NOME

from ALUNOS a, DISCIPLINAS d, PROFESSORES p, HISTORICO h

where

a. matricula = h. matricula and

d.COD_DISC = h.COD_DISC and

Page 3: Exercicio SQL Gabarito PUC-Rio

p.COD_PROF = h.COD_PROF and

d.CARGA_HOR<60

10. Localize o nome dos professores que lecionaram matérias nas quais o aluno “Pedro

Paulo Cunha” foi reprovado. (nota < 5)

select distinct p.NOME

from ALUNOS a, PROFESSORES p, HISTORICO h

where

a. matricula = h. matricula and

p.COD_PROF = h.COD_PROF and

a.NOME = 'PEDRO PAULO CUNHA' AND h.NOTA<5

11. Encontre a matrícula dos alunos que já cursaram todas as disciplinas lecionadas pelo

prof. João Paulo.

Select distinct a.NOME, a.matricula

from ALUNOS a

where not exists (select Cod_disc

from PROFESSORES p, TURMA t

where p.COD_PROF = t.COD_PROF and

P.NOME like '%JOÃO%'

and Cod_disc not in (select cod_disc

from HISTORICO h

where a.matricula = h.matricula))

12. Encontre a matrícula, nome e média das notas dos alunos que cursaram todas as

matérias lecionadas por professores de Petrópolis.

Select distinct A.matricula, A.NOME, AVG(nota)

From ALUNOS A , HISTORICO h

where A.matricula= h.matricula

and not exists

(select t.Cod_disc

from PROFESSORES p, TURMA t

where t.cod_prof = p.cod_prof and p.CIDADE = 'PETROPOLIS'

and t.Cod_disc not in

(select h2.cod_disc

from HISTORICO h2

where A.matricula = h2.matricula)

)

group by A.matricula, A.NOME

13. Localize a matrícula e nome dos alunos com nota menor que qualquer uma das notas

do aluno de matrícula “20090121”.

Select distinct a.matricula,a.NOME

from ALUNOS a, HISTORICO h

where a. matricula = h. matricula and

Page 4: Exercicio SQL Gabarito PUC-Rio

h.NOTA < (select min(nota) from HISTORICO where matricula = '20090121')

14. Forneça a matrícula, nome e média das notas por alunos.

select a.matricula, a.NOME, avg(h.NOTA)

from ALUNOS a, HISTORICO h

where a.matricula =h.matricula

group by a.matricula, a.NOME

15. Encontre o nome dos alunos que não cursaram nenhuma disciplina oferecida em

2012.1.

select nome

from ALUNOS

where matricula not in (select distinct matricula from HISTORICO where ano=2012 and

semestre = 1)

16. Forneça o nome dos professores que somente lecionaram matérias com carga horária

inferior a 60 horas.

Select distinct p.NOME

from PROFESSORES p,TURMA t

where p.COD_PROF = t.COD_PROF and

p.COD_PROF not in ( select t.COD_PROF

from TURMA t, DISCIPLINAS d

where t.COD_DISC = d.COD_DISC and CARGA_HOR >=60)

17. Encontre o nome dos alunos que não foram reprovados em nenhuma matéria (nota <

5).

select a.NOME

from ALUNOS a

where a. matricula not in (select h. matricula

from HISTORICO h, DISCIPLINAS d

where h.COD_DISC = d.COD_DISC and h.NOTA <5.0)

18. Forneça a matrícula e nota dos alunos com nota em SIG em 2012.1 menor que a média

das notas na disciplina.

select a.matricula, h.NOTA

from ALUNOS a , HISTORICO h

where a.matricula = h.matricula and h.COD_DISC='SIG' and ano=2012 and semestre = 1

and nota < (select avg(NOTA)

from HISTORICO

where COD_DISC='SIG')

Page 5: Exercicio SQL Gabarito PUC-Rio

19. Encontre o código e nome das disciplinas e media das notas por disciplina, mostre

apenas as disciplinas com média de notas maior que 5.0.

select d.COD_DISC, d.NOME_DISC, avg(nota)

from DISCIPLINAS d, HISTORICO h

where h.COD_DISC = d.COD_DISC

group by d.COD_DISC, d.Nome_DISC having avg(nota) >5

20. Forneça o número de alunos que fizeram SIG no ano de 2012.

SELECT count(*) from historico where cod_disc = ‘SIG’ and ano =2012

21. Encontre a disciplina com maior média de notas.

Select nome_disc, avg(nota)

from Disciplinas D, Historico H

Where D.cod_disc = h.cod_disc

group by nome_disc

having avg(nota) >= all( Select avg(nota)

from historico

group by cod_disc)

22. Forneça o código das disciplinas com média menor que a média das notas em SIG.

Select cod_disc, AVG(nota)

from historico

group by cod_disc

having avg(nota) < all (select avg(nota)

from historico

where cod_disc = 'SIG')

23. Forneça o nome dos professores que já lecionaram alguma disciplina para o aluno de

matrícula “5400001”.

Select distinct P.nome

from Professores P, historico h

where P.cod_prof = H.cod_prof and H.matricula = ‘5400001’

24. Encontre o nome das disciplinas com média de frequência abaixo de 80%.

Select distinct nome_disc

From Disciplinas D,Historico H

Where H.cod_disc = D.cod_disc

group by nome_disc, carga_hor

having avg(frequencia) < 0.8 * carga_horaria

25. Forneça o nome dos alunos que tiveram no mínimo 2 reprovações em 2012.1.

Select A.Nome

From Alunos A, Historico H

Where A.matricula = H. matricula and ano = 2012 and semestre = 1 and nota < 5.0

Page 6: Exercicio SQL Gabarito PUC-Rio

group by A.Nome

having count(*) >= 2

26. Quantas vezes o aluno “José da Silva” cursou a disciplina de SIG.

select count(*)

from ALUNOS a, HISTORICO h

where a.matricula=h.matricula

and d.COD_DISC=h.COD_DISC

and a.NOME ='Jose da Silva'

and h.COD_DISC='SIG'

27. Quantos alunos já cursaram a disciplina de SIG em 2011 e 2012.

select count(*)

from HISTORICO h

where h.COD_DISC='SIG' and (ano=2011 or ano=2012)

28. Forneça o nome dos alunos que obtiveram mais que 2 reprovações em algum período.

select a.NOME, h.ano, h.semestre , count(*)

from ALUNOS a, HISTORICO h

where h.matricula = h.matricula and h.NOTA<5.0

group by a.NOME, h.ANO, h.semestre

having count(*) >2

29. Encontre o nome dos professores e das disciplinas onde o número de reprovações foi

superior a 20 alunos em 2012.1.

select p.NOME, d.NOME_DISC

from PROFESSORES p, DISCIPLINAS d, HISTORICO h

where h.COD_PROF = p.COD_PROF and d.COD_DISC= h.COD_DISC

and h.NOTA<5 and h.ANO='2012' and h.semestre = 1

group by p.NOME, d.NOME_DISC

having count(*) >20

30. Forneça o nome dos professores e código das disciplinas lecionadas por ele em 2012.1

e a média das notas por disciplina.

select p.NOME, h.COD_DISC, avg(h.NOTA)

from PROFESSORES p, HISTORICO h

where h.COD_PROF=p.COD_PROF and h.ANO = 2010 and h.semestre = 1

group by p.NOME, h.COD_DISC