关于MySQL的经典例题50道 答案参考
答案不全面,欢迎交流沟通
— 1、查询”01″课程比”02″课程成绩高的学生的信息及课程分数
select * from sc s INNER JOIN sc c INNER JOIN student st on s.S = c.S and st.S=s.S where s.C=01 and c.C=02 and s.score > c.score;
— //select * from sc s inner join sc c on s.s=c.s where s.c=01 and c.c=02 and s.scorse>c.scorse;
— select * from student,(select * from sc where c=01) q INNER JOIN(select * from sc where c=02) w on q.s=w.s on where q.score > w.score;//
— 2、查询”01″课程比”02″课程成绩低的学生的信息及课程分数
select * from sc s INNER JOIN sc c INNER JOIN student st on s.S = c.S and st.S=s.S where s.C=01 and c.C=02 and s.score < c.score;
— 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select s.S,avg(s.score),st.Sname from student st INNER JOIN sc s on st.S=s.S group by s.S having avg(s.score) >=60;
— 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
select s.S,avg(s.score),st.Sname from student st INNER JOIN sc s on st.S=s.S GROUP BY s.S HAVING avg(score)>60;
— 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select st.S,st.Sname, sum(s.score),count(s.C)from student st INNER JOIN sc s ON st.S=s.S GROUP BY s.S;
— 6、查询”江”姓老师的数量
select count(s) as ‘总人数’from sc where sc.C=(select t from teacher where Tname=’江老师’)
— 7、查询学过”向老师”授课的同学的信息
select * from sc INNER JOIN student on sc.S =student.S where sc.C=(select t from teacher where Tname=’向老师’) and sc.S=student.S;
— 8、查询没学过”向老师”授课的同学的信息
//select * from teacher tt INNER JOIN course c ON tt.T=c.T INNER JOIN sc s on c.C=s.C INNER JOIN student st on s.S=st.S where tt.Tname !=’向老师’;
— 9、查询学过编号为”01″并且也学过编号为”02″的课程的同学的信息
select * from sc s INNER JOIN student st INNER JOIN sc c on s.S =st.S and st.S=c.S where s.C= 01 and c.C=02;
— 10、查询学过编号为”01″但是没有学过编号为”02″的课程的同学的信息
select * from (select * from sc where c=01) s INNER JOIN (select * from sc where c!=02) c on s.s=c.s;
— 11、查询没有学全所有课程的同学的信息
select * from sc GROUP BY s HAVING count(c) != (select count(c) from course);
select s,group_concat(c) from sc group by s
select datediff(‘2019-09-09′,’2018-07-06’)
— 12、查询至少有一门课与学号为”01″的同学所学相同的同学的信息
select * from student sd where sd.S in(select s.S from sc s where s.C in(select s.C from sc s where s.S=01) GROUP BY s.S) and sd.S !=01;
— 13、查询和”01″号的同学学习的课程完全相同的其他同学的信息
select * from student sd where sd.S in(select s.S from sc s INNER JOIN sc c INNER JOIN sc cs on s.S=c.S and c.S= cs.S where s.C=01 and c.C=02 and cs.C=03 GROUP BY s.S);
— 14、查询没学过”哈哈”老师讲授的任一门课程的学生姓名
select * from teacher tt INNER JOIN course c on tt.T=c.T INNER JOIN sc s on c.C=s.C INNER JOIN student sd on s.S=sd.S where tt.Tname=’哈哈’;
— 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select sc.S,sd.Sname,avg(sc.score) from sc INNER JOIN student sd on sc.S=sd.S where score <60 HAVING count(c) >= (select count(c)-1 from course);
— 16、检索”01″课程分数小于60,按分数降序排列的学生信息
select * from sc s INNER JOIN student sd on s.s=sd.s where s.C=01 and s.score<60 GROUP BY s.score desc;
— 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select s.S,a.d as’平均成绩’from sc s INNER JOIN (select avg(s.score) d,s.S from sc s GROUP BY s.S) a on s.S=a.S GROUP BY a.d DESC;
— 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
— 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select c.c ‘课程ID’,cs.Cname ‘课程name’,max(c.score) ‘最高分’,min(c.score) ‘最低分’,avg(c.score) ‘平均分’,from sc c INNER JOIN course cs on c.C=cs.C where
SELECT
L.C AS 课程ID,
L.score AS 最高分,
R.score AS 最低分
FROM
SC L,
SC AS R
WHERE
L.C = R.C
AND L.score = (
SELECT
MAX(IL.score)
FROM
SC AS IL,
Student AS IM
WHERE
L.C = IL.C
AND IM.S = IL.S
GROUP BY
IL.C
)
AND R.Score = (
SELECT
MIN(IR.score)
FROM
SC AS IR
WHERE
R.C = IR.C
GROUP BY
IR.C
);
— 19、按各科成绩进行排序,并显示排名
select s.score,cs.Cname from sc s INNER JOIN course cs on s.C=cs.C GROUP BY s.C ORDER BY s.score desc;
— 20、查询学生的总成绩并进行排名
select d.a from sc e INNER JOIN (select sum(s.score) a,s.C from sc s GROUP BY s.S) d on e.C=d.C GROUP BY d.a desc ;
— 21、查询不同老师所教不同课程平均分从高到低显示
select * from teacher tc INNER JOIN course cs INNER JOIN (select avg(s.score) 平均分,s.C from sc s GROUP BY s.C) w on tc.T=cs.T and cs.C=w.C GROUP BY w.平均分 DESC;
— 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
— 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
— 24、查询学生平均成绩及其名次
select 1+(select COUNT( distinct 平均成绩)FROM (select S,AVG(score) AS 平均成绩 FROM SC GROUP BY S) AS T1
WHERE 平均成绩 > T2.平均成绩) as 名次,S as 学生学号,平均成绩
FROM (SELECT S,AVG(score) 平均成绩 FROM SC GROUP BY S) AS T2 ORDER BY 平均成绩 desc;
— 25、查询各科成绩前三名的记录
////select s.score,s.C from sc s GROUP BY s.C ORDER BY s.score DESC;
— 26、查询每门课程被选修的学生数
select count(s.C) as ‘每门课的总人数’,s.C as’科目’, cs.Cname from sc s INNER JOIN course cs on s.C=cs.C GROUP BY s.C;
— 27、查询出只有两门课程的全部学生的学号和姓名
— 28、查询男生、女生人数
select count(s) as’个数’,Ssex from student where ssex=’男’ or ssex=’女’ GROUP BY Ssex;
— 29、查询名字中含有”风”字的学生信息
select * from student where Sname like ‘%风%’;
— 30、查询同名同性学生名单,并统计同名人数
////– 错误答案:select * from student st INNER JOIN student sd ON st.S=sd.S where st.Sname=sd.Sname and st.Ssex=sd.Sname GROUP BY st.Sname;
select Sname,count(s)from student GROUP BY Sname having count(*)>1;
— 31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
select * from student sd where sd.Sage like ‘%1990%’;
— 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号
select avg(score),c from sc where c=c group by c ORDER BY avg(score) desc;
— 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select s.S AS’学号’,sd.Sname as’姓名’,avg(s.score) as’平均成绩’from sc s INNER JOIN student sd on s.S=sd.S GROUP BY s.S HAVING avg(s.score)>=85;
— 34、查询课程名称为”数学”,且分数低于60的学生姓名和分数
select s.S,sd.Sname,cs.Cname,s.score from sc s INNER JOIN course cs ON s.C=cs.C INNER JOIN student sd ON s.S=sd.S where cs.Cname=’数学’ and s.score<60;
— 35、查询所有学生的课程及分数情况;
select c.Cname,c.c,stu.Sname,s.score from course c inner join sc s on c.C=s.C INNER JOIN student stu on s.S=stu.S ;
— 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
//– select * from sc s INNER JOIN student sd on s.S=sd.S where s.score>70 GROUP BY s.C
Select c.Cname,stu.Sname,s.score from course c inner join sc s on c.C=s.C INNER JOIN student stu on s.S=stu.S where score>70 ;
— 37、查询不及格的课程
select c.Cname,c.c,stu.Sname,s.score from course c inner join sc s on c.C=s.C INNER JOIN student stu on s.S=stu.S where score<60 ;
— 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
select stu.Sname,s.score from sc s INNER JOIN student stu on s.S=stu.S where s.C=01 and s.score>80;
— 39、求每门课程的学生人数
select count(s.C) as ‘每门课的总人数’,s.C as’科目’, cs.Cname from sc s INNER JOIN course cs on s.C=cs.C GROUP BY s.C;
— 40、查询选修”哈哈”老师所授课程的学生中,成绩最高的学生信息及其成绩
select max(score),stu.Sname,t.Tname from teacher t INNER JOIN course c on t.T=c.T INNER JOIN sc s on c.C=s.C INNER JOIN student stu on s.S=stu.S where t.tname=’哈哈’ ;
— 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
///select * from sc where c<>c and score=score;
— 42、查询每门功成绩最好的前两名
— 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
— 44、检索至少选修两门课程的学生学号
— 45、查询选修了全部课程的学生信息
select * from sc s INNER JOIN sc c INNER JOIN sc cs on s.C=c.C and c.S=cs.S where s.C=01 ;
— 46、查询各学生的年龄
select round((TO_DAYS(now())-TO_DAYS(Sage))/365),Sname from student;
— 47、查询本周过生日的学生
select * from student where week(now())=week(Sage);
— 48、查询下周过生日的学生
select * from student where date_add(now(),interval 1 week)=week(sage);
— 49、查询本月过生日的学生
select * from student where month(now())= month(Sage);
— 50、查询下月过生日的学生
select * from student where date_add(now(),interval 1 month)=month(sage);