create table S(
Sno varchar(20) primary key comment '学号',
Sname varchar(20),
Ssex char(1),
Sage int,
Sdept varchar(20) comment '系'
);
create table C(
Cno int primary key comment '课程号',
Cname varchar(50),
Credit int comment '学分'
);
create table SC(
Sno varchar(20),
Cno int,
Grade int comment '成绩',
foreign key (Sno) references S(Sno),
foreign key (Cno) references C(Cno),
primary key (Sno,Cno)
);
insert into S
values ('201215121','李勇','男',20,'CS'),
('201215122','刘晨','女',19,'CS'),
('201215123','王敏','女',18,'MA'),
('201215125','张立','男',19,'IS');
insert into C
values (1,'数据库',4),
(2,'数学',5),
(3,'数据结构',4),
(4,'操作系统',4);
insert into SC
values ('201215121',1,92),
('201215121',2,85),
('201215121',3,88),
('201215122',2,90),
('201215122',3,80);
select Sname,Ssex from S where Sage>=18 and Sage<=20;
select Sno,Sname from S where Sage<=16 or Sage>=25;
select Sname from S where Sage is null ;
select Sname,Ssex from S where Sdept!='CS' and Sdept!='MA' and Sdept!='IS';
select Cname from C where Credit=(select max(Credit) from C);
select Sname,Sage,Ssex from S where Sage>(select Sage from S where Sname = '王华');
select Sno from Sc Where Grade = (select max(Grade) from SC where Cno = 2);
select distinct S.Sno,S.Sname,S.Sage,s.Ssex,s.Sdept,SC.Cno,SC.Grade from S,SC where S.Sno=SC.Sno and S.Sno not in (select Sno from SC where Grade>90);
select distinct S.Sname from S,SC where S.Sno = SC.Sno and S.Sno not in (select Sno from SC where SC.Cno = 1);
select S.Sname,SC.Grade from S,SC,C where S.Sno = SC.Sno and SC.Cno = C.Cno and C.Cname = '数据库';
select S.Sname,SCC.Grade from S,(select * from sc order by Grade desc) as SCC,C where S.Sdept = 'CS' and C.Cname = '数据库' and S.Sno = SCC.Sno and C.Cno = SCC.Cno;
select count(*) from S,SC,C where S.Sno = SC.Sno and C.Cno = SC.Cno and C.Cname = '数据库';
select C.Cno,count(*) from C left join SC on SC.Cno = C.Cno group by C.Cno;
select S.Sage,count(distinct S.Sno) from S,SC where SC.Sno = S.Sno group by S.Sage;
select S.Sno,sum(C.Credit) from C,S,SC where SC.Grade>=60 and S.Sno = SC.Sno and SC.Cno = C.Cno group by S.Sno;
select S.Sno,score from S,(select S.Sno,sum(SC.Grade) score from S,SC where S.Sno = SC.Sno and SC.Grade>=60 group by S.Sno having count(*)>2) as SCC where S.Sno = SCC.Sno order by SCC.score desc ;
select distinct S.Sno from S,(select S.Sno from S,SC where S.Sno = SC.Sno group by S.Sno having count(*)>=2) as SCC where SCC.Sno = S.Sno;
select S.Sno from S,(select S.Sno,sum(C.Credit) score from S,SC,C where S.Sno = SC.Sno and SC.Cno = C.Cno and sc.Grade>=60 group by S.Sno) as SCC where S.Ssex='女' and SCC.score>5 and S.Sno = SCC.Sno;
select S.Sno from S,(select S.Sno,avg(SC.Grade) avgscor from SC,S where SC.Sno = S.Sno and S.Sdept = 'IS' group by S.Sno) as SCC where S.Sno = SCC.Sno and SCC.avgscor>90;
select S.Sname,C.Cname from C,S left join SC on S.Sno = SC.Sno where SC.Cno = C.Cno and S.Sname = '刘晨';
select S.Sno,S.Sname from S where S.Sno in (select SC.Sno from SC where SC.Grade >=60 group by SC.Sno having count(*) = (select count(*) from C));
select S.Sno,S.Sname from S where S.Sdept = 'CS' and S.Sno in(select SC.Sno from SC group by SC.Sno having count(*) = (select count(*) from C));
update SC set Grade := Grade+10 where Sno = (select Sno from S where Sname = '刘晨');
create view view_student_course_grade as select S.Sno,S.Sname,C.Cname,SC.Grade from S,SC,C where S.Sno = SC.Sno and SC.Cno = C.Cno;
create view view_student_average_grade as select S.Sno,avg(SC.Grade) from S,SC where S.Sno = SC.Sno group by S.Sno;