0
点赞
收藏
分享

微信扫一扫

mysql 语句练习

烟中雯城 2022-04-16 阅读 39
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);
-- 1、检索出年龄大于等于18小于20的学生姓名和性别
select Sname,Ssex from S where Sage>=18 and Sage<=20;
-- 2、查询年龄不在16和25之间的学生姓名和学号
select Sno,Sname from S where Sage<=16 or Sage>=25;
-- 3、检索年龄为空值的学生姓名
select Sname from S where Sage is null ;
-- 4、查询不是CS系 MA系 IS系的学生姓名和性别
select Sname,Ssex from S where Sdept!='CS' and Sdept!='MA' and Sdept!='IS';
-- 5、查询学分最多的课程名
select Cname from C where Credit=(select max(Credit) from C);
-- 6、检索所有比王华年龄大的学生姓名、年龄和性别
select Sname,Sage,Ssex from S where Sage>(select Sage from S where Sname = '王华');
-- 7、检索选修02号课程的学生中成绩最高的学生学号
select Sno from Sc Where Grade = (select max(Grade) from SC where Cno = 2);
-- 8、查询没有任何一门课程成绩超过90分的所有同学的信息
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);
-- 9、查询没有选修01号课程的学生姓名
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);
-- 10、查询选修课程名为“数据库”的学生姓名和成绩
select S.Sname,SC.Grade from S,SC,C where S.Sno = SC.Sno and SC.Cno = C.Cno and C.Cname = '数据库';
-- 11、按分数降序排序,输出计科系学生选修了数据库课程的学生姓名和分数
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;
-- 12、查询共有多少学生选修了数据库
select count(*) from S,SC,C where S.Sno = SC.Sno and C.Cno = SC.Cno and C.Cname = '数据库';
-- 13、查询各门课程的课程号及其选课人数
select C.Cno,count(*) from C left join SC on SC.Cno = C.Cno group by C.Cno;
-- 14、统计每一年龄选修课程的学生人数
select S.Sage,count(distinct S.Sno) from S,SC where SC.Sno = S.Sno group by S.Sage;
-- 15、查询每个学生已获得的总学分(成绩大于等于60)
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;
-- 16、检索选修2门以上课程的学生的学号和其总成绩(不统计不及格课程),并要求总成绩降序
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 ;
-- 17、查询选修过两门及以上课程的学生的学号
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;
-- 18、查询已获得的总学分高于五分的女生学号
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;
-- 19、查询平均成绩在90以上的IS系学生的学号和平均成绩
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;
-- 20、查询刘晨没有选修的课程名
select S.Sname,C.Cname from C,S left join SC on S.Sno = SC.Sno where SC.Cno = C.Cno and S.Sname = '刘晨';
-- 21、查询选修了全部课程且全部通过考试的学生学号和姓名
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));
-- 22、查询选修了全部课程的CS系学生的学号和姓名
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));
-- 23、把学生刘晨所选修的课程的成绩加十分
update SC set Grade := Grade+10 where Sno = (select Sno from S where Sname = '刘晨');
-- 24、创建学生成绩视图view_student_course_grade,包括学号、姓名、课程、成绩
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;
-- 25、定义一个有关学生学号及其平均成绩视图view_student_average_grade
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;
举报

相关推荐

0 条评论