Escolar Documentos
Profissional Documentos
Cultura Documentos
Exercício 2
insert into Aluno(Nome, Cidade) values
('João', 'Mogi Mirim'),
('Kaue', 'Santo Antônio de Posse'),
('Pedro', 'Campinas'),
('Rodolfo', 'Mogi das Cruzes'),
('Rubia', 'Mogi Guaçu'),
('Jéssica', 'São Paulo'),
('Márcia', 'Rio de Janeiro'),
('Leandro', 'Itapira'),
('Lucas', 'Santo André'),
('Henrique', 'Conchal');
Exercício 3
select Aluno.Nome, Aluno.RA from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Disciplina.Disciplina_Nome = 'Banco de Dados' and Historico.Semestre = 2
and Historico.Nota < 5;
Exercício 4
alter table Historico add Ano int;
Exercício 5
update Historico set Ano = case
when CodigoHistorico = 1 then 2020
when CodigoHistorico = 2 then 2020
when CodigoHistorico = 3 then 2020
when CodigoHistorico = 4 then 2020
when CodigoHistorico = 5 then 2020
when CodigoHistorico = 6 then 2020
when CodigoHistorico = 7 then 2020
when CodigoHistorico = 8 then 2020
when CodigoHistorico = 9 then 2020
when CodigoHistorico = 10 then 2020
when CodigoHistorico = 11 then 2020
when CodigoHistorico = 12 then 2020
when CodigoHistorico = 13 then 2020
when CodigoHistorico = 14 then 2020
when CodigoHistorico = 15 then 2020
else 2020
end;
Exercício 6
select Professor.Nome from Professor
join Historico on Professor.ID_Prof = Historico.ProfessorID_Prof
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Disciplina.Disciplina_Nome = 'Banco de Dados' and Historico.Ano = 2020;
Exercício 7
select Professor.Nome, count(distinct Disciplina.ID) as QuantidadeDisciplinas
from Professor
join Historico on Professor.ID_Prof = Historico.ProfessorID_Prof
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Historico.Ano = 2020 group by Professor.Nome;
Exercício 8
select Aluno.Nome, Aluno.Cidade, Disciplina.ID, Disciplina.Disciplina_Nome from
Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Historico.Semestre = 1 and Historico.Ano = 2020 and Historico.Nota < 5;
Exercício 9
select Aluno.Nome, Aluno.RA from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
join Professor on Professor.ID_Prof = Historico.ProfessorID_Prof
where Disciplina.Disciplina_Nome = 'Estrutura de Dados' and Professor.Nome =
'Marcos' and Historico.Ano = 2019;
Exercício 10
select Aluno.RA, Aluno.Nome, Disciplina.Disciplina_Nome, Historico.Faltas,
Historico.Nota, Historico.Ano, Historico.Semestre from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Aluno.Nome = 'Alex';
Exercício 11
select Nome from Professor
where Cidade = 'Mogi Mirim';
Exercício 12
select Aluno.Nome as Nome_Aluno, Disciplina.Disciplina_Nome as Nome_Disciplina,
Professor.Nome as Nome_Professor from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
join Professor on Professor.ID_Prof = Historico.ProfessorID_Prof
where Disciplina.CargaHoraria < 60;
Exercício 13
select Professor.Nome from Professor
join Historico on Professor.ID_Prof = Historico.ProfessorID_Prof
join Aluno on Aluno.RA = Historico.AlunoRA
where Aluno.Nome = 'Pedro Paulo Cunha' and Historico.Nota < 5;
Exercício 14
select Aluno.RA, Aluno.Nome from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Professor on Professor.ID_Prof = Historico.ProfessorID_Prof
where Professor.Nome = 'Sandro';
Exercício 15
select Aluno.RA, Aluno.Nome, avg(Historico.Nota) as MediaNotas from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Professor on Professor.ID_Prof = Historico.ProfessorID_Prof
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Professor.Cidade = 'Mogi Mirim'
group by Aluno.RA, Aluno.Nome
having count(distinct Disciplina.ID) = (
select count(distinct Disciplina.ID) from Disciplina
join Historico on Disciplina.ID = Historico.DisciplinaID
join Professor on Professor.ID_Prof = Historico.ProfessorID_Prof
where Professor.Cidade = 'Mogi Mirim'
);
Exercício 16
select count(distinct AlunoRA) as Numero_Alunos from Historico
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Disciplina.Disciplina_Nome in ('Banco de Dados', 'Estrutura de Dados') and
Historico.Ano = 2020 and Historico.Semestre = 1;
Exercício 17
select Disciplina.Disciplina_Nome, avg(Historico.Nota) as MediaNotas from
Historico
join Disciplina on Disciplina.ID = Historico.DisciplinaID
group by Disciplina.Disciplina_Nome
order by MediaNotas desc;
Exercício 18
select Aluno.Nome, Aluno.Cidade, Disciplina.ID, Disciplina.Disciplina_Nome from
Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Historico.Semestre = 1 and Historico.Ano = 2020 and Historico.Nota > 5
order by Disciplina.Disciplina_Nome;
Exercício 19
select Aluno.RA, Aluno.Nome, Disciplina.ID, Disciplina.Disciplina_Nome,
Historico.Faltas, Historico.Nota, Historico.Ano, Historico.Semestre from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Aluno.Nome = 'Alex';
Exercício 20
select count(*) as Quantidade from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Aluno.Nome = 'José da Silva' and Disciplina.Disciplina_Nome = 'Banco de
Dados';
Exercício 21
select Ano, count(distinct AlunoRA) as Quantidade_Alunos from Historico
join Disciplina on Disciplina.ID =Historico.DisciplinaID
where Disciplina.Disciplina_Nome = 'Banco de Dados' and (Ano = 2019 or Ano =
2020)
group by Ano;
Exercício 22
select Aluno.Nome, Aluno.RA from Aluno
join Historico as BD on Aluno.RA = BD.AlunoRA
join Disciplina as D1 on BD.DisciplinaID = D1.ID
join Professor as P1 on BD.ProfessorID_Prof = P1.ID_Prof
left join Historico as TBD on BD.AlunoRA = TBD.AlunoRA
and TBD.Ano = 2018
and TBD.DisciplinaID = (select ID from Disciplina where Disciplina_Nome =
'Tópicos em Banco de Dados')
and TBD.ProfessorID_Prof = BD.ProfessorID_Prof
where D1.Disciplina_Nome = 'Banco de Dados' and BD.Ano = 2019 and BD.Nota > 5 and
TBD.CodigoHistorico is null;
Exercício 23
update Historico set Nota = case
when DisciplinaID = (select ID from Disciplina where Disciplina_Nome =
'Banco de Dados')
and Ano = 2019
and ProfessorID_Prof = (select ID_Prof from Professor where Nome =
'Sandro')
and Nota >= 4.0 and Nota < 5.0 then 4.0
when DisciplinaID = (select ID from Disciplina where Disciplina_Nome =
'Banco de Dados')
and Ano = 2019
and ProfessorID_Prof = (select ID_Prof from Professor where Nome =
'Sandro')
and Nota >= 5.0 and Nota <= 9.5 then Nota + 0.5
when DisciplinaID = (select ID from Disciplina where Disciplina_Nome =
'Banco de Dados')
and Ano = 2019
and ProfessorID_Prof = (select ID_Prof from Professor where Nome =
'Sandro')
and Nota > 9.5 then 10.0
else Nota
end
where DisciplinaID = (select ID from Disciplina where Disciplina_Nome = 'Banco de
Dados')
and Ano = 2019
and ProfessorID_Prof = (select ID_Prof from Professor where Nome = 'Sandro');
Exercício 24
select Aluno.Nome as NomeAluno, Disciplina.Disciplina_Nome, Historico.Faltas,
Historico.Nota,
case
when Historico.Nota >= 7.0 then 'Aprovado'
else 'Reprovado'
end as Situacao from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Historico.DisciplinaID = Disciplina.ID;
Exercício 25
select Disciplina.Disciplina_Nome as Disciplina, avg(Historico.Nota) as Media
from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Historico.DisciplinaID = Disciplina.ID
where Historico.Nota < 5.0
group by Disciplina.Disciplina_Nome;
Exercício 26
update Historico set Nota = Nota + 0.5
where DisciplinaID = (select ID from Disciplina where Disciplina_Nome = 'Banco de
Dados');
Exercício 27
create table CIDADE (
ID_Cid int identity(1,1) primary key,
Cidade_Nome varchar(40)
);