数据库上机实验3


--查询选了“C001”课程的学生姓名和所在系。
select Sname,Sdept from Student where Sno In(
select Sno from SC where Cno = 'C001');

--查询通信工程系成绩80分以上的学生的学号和姓名。

select Sno,Sname from Student where Sdept = '通信工程系' and Sno IN(
select Sno from SC where Grade > 80);

--查询计算机系考试成绩最高的学生的姓名。

Select Sname from student Where Sdept='计算机系' AND Sno IN(select Sno from SC
Where Grade IN(select MAX(Grade) from SC));

--查询年龄最大的男生的姓名、所在系和年龄
select Sname,Sdept,Sage from Student where Sage IN 
(Select MAX(Sage) from student) AND Ssex = '男';

--查询C001课程的考试成绩高于该课程平均成绩的学生的学号和成绩。
select Sno,Grade from SC where Grade >
(select AVG(Grade) from SC where Cno = 'C001') AND Cno = 'C001'; 

--查询计算机系学生考试成绩高于计算机系学生平均成绩的学生的姓名、考试的课程名和考试成绩。
select Sname,Cname,Grade from Student,SC,Course 
where Student.Sno = SC.Sno AND SC.Cno = Course.Cno 
AND Grade > (select AVG(Grade) from SC 
             where Sno IN (select Sno from Student 
             where Sdept = '计算机系')) AND Sdept = '计算机系';
             
--查询VB课程考试成绩高于VB平均成绩的学生姓名和VB成绩。
select Sname,Grade from Student,SC,Course where Student.Sno = SC.Sno AND SC.Cno = Course.Cno 
AND Grade > (select AVG(Grade) from SC 
             where Cno IN(select Cno from Course 
             where Cname = 'VB')) AND Cname = 'VB';
             
--查询没选VB的学生姓名和所在系
select Sname,Sdept from Student,SC,Course where Student.Sno = SC.Sno AND SC.Cno = Course.Cno
AND Grade IS NULL AND Cname = 'VB';

--查询每个学期学分最高的课程信息,列出开课学期、课程名和学分
select Semester,Cname,Credit from Course C1 
where NOT EXISTS (
                  select * from Course C2 
				  where C1.Semester = C2.Semester 
				  AND C1.Credit < C2.Credit
				  );



  --查询每门课程考试成绩最高的学生信息,
  --列出课程号、学生姓名和最高成绩,
  --结果按课程号升序排序,不包括没考试的课程。

select SC1.Cno,Sname,Grade from SC SC1,Student
where Student.Sno = SC1.Sno 
AND NOT EXISTS(select * from SC SC2
			   where SC1.Cno = SC2.Cno
			   AND SC1.Grade < SC2.Grade)
AND SC1.Grade IS NOT NULL
ORDER BY SC1.Cno;

  --查询计算机系学生姓名、年龄和年龄情况,
  --其中年龄情况为:如果年龄小于18,则显示“偏小”;
  --如果年龄在18-22,则显示“合适”;
  --如果年龄大于22,则显示“偏大”。

select Sname 姓名,Sage 年龄,
case when Sage<18 then '偏小'
when Sage>18 and Sage<22 then '合适'
else '偏大'
end '年龄情况'
from Student where Sdept = '计算机系';


  --统计每门课程的选课人数,包括有人选的课程和没有人选的课程,
  --列出课程号,选课人数及选课情况,其中选课情况为:
  --如果此门课程的选课人数超过100人,则显示“人多”;
  --如果此门课程的选课人数在40~100,则显示“一般”;
  --如果此门课程的选课人数在1~40,则显示“人少”;
  --如果此门课程没有人选,则显示“无人选”。

select Course.Cno 课程号,COUNT(Sno) 选课人数 ,
case when COUNT(Sno)>100 then '人多'
when COUNT(Sno)>40 and COUNT(Sno)<100 then '一般'
when COUNT(Sno)>=1 and COUNT(Sno)<40 then '人少'
when COUNT(Sno)=0 then '无人选'
end 选课情况
from SC RIGHT JOIN Course ON Course.Cno = SC.Cno GROUP BY Course.Cno;

--查询计算机系选了VB课程的学生姓名、所在系和考试成绩,并将结果保存到新表
select Sname,Sdept,Grade INTO VB_Grade 
from Student,SC,Course 
where Student.Sno = SC.Sno 
AND SC.Cno = Course.Cno AND Cname = 'VB';

--统计每个系的女生人数,并将结果保存到新表Girls中
select Sdept,COUNT(Sno) 人数 INTO Girls
from Student where Ssex = '女'
GROUP BY Sdept;

--创建一个新表,表名为test,其结构为(COL1, COL 2, COL 3)
create table test(
C0L1 int,
C0L2 char(10) not null,
C0L3 char(10)
);

insert into test(C0L2) values('B1')
insert into test values(1,'B2','C2')
insert into test(C0L1,C0L2) values(2,'B3');

--利用第10题建立的VB_Grade表,
--将信息管理系选了VB课程的学生姓名、所在系和考试成绩插入到VB_Grade表中

insert into VB_Grade select Sname,Sdept,Grade from Student,SC,Course
where Student.Sno = SC.Sno
AND SC.Cno = Course.Cno
AND Sdept = '信息管理系'
AND Cname = 'VB';

--将所有选修C001课程的学生的成绩加10分
UPDATE  SC  
SET Grade = Grade + 10  
where Cno = 'C001';
--select * 
--from SC
--where Cno = 'c001';

--将计算机系所有学生的“计算机文化学”的考试成绩加10分
UPDATE SC
SET Grade = Grade + 10
where Cno IN
(
	select Cno from Course
	where Cname = '计算机文化学'
)AND Sno IN(
	select Sno fROM Student
	where Sdept = '计算机系'
);

--select student.Sno,sdept,cname,Grade
--from Course ,Student,SC
--where Cname = '计算机文化学'
--and Sdept = '计算机系'
--and Student.Sno = SC.Sno
--and Course.Cno = SC.Cno

--修改“VB”课程的考试成绩,
--  如果是通信工程系的学生,则增加10分;
--  如果是信息管理系的学生则增加5分;
--  其他系的学生不加分。
UPDATE SC
SET Grade = Grade +
CASE Sdept
when '通信工程系' then 10
when '信息管理系' then 5
else 0
end
from Student,SC,Course
where Student.Sno = SC.Sno and SC.Cno = Course.Cno
AND Cname = 'VB';

--select Student.Sno,sdept,cname,Grade
--from Student,Course,SC
--where Cname = 'vb'
--and Student.Sno = SC.Sno
--and SC.Cno = Course.Cno

--删除成绩小于50分的学生的选课记录。

DELETE from SC where Grade < 50;

select * from SC

删除计算机系VB考试成绩不及格学生的VB选课记录
DELETE from SC from SC,Student,Course
where Student.Sno = SC.Sno AND SC.Cno = Course.Cno 
AND Sdept = '计算机系'
AND Cname = 'VB'
AND Grade < 60;

--select student.Sno,sdept,cname,Grade from SC,Student,Course
--where Student.Sno = SC.Sno AND SC.Cno = Course.Cno 
--AND Sdept = '计算机系'
--AND Cname = 'VB'
--AND Grade < 60;

--删除“VB”考试成绩最低的学生的VB修课记录。
DELETE from SC from SC,Course
where sc.Cno = Course.Cno
AND Grade IN (select MIN(Grade) from SC where Cno IN (select Cno from Course where Cname = 'VB'))
AND Cname = 'VB';

select Student.Sno,cname,grade
from Student,SC,Course
where Student.Sno = SC.Sno
and SC.Cno = Course.Cno
and Cname = 'vb'

--删除没人选的课程的基本信息。
DELETE from Course 
from Course LEFT JOIN SC ON SC.Cno = Course.Cno
where SC.Cno is NULL;

--select * from SC

你可能感兴趣的:(数据库)