DML练习题

DML练习

drop database if exists newTest;
create database newTest;

use newTest;

create table student(
sno varchar(10) primary key,
sname varchar(20),
sage int,
ssex varchar(5)
);
create table teacher(
tno varchar(10) primary key,
tname varchar(20)
);
create table course(
cno varchar(10),
cname varchar(20),
tno varchar(20),
constraint pk_course primary key (cno,tno)
);
create table sc(
sno varchar(10),
cno varchar(10),
score decimal(4,2),
constraint pk_sc primary key (sno,cno)
);

/*初始化学生表的数据**/

insert into student values (‘s001’,’张三’,23,’男’);
insert into student values (‘s002’,’李四’,23,’男’);
insert into student values (‘s003’,’吴鹏’,25,’男’);
insert into student values (‘s004’,’琴沁’,20,’女’);
insert into student values (‘s005’,’王丽’,20,’女’);
insert into student values (‘s006’,’李波’,21,’男’);
insert into student values (‘s007’,’刘玉’,21,’男’);
insert into student values (‘s008’,’萧蓉’,21,’女’);
insert into student values (‘s009’,’陈萧晓’,23,’女’);
insert into student values (‘s010’,’陈美’,22,’女’);

/********初始化教师表*************/

insert into teacher values (‘t001’, ‘刘阳’);
insert into teacher values (‘t002’, ‘谌燕’);
insert into teacher values (‘t003’, ‘胡明星’);

/*****初始化课程表******************/

insert into course values (‘c001’,’J2SE’,’t002’);
insert into course values (‘c002’,’Java Web’,’t002’);
insert into course values (‘c003’,’SSH’,’t001’);
insert into course values (‘c004’,’Oracle’,’t001’);
insert into course values (‘c005’,’SQL SERVER 2005’,’t003’);
insert into course values (‘c006’,’C#’,’t003’);
insert into course values (‘c007’,’JavaScript’,’t002’);
insert into course values (‘c008’,’DIV+CSS’,’t001’);
insert into course values (‘c009’,’PHP’,’t003’);
insert into course values (‘c010’,’EJB3.0’,’t002’);

/*****初始化成绩表*************/

insert into sc values (‘s001’,’c001’,78.9);
insert into sc values (‘s002’,’c001’,80.9);
insert into sc values (‘s003’,’c001’,81.9);
insert into sc values (‘s004’,’c001’,60.9);
insert into sc values (‘s001’,’c002’,82.9);
insert into sc values (‘s002’,’c002’,72.9);
insert into sc values (‘s003’,’c002’,81.9);
insert into sc values (‘s001’,’c003’,59);

1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;

select s.sno from sc c,sc s
where c.sno=s.sno
and c.cno= ‘c001’ and s.cno = ‘c002’ and c.score>s.score;

2、查询平均成绩大于60分的同学的学号和平均成绩;

select sno,avg(score) from sc group by sno
having avg(score)>60;

3、查询所有同学的学号、姓名、选课数、总成绩

select s.sno,s.sname,count(c.cno),sum(c.score)
from student s left join sc c on s.sno=c.sno group by sno;

第二种

select s.sno,s.sname,t.cno_count,t.total_score
from student s
left join (
select sno, count(cno) cno_count, sum(score) total_score
from sc
group by sno) t on s.sno=t.sno;

4、查询姓“刘”的老师的个数;

select count(*)from teacher where tname like ‘刘%’;

5、查询没学过“谌燕”老师课的同学的学号、姓名;

select sno,sname from student where sno not in(
select distinct st.sno from teacher t
join course c on t.tno=c.tno
join sc s on c.cno=s.cno
join student st on s.sno=st.sno
where t.tname=’谌燕’);

第二种

不在这些学号中的同学

select sno ,sname from student s where sno not in(
#学过谌燕老师的课程的学号
select sc.sno from sc ,teacher t,course c where sc.cno=c.cno and c.tno=t.tno and t.tname=’谌燕’);

6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;

select sno,sname from student
where sno in (select sno from sc where sno in
(select sno from sc where cno=’c001’)and cno=’c002’);

第二种

select a.sno,s.sname from sc a,sc b ,student s
where a.sno=b.sno and b.sno=s.sno
and a.cno=’c001’ and b.cno=’c002’;

7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;

select distinct st.sno,st.sname
from teacher t join course c on t.tno=c.tno
join sc s on c.cno=s.cno
join student st on s.sno=st.sno
where t.tname=’谌燕’ group by s.sno having count(s.cno)=4;

select count(c.cno) from teacher t
join course c on t.tno=c.tno where t.tname=’谌燕’;

9、查询所有课程成绩小于60 分的同学的学号、姓名;

select s.sno,s.sname
from student s join sc c on s.sno=c.sno
group by sno having max(score)<60;

10、查询没有学全所有课的同学的学号、姓名;

select t.sno,t.sname from student t
join sc s on t.sno=s.sno
join course c on s.cno=c.cno
group by s.sno having count(s.cno)<10;

select count(course.cname)from course ;

11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名

select distinct s.sno,s.sname from sc,student s where sc.cno in(
select cno from sc where sno=’s001’)
and sc.sno=s.sno and sc.sno!=’s001’;

13、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;

update sc a join (select cno,round(avg(score),2) ascore from sc group by cno) b on a.cno=b.cno
set a.score = b.ascore
where a.cno in (select cno from course c join teacher t on c.tno=t.tno and t.tname=’谌燕’) ;

14、查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名;

select t.sno,t.sname from sc,student t where cno in
(select cno from sc where sc.sno=’s001’) and t.sno!=’s001’ and t.sno=sc.sno
group by sc.sno having count(cno)=(select count(cno) from sc where sc.sno=’s001’)
and count(cno)=(select count(cno) from sc b where b.sno=sc.sno group by sno );

15、删除学习“谌燕”老师课的SC 表记录;

delete from sc where cno in
(select c.cno from course c,teacher t where c.tno=t.tno and t.tname=’谌燕’);

17、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

select cno,max(score),min(score) from sc group by cno;

18、按各科 平均成绩 从 低到高 和 及格率的百分数 从高到低顺序

select cno,100*sum(case when score>60 then 1 else 0 end)/
count(sno) rate from sc group by cno order by rate desc;

21、查询各科成绩前三名的记录:(不考虑成绩并列情况)

select sno,cno,score
from sc r1
where (select count(1) from sc r2
where r2.cno=r1.cno and r2.score >= r1.score) <=3;

22、查询每门课程被选修的学生数

select c.cno,count(s.sno)
from course c left join sc s on c.cno=s.cno group by cno;

23、查询出只选修了一门课程的全部学生的学号和姓名

select t.sno,t.sname from student t join sc s on t.sno=s.sno group by sno having count(s.cno)=1;

24、查询男生、女生人数

select m.boy_count,n.girl_count
from(
select count(*) boy_count from student where ssex=’男’) m,(
select count(*) girl_count from student where ssex=’女’ ) n;

select count(*) from student group by ssex;

25、查询姓“张”的学生名单

select * from student where sname like ‘张%’;

26、查询同名同性学生名单,并统计同名人数

select s.sname,count(s.sno) from student s join(
select distinct a.sno, a.sname, a.ssex from student a,student b
where a.sname = b.sname and a.sno!=b.sno and a.ssex=b.ssex) b on s.sno=b.sno
where s.sname=b.sname
group by s.sname;?

27、1981 年出生的学生名单(注:Student 表中Sage 列的类型是number)

select * from student where year(current_timestamp)-‘1981’=sage;
select *from student where year(date_sub(now(),interval sage year)) =1994;

28、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

select distinct sc.cno,b.a from sc
join (select cno, avg(score) a from sc group by cno) b
on sc.cno=b.cno order by b.a asc,sc.cno desc;

select cno, avg(score) from sc group by cno order by avg(score) asc,cno desc;

29、查询平均成绩大于85 的所有学生的学号、姓名和平均成绩

select t.sno,t.sname,avg(s.score)
from student t join sc s on t.sno=s.sno group by sno
having avg(score)>85;

30、查询课程名称为“数据库”,且分数低于60 的学生姓名和分数

select t.sname,s.score
from student t join sc s on t.sno=s.sno
join course c on c.cno=s.cno where c.cname=’数据库’ and s.score<60;

31、查询所有学生的选课情况;

select t.sno,t.sname,t.sage,t.ssex,c.cname from
student t left join sc s on t.sno=s.sno
left join course c on c.cno=s.cno;

32、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;

select t.sname,c.cname,s.score
from student t join sc s on t.sno=s.sno
join course c on c.cno=s.cno and s.score>70;

33、查询不及格的课程,并按课程号从大到小排列

select cno
from sc where score<60 order by cno desc;

34、查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名;

select t.sno,t.sname,s.score
from student t join sc s on t.sno=s.sno
where s.cno=’c001’ and s.score>80;

35、求选了课程的学生人数

select count(b.sno) from(
select distinct sno from sc) b;

36、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩

select t.sname,max(s.score)
from student t join sc s on t.sno=s.sno
join course c on c.cno=s.cno
join teacher h on h.tno=c.tno where h.tname=’谌燕’group by s.sno;

37、查询各个课程及相应的选修人数

select c.cno,c.cname,count(s.sno)
from course c left join sc s on c.cno=s.cno group by c.cno;

39、查询每门功课成绩最好的前两名

select sno,cno,score
from sc r1
where (select count(1) from sc r2
where r2.cno=r1.cno and r2.score >= r1.score) <=2;

40、统计每门课程的学生选修人数(超过10 人的课程才统计)。

要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select sc.cno, b.a from sc join(
select cno,sno,count(sno) a from sc group by cno having count(sno)>10) b
on sc.cno=b.cno group by sc.cno having count(sc.sno)=count(b.sno)
order by b.a desc,sc.cno asc ;

2

select cno,sno,count(sno) from sc group by cno having count(sno)>10
order by count(sno) desc,cno asc ;

41、检索至少选修两门课程的学生学号

select sno from sc group by sno having count(cno)>=2;

42、查询全部学生都选修的课程的课程号和课程名

select c.cno,c.cname from course c join sc s on c.cno=s.cno
group by s.cno having count(s.sno)=(
select count(sno)from student);

43、查询没学过“谌燕”老师讲授的任一门课程的学生姓名

select sname from student where sno not in(
select distinct st.sno from teacher t
join course c on t.tno=c.tno
join sc s on c.cno=s.cno
join student st on s.sno=st.sno
where t.tname=’谌燕’);

44、查询两门以上不及格课程的同学的学号及其平均成绩

select c.sno,avg(c.score) from(select s.sno,s.cno,s.score from sc s where s.score<60
group by s.cno) c group by c.sno having count(c.cno)>=2;

select sc.sno,avg(sc.score) from sc where sc.score<60 group by sno having count(c.cno)>=2;

45、检索“c004”课程分数小于60,按分数降序排列的同学学号

select sno from sc
where cno=’c004’ and score<60 order by score desc;

46、删除“s002”同学的“c001”课程的成绩

delete from sc where sno=’s002’ and cn0=’c001’;

你可能感兴趣的:(MySQL)