Você está na página 1de 15

Ol pessoal, como prometido, colocarei aqui alguns exerccios e suas

resolues. Estes exerccios eu fiz em aula com o Prof. Carrilho da Fatec


Americana.
Crie uma base de dados Universidade com as tabelas a seguir:
Alunos (RA, nome, endereo, cidade)
Disciplinas (COD_DISC, nome_disc, carga_hor)
Professores (COD_PROF, nome, endereo, cidade)
Turma (COD_DISC, COD_TURMA, COD_PROF, ANO, horrio)
Histrico (RA, COD_DISC, COD_TURMA, COD_PROF, ANO, freqncia,
nota)
Aps criar esta tabelas, e incluir alguns dados neslas, poderemos brincar e
testar todos os comando que aprendemos nos artigos anteirores 1. Encontre o
RA dos alunos com nota em BD em 2010 menor que 5 (obs: BD = cdigo da
disciplinas).
SELECT ra
FROM HISTORICO
WHERE cod_disc='BD' AND ano=2010 AND nota <5

2. Fornea o RA, nome e nota dos alunos com nota menor que 5 em BD em
2010.
SELECT a.RA, a.NOME, a.NOME
from HISTORICO h, ALUNOS a
where cod_disc='BD' and ano=2010 and nota <5 and a.RA=h.RA

3. Fornea o nome dos professores de BD em 2010.


SELECT distinct(p.NOME)
from PROFESSORES p, TURMA t

where p.COD_PROF=t.COD_PROF and t.ANO=2010 and


t.COD_DISC='BD'

4. Encontre o nome, endereo, cidade dos alunos e cdigo das disciplinas onde
os alunos tiveram nota menor que 5 em 2010.
SELECT a.nome, a.ENDERECO, a.CIDADE, h.COD_DISC
from ALUNOS a, HISTORICO h
where a.RA = h.RA and h.NOTA<5 and h.ano=2010

5. Obtenha o nome e RA dos alunos do professor em 2010.


SELECT distinct a.NOME, a.RA
from ALUNOS a, PROFESSORES p, HISTORICO h
where h.RA=a.RA and h.COD_PROF = p.COD_PROF
and p.NOME like '%NOME%'
and h.ANO = 2010

6. Localize o nome e RA dos alunos do professor JOS em 2010 ou 2009 que


tiveram aulas com o professor MARCOS em algum ano.
SELECT distinct a.NOME, a.RA
from ALUNOS a, PROFESSORES p, HISTORICO h
where h.RA=a.RA and h.COD_PROF = p.COD_PROF
and p.NOME like '%JOS%'
and (h.ANO = 2010 or h.ANO = 2009)
AND A.RA in

(select H.RA
from HISTORICO h, PROFESSORES p
where h.COD_PROF = p.COD_PROF
and p.nome like '%MARCOS%')

7. Fornea o histrico escolar do aluno de nome Alex, ou seja, seu RA, nome, a
lista de disciplinas que ele j cursou contendo o cdigo e nome da disciplina,
freqncia e nota e ano que o aluno a cursou.
select a.RA, a.NOME,
h.COD_DISC, d.NOME_DISC,
h.ANO, h.FREQUENCIA, h.NOTA
from HISTORICO h, ALUNOS a, DISCIPLINAS d
where a.RA = h.RA
and h.COD_DISC = d.COD_DISC
and a.NOME like '%ALEX%'

8. Encontre o nome e endereo dos alunos e professores de Campinas.


select a.NOME ,a.ENDERECO, 'aluno'
from ALUNOS a
where a.CIDADE='CAMPINAS'
union
select p.NOME ,p.ENDERECO, 'prof'
from PROFESSORES p
where p.CIDADE='CAMPINAS'

as tipo

9. Fornea o nome dos alunos que cursaram disciplinas com carga horria
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.RA = h.RA and
d.COD_DISC = h.COD_DISC and
p.COD_PROF = h.COD_PROF and
d.CARGA_HOR<60

10. Localize o nome dos professores que lecionaram matrias nas quais o
aluno Pedro Paulo Cunha foi reprovado. (nota < 5)
select p.NOME
from ALUNOS a, PROFESSORES p, HISTORICO h
where
a.RA = h.ra and
p.COD_PROF = h.COD_PROF and
upper(a.NOME) = 'PEDRO PAULO CUNHA' AND
h.NOTA<5

11. Encontre o RA dos alunos que j cursaram todas as disciplinas lecionadas


pelo prof.Joo.
Select distinct a.NOME, a.Ra

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 '%JOO%'
and Cod_disc not in (select cod_disc
from HISTORICO h
where a.RA = h.RA))

12. Encontre o Ra, nome e mdia das notas dos alunos que cursaram todas as
matrias lecionadas por professores de Campinas.
Select distinct A.RA, A.NOME, AVG(nota)
from ALUNOS A , HISTORICO h where A.RA= h.RA
not exists
(select Cod_disc
from PROFESSORES p, TURMA t
where p.CIDADE = 'CAMPINAS'and Cod_disc not in
(select cod_disc
from HISTORICO h
where A.RA = h.RA))
group by A.RA, A.NOME

and

13. Localize o RA e nome dos alunos com nota menor que qualquer uma das
notas do aluno de RA 20090121.
select a.RA,a.NOME
from ALUNOS a, HISTORICO h
where a.RA = h.RA and
h.NOTA <any
(select nota from HISTORICO where RA = 20090121)

14. Fornea o Ra, nome e mdia das notas por alunos.


select a.RA, a.NOME, avg(h.NOTA)
from ALUNOS a, HISTORICO h
where a.RA=h.RA
group by a.RA, a.NOME

15. Encontre o nome dos alunos que no cursaram nenhuma disciplina


oferecida em 2010.
select nome
from ALUNOS
where ra not in (select distinct ra from HISTORICO where
ano=2010 )

16. Fornea o nome dos professores que somente lecionaram matrias com
carga horria foi inferior a 60 horas.
Select 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 no foram reprovados em nenhuma


matria.
select a.NOME
from

ALUNOS a

where a.RA not in (select a.RA


from HISTORICO h, DISCIPLINAS d
where h.COD_DISC = d.COD_DISC and h.NOTA
<6.0)

18. Fornea o RA e nota dos alunos com nota em BD em 2010 menor que a
mdia das notas na disciplina.
select a.NOME, h.NOTA
from

ALUNOS a , HISTORICO h

where a.RA = h.RA and

h.COD_DISC='BD' and ano=2010

and nota <all (select avg(NOTA)


from HISTORICO

where COD_DISC='BD'and ano=2010)

19. Encontre o cdigo e nome das disciplinas e media das notas por disciplina,
mostre apenas as disciplinas com mdia de notas maior que 5.0.
select d.COD_DISC, d.NOME_DISC, avg(nota)
from
where

DISCIPLINAS d, HISTORICO h
h.COD_DISC = d.COD_DISC

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

20. Fornea o nmero de alunos que fizeram BD em 2010.


SELECT count(*) from histrico where cd_disc =bd and ano
=2010

21. Encontre a disciplina com maior mdia 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. Fornea o cdigo das disciplinas com mdia menor que a mdia das notas
em BD.
Select cod_disc, AVG(nota)

from historico
group by cod_disc
having avg(nota) < all (select avg(nota)
from historico
where cod_disc = 'BD')

23. Fornea o nome dos professores que j lecionaram alguma disciplina para
o aluno de RA 5400001.
Select P.nome
from Professores P, historico h
where P.cod_prof = H.cod_prof and H.RA = 5400001

24. Encontre o nome das disciplinas com mdia de freqncia abaixo de 80%.
Select 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. Fornea o nome dos alunos que tiveram no mnimo 2 reprovaes em


2010.
Select A.Nome
From Alunos A, Historico H

Where A.RA = H.RA and ano = 2010 and nota < 6.0
group by A.Nome
having count(*) >= 2

26. Quantas vezes o aluno Jos da Silva cursou a disciplina de Banco de


Dados.
select count(*)
from ALUNOS a, HISTORICO h, DISCIPLINAS d
where a.ra=h.ra
and d.COD_DISC=h.COD_DISC
and a.NOME ='Jose da Silva'
and d.NOME_DISC='Banco de Dados'

27. Quantos alunos j cursaram a disciplina de Banco de Dados em 2009 e


2010.
select count(*)
from HISTORICO h inner join DISCIPLINAS d on D.COD_DISC=
h.COD_DISC
where d.NOME_DISC='Banco de Dados' and (ano=2009 or ano=2010)

28 Fornea o nome dos alunos que obtiveram mais que 2 reprovaes em


algum ano.
select a.NOME, h.ano, count(*)
from ALUNOS a, HISTORICO h

where h.RA = h.RA and h.NOTA<6.0


group by a.NOME, h.ANO
having count(*) >2

29. Encontre o nome dos professores e das disciplinas onde o nmero de


reprovaes foi superior a 20 alunos em 2010.
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<6 and h.ANO='2010'
group by p.NOME, d.NOME_DISC
having count(*) >20

30. Fornea o nome dos professores e cdigo das disciplinas lecionadas por
ele em 2010 e a media 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
group by p.NOME, h.COD_DISC

Operaes Diversas
Agora, iremos fazer algumas atualizaes nas tabelas que criamos acima.
1. Insira seus dados no BD: tabela de alunos (seu nome, Ra, endereo,
cidade), tabela de histrico cursando BD em 2009 na turma A com o professor

JAC e obtendo nota 8.2 e freqncia 80, e a disciplina de EDA em 2010 na


turma A com o professor RCSV e obtendo nota 7.5 e freqncia 75.
INSERT into ALUNOS
values (0912252, 'Aline Bossi', 'Rua manoel', 'Piracicaba')

INSERT into HISTORICO


values (0912252,'BD','A','JAC','2009','80','8.2')

INSERT into HISTORICO


values (0912252,'EDA','A','RCSV',2010,75,7.5)

2. Insira todos os alunos da disciplina de BD (Banco de Dados) em 2009 e


tiveram nota > 5, cursando a disciplina BDII (Banco de Dados II) em 2010 com
o mesmo cdigo de turma e o mesmo professor, mas com freqncia e nota
desconhecidas.
insert into HISTORICO
select h.RA, 'BDII',
h.COD_TURMA, h.COD_PROF,
2010, null, null
from HISTORICO h
where h.COD_DISC = 'BD'
and h.ANO = 2009 and h.NOTA >5

3. Altere as notas dos alunos de Banco de Dados (BD) em 2010 com o


professor JAC atravs da seguinte regra:
notas entre [4.0 e 5.0[ ficaram com 4.0 (inclusive a esquerda e exclusive a
direita).
notas entre [5.0 e 9.5[ tero acrscimo de 0.5 na nota.
notas acima de 9.5, inclusive, ficaro com 10.0
update HISTORICO
set nota

= 4

where nota>=4 and nota<5


and cod_disc = 'BD'
and ano = '2010'
and cod_prof='JAC';

update
set

HISTORICO

nota

= 10

where nota>=9.5 and cod_disc = 'BD'


and ano = '2010' and cod_prof='JAC';

update

HISTORICO

set nota = nota +0.5


where nota>=5.0 and nota<9.5 and cod_disc = 'BD'
and ano = '2010' and cod_prof='JAC';

4. Remova todas as informaes referentes ao aluno Jorge dos Santos do


sistema.
delete from

HISTORICO

where RA IN
(select RA from Alunos where nome='Jorge dos Santos');
delete from ALUNOS
where nome = 'Jorge dos Santos';

5. Faa uma atualizao na freqncia dos alunos de BD em 2010 com o


professor JC de acordo com a seguinte regra:
freqncia abaixo de 45, inclusive, acrscimo de 3.0%
freqncia acima de 45, acrscimo de 5.0%
update Historico h
set

h.FREQUENCIA = h.FREQUENCIA + 0.05* h.FREQUENCIA

where h.FREQUENCIA >45


and h.COD_DISC='Bd'
and h.ANO = '2010 and h.Cod_prof = JAC;
update Historico h
set

h.FREQUENCIA = h.FREQUENCIA * 1.03

where h.FREQUENCIA <=45


and h.COD_DISC='Bd'
and h.ANO = '2010
and h.Cod_prof = JAC;

6. Insira os seguintes dados: Aluna de RA 20090301 obteve nota 8.0 e


freqncia 60 em BD (EM 2009 COM O PROFESSOR JAC NA TURMA A).
INSERT into HISTORICO
values(20090301, 'BD','A', 'JC', 2009, 60, 8 )

7. Execute todas as operaes necessrias para substituir o nome do Prof.


Joo Pedro por Jos Pedro, visto ter ocorrido um erro no momento de
cadastrar o nome do professor.
update PROFESSORES
set nome='Jose Pedro'
where nome='Joao Pedro'

8. Substitua todas as matrias lecionadas pelo Prof. Raimundo Claudio


Vasconcelos em 2010 pelo Prof. JAC.
update TURMA
set cod_prof = 'JAC'
where ano = 2010 and cod_prof in ( select cod_prof
from PROFESSORES
where nome = 'Joo")

Bom, estes so alguns exerccios para praticar,


Bons estudos e at logo!

Você também pode gostar