表格内容:
#创建表
--1.学生表
Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表
Course(CID,Cname,TID) --CID --课程编号,Cname 课程名称,TID 教师编号
--3.教师表
Teacher(TID,Tname) --TID 教师编号,Tname 教师姓名
--4.成绩表
SC(SID,CID,score) --SID 学生编号,CID 课程编号,score 分数
CREATE TABLE student
(SID VARCHAR(10),Sname VARCHAR(20),Sage DATETIME,Ssex VARCHAR(10));
# 插入数据
INSERT INTO Student VALUES('01' , '赵雷' , '1990-01-01' , '男');
INSERT INTO Student VALUES('02' , '钱电' , '1990-12-21' , '男');
INSERT INTO Student VALUES('03' , '孙风' , '1990-05-20' , '男');
INSERT INTO Student VALUES('04' , '李云' , '1990-08-06' , '男');
INSERT INTO Student VALUES('05' , '周梅' , '1991-12-01' , '女');
INSERT INTO Student VALUES('06' , '吴兰' , '1992-03-01' , '女');
INSERT INTO Student VALUES('07' , '郑竹' , '1989-07-01' , '女');
INSERT INTO Student VALUES('08' , '王菊' , '1990-01-20' , '女');
CREATE TABLE Course(CID VARCHAR(10),Cname VARCHAR(10),TID VARCHAR(10));
INSERT INTO Course VALUES('01' , '语文' , '02');
INSERT INTO Course VALUES('02' , '数学' , '01');
INSERT INTO Course VALUES('03' , '英语' , '03');
CREATE TABLE Teacher(TID VARCHAR(10),Tname VARCHAR(10));
INSERT INTO Teacher VALUES('01' , '张三');
INSERT INTO Teacher VALUES('02' , '李四');
INSERT INTO Teacher VALUES('03' , '王五');
CREATE TABLE SC(SID VARCHAR(10),CID VARCHAR(10),score DECIMAL(18,1));
INSERT INTO SC VALUES('01' , '01' , 80);
INSERT INTO SC VALUES('01' , '02' , 90);
INSERT INTO SC VALUES('01' , '03' , 99);
INSERT INTO SC VALUES('02' , '01' , 70);
INSERT INTO SC VALUES('02' , '02' , 60);
INSERT INTO SC VALUES('02' , '03' , 80);
INSERT INTO SC VALUES('03' , '01' , 80);
INSERT INTO SC VALUES('03' , '02' , 80);
INSERT INTO SC VALUES('03' , '03' , 80);
INSERT INTO SC VALUES('04' , '01' , 50);
INSERT INTO SC VALUES('04' , '02' , 30);
INSERT INTO SC VALUES('04' , '03' , 20);
INSERT INTO SC VALUES('05' , '01' , 76);
INSERT INTO SC VALUES('05' , '02' , 87);
INSERT INTO SC VALUES('06' , '01' , 31);
INSERT INTO SC VALUES('06' , '03' , 34);
INSERT INTO SC VALUES('07' , '02' , 89);
INSERT INTO SC VALUES('07' , '03' , 98);
# 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
mysql> select stu.*,sc1.score,sc2.score
-> from student stu
-> left join sc sc1 on sc1.sid=stu.sid and sc1.cid=01
-> left join sc sc2 on sc2.sid=stu.sid and sc2.cid=02
-> where sc1.score>sc2.score;
+------+--------+---------------------+------+-------+-------+
| sid | sname | saged | ssex | score | score |
+------+--------+---------------------+------+-------+-------+
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 70 | 60 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 | 50 | 30 |
+------+--------+---------------------+------+-------+-------+
# 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
mysql> select stu.*,sc1.score,sc2.score
-> from student stu
-> left join sc sc1 on sc1.sid=stu.sid and sc1.cid=01
-> left join sc sc2 on sc2.sid=stu.sid and sc2.cid=02
-> where sc1.score
# 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
mysql> select stu.sid,stu.sname,avg(score)avg
-> from student stu
-> left join sc on sc.sid=stu.sid
-> group by stu.sid,stu.sname
-> having avg>=60;
+------+--------+---------+
| sid | sname | avg |
+------+--------+---------+
| 01 | 赵雷 | 89.6667 |
| 02 | 钱电 | 70.0000 |
| 03 | 孙风 | 80.0000 |
| 05 | 周梅 | 81.5000 |
| 07 | 郑竹 | 93.5000 |
+------+--------+---------+
# 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩的
mysql> select stu.sid,stu.sname,avg(score)avg
-> from student stu
-> left join sc on sc.sid=stu.sid
-> group by stu.sid,stu.sname
-> having avg<60;
+------+--------+---------+
| sid | sname | avg |
+------+--------+---------+
| 04 | 李云 | 33.3333 |
| 06 | 吴兰 | 32.5000 |
+------+--------+---------+
# 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
mysql> select stu.sid,stu.sname,count(sc.cid),sum(sc.score)
-> from student stu
-> left join sc on sc.sid=stu.sid
-> group by stu.sid,stu.sname;
+------+--------+---------------+---------------+
| sid | sname | count(sc.cid) | sum(sc.score) |
+------+--------+---------------+---------------+
| 01 | 赵雷 | 3 | 269 |
| 02 | 钱电 | 3 | 210 |
| 03 | 孙风 | 3 | 240 |
| 04 | 李云 | 3 | 100 |
| 05 | 周梅 | 2 | 163 |
| 06 | 吴兰 | 2 | 65 |
| 07 | 郑竹 | 2 | 187 |
| 08 | 王菊 | 0 | NULL |
+------+--------+---------------+---------------+
# 6、查询"李"姓老师的数量
mysql> select count(*)
-> from teacher
-> where teacher.tname like '李%';
+----------+
| count(*) |
+----------+
| 1 |
+----------+
# 7、查询学过"张三"老师授课的同学的信息
mysql> select stu.*
-> from student stu
-> left join sc on sc.sid=stu.sid
-> left join course c on c.cid=sc.cid
-> left join teacher t on t.tid=c.tid
-> where t.tname='张三';
+------+--------+---------------------+------+
| sid | sname | saged | ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-07-01 00:00:00 | 女 |
+------+--------+---------------------+------+
# 8、查询没学过"张三"老师授课的同学的信息
mysql> select stu.*
-> from student stu
-> where sid not in(
-> select sc.sid
-> from sc
-> left join course c on sc.cid=c.cid
-> left join teacher t on c.tid=t.tid
-> where t.tname='张三');
+------+--------+---------------------+------+
| sid | sname | saged | ssex |
+------+--------+---------------------+------+
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 |
| 08 | 王菊 | 1990-01-20 00:00:00 | 女 |
+------+--------+---------------------+------+
# 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
mysql> select stu.*
-> from student stu
-> left join sc sc1 on sc1.sid=stu.sid
-> left join sc sc2 on sc2.sid=stu.sid
-> where sc1.cid=01 and sc2.cid=02;
+------+--------+---------------------+------+
| sid | sname | saged | ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
+------+--------+---------------------+------+
# 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
mysql> select stu.*
-> from student stu
-> where sid in (select sid from sc where cid=01
-> and sid not in(select sid from sc where cid =02));
+------+--------+---------------------+------+
| sid | sname | saged | ssex |
+------+--------+---------------------+------+
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 |
+------+--------+---------------------+------+
# 11、查询没有学全所有课程的同学的信息
mysql> select stu.sid,stu.sname,stu.saged,stu.ssex,count(sc.cid)count
-> from student stu
-> left join sc on sc.sid=stu.sid
-> group by stu.sid,stu.sname,stu.saged,stu.ssex
-> having count <(select count(c.cid)from course c);
+------+--------+---------------------+------+-------+
| sid | sname | saged | ssex | count |
+------+--------+---------------------+------+-------+
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 | 2 |
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 | 2 |
| 07 | 郑竹 | 1989-07-01 00:00:00 | 女 | 2 |
| 08 | 王菊 | 1990-01-20 00:00:00 | 女 | 0 |
+------+--------+---------------------+------+-------+
# 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
mysql> select distinct stu.*
-> from student stu
-> left join sc on sc.sid=stu.sid
-> where sc.cid in(
-> select cid from sc where sid=01);
+------+--------+---------------------+------+
| sid | sname | saged | ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-07-01 00:00:00 | 女 |
+------+--------+---------------------+------+
# 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
# 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
mysql> select *
-> from student
-> where sid not in(
-> select sc.sid
-> from sc
-> left join course c on sc.cid=c.cid
-> left join teacher t on c.tid=t.tid
-> where t.tname='张三');
+------+--------+---------------------+------+
| sid | sname | saged | ssex |
+------+--------+---------------------+------+
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 |
| 08 | 王菊 | 1990-01-20 00:00:00 | 女 |
+------+--------+---------------------+------+
# 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
mysql> select stu.sid,stu.sname,avg(score)avg
-> from student stu
-> left join sc on sc.sid=stu.sid
-> where score<60
-> group by stu.sid,stu.sname
-> having count(*)>=2;
+------+--------+---------+
| sid | sname | avg |
+------+--------+---------+
| 04 | 李云 | 33.3333 |
| 06 | 吴兰 | 32.5000 |
+------+--------+---------+
# 16、检索"01"课程分数小于60,按分数降序排列的学生信息
mysql> select stu.*
-> from student stu
-> left join sc on sc.sid=stu.sid
-> where sc.score<60 and sc.cid =01
-> order by sc.score desc;
+------+--------+---------------------+------+
| sid | sname | saged | ssex |
+------+--------+---------------------+------+
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 |
+------+--------+---------------------+------+
# 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
# 涉及表 student,sc
# 聚合函数 sum,avg
# case when then else end
select stu.sid,
stu.sname,
ifnull(t.avgscore, 0) avgscore,
sum(case when s2.cid = 01 then s2.score else 0 end) '语文',
sum(case when s2.cid = 02 then s2.score else 0 end) '数学',
sum(case when s2.cid = 03 then s2.score else 0 end) '英语'
from (select s1.sid, avg(s1.score) avgscore
from sc s1
group by s1.sid
order by avgscore desc) t
left join sc s2 on t.sid = s2.sid
right join student stu on t.sid = stu.sid
group by stu.sid, stu.sname, t.avgscore
# 18、查询各科成绩最高分、最低分和平均分,以如下形式显示:
课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
# 涉及表:sc
# 聚合函数 max,min,avg,sum,count
select c.cid,
c.cname,
max(s.score)'最高分',
min(s.score)'最低分',
avg(s.score)'平均分',
sum(case when score >= 60 then 1 else 0 end) / count(*) '及格率',
sum(case when score between 70 and 80 then 1 else 0 end) / count(*) '中等率',
sum(case when score between 80 and 90 then 1 else 0 end) / count(*) '优良率',
sum(case when score >=90 then 1 else 0 end) / count(*) '优秀率'
from sc s
left join course c on s.cid = c.cid
group by c.cid, c.cname
# 19、按各科成绩进行排序,并显示排名
#涉及表sc
#窗口函数 row_number()
select s.*, row_number() over (partition by s.cid order by s.score desc ) 排名
from sc s;
# 20、查询学生的总成绩并进行排名
# 涉及表 student ,sc
# 聚合函数 sum
# 排序 order by desc
select stu.sid, ifnull(sum(s.score), 0) sumscore
from student stu
left join sc s on stu.sid = s.sid
group by stu.sid
order by sumscore desc
# 21、查询不同老师所教不同课程平均分从高到低显示
# 涉及表 teacher, course,sc
# 聚合函数 avg
# 排序 order by desc
select t.tid, t.tname, avg(s.score) avgscore
from teacher t
left join course c on t.tid = c.tid
left join sc s on c.cid = s.cid
group by t.tid, t.tname
order by avgscore desc;
# 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
#涉及表 sc
SELECT a.* , (SELECT COUNT(DISTINCT score) FROM SC WHERE CID = a.CID AND score > a.score) + 1 AS 排名
FROM SC AS a HAVING 排名 BETWEEN 2 AND 3 ORDER BY a.CID,排名;
# 23、统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比
# 涉及表 sc,course
# 聚合函数 sum count round
# case when then else end
select c.cid '课程编号',
c.cname '课程名称',
round(sum(case when s.score between 0 and 60 then 1 else 0 end) / count(*) * 100) '0-60占比%',
round(sum(case when s.score between 60 and 70 then 1 else 0 end) / count(*) * 100) '70-60占比%',
round(sum(case when s.score between 70 and 85 then 1 else 0 end) / count(*) * 100) '85-70占比%',
round(sum(case when s.score between 85 and 100 then 1 else 0 end) / count(*) * 100) '100-85占比%'
from sc s
left join course c on s.cid = c.cid
group by c.cid, c.cname
# 24、查询学生平均成绩及其名次
# 涉及表 sc student
# 聚合函数 avg
select stu.sname,avg(s.score)
from sc s
left join student stu on s.sid = stu.sid
group by stu.sname,stu.sid
# 25、查询各科成绩前三名的记录
#涉及表 sc
select s1.sid, s1.score
from sc s1
where (
select count(*)
from sc s2
where s1.cid = s2.cid
and s1.score < s2.score
) < 3
order by s1.cid, s1.score desc;
# 26、查询每门课程被选修的学生数
# 涉及表 course sc
select c.cid, c.cname, count(*)
from course c
left join sc s on c.cid = s.cid
group by c.cid, c.cname;
# 27、查询出只有两门课程的全部学生的学号和姓名
# 涉及表 student sc
select stu.sid, stu.sname
from student stu
left join sc s on stu.sid = s.sid
group by stu.sid, stu.sname
having count(s.cid) = 2
# 28、查询男生、女生人数
mysql> select ssex,count(*)
-> from student
-> group by ssex;
+------+----------+
| ssex | count(*) |
+------+----------+
| 男 | 4 |
| 女 | 4 |
+------+----------+
# 29、查询名字中含有"风"字的学生信息
mysql> select *
-> from student
-> where sname like'%风%';
+------+--------+---------------------+------+
| sid | sname | saged | ssex |
+------+--------+---------------------+------+
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
+------+--------+---------------------+------+
# 30、查询同名同性学生名单,并统计同名人数
# 涉及表 student
# 内连接,不排除自身情况下,若出现同名同姓的至少大于1
select stu1.sid, stu2.sname
from student stu1
join student stu2 on stu1.sname = stu2.sname
group by stu1.sid, stu2.sname
having count(*) > 1
# 31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
mysql> select *
-> from student
-> where saged like'1990%';
+------+--------+---------------------+------+
| sid | sname | saged | ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
| 08 | 王菊 | 1990-01-20 00:00:00 | 女 |
+------+--------+---------------------+------+
# 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
mysql> select c.cid,c.cname,avg(score)
-> from sc
-> left join course c on sc.cid=c.cid
-> group by c.cid,c.cname
-> order by avg(score) desc,c.cid asc;
+------+--------+------------+
| cid | cname | avg(score) |
+------+--------+------------+
| 02 | 数学 | 72.6667 |
| 03 | 英语 | 68.5000 |
| 01 | 语文 | 64.5000 |
+------+--------+------------+
# 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
mysql> select stu.sid,stu.sname,avg(score)avg
-> from student stu
-> left join sc on sc.sid=stu.sid
-> group by stu.sid,stu.sname
-> having avg>=85;
+------+--------+---------+
| sid | sname | avg |
+------+--------+---------+
| 01 | 赵雷 | 89.6667 |
| 07 | 郑竹 | 93.5000 |
+------+--------+---------+
# 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
mysql> select stu.sname,sc.score
-> from student stu
-> left join sc on sc.sid=stu.sid
-> left join course c on c.cid=sc.cid
-> where c.cname='数学' and sc.score<60;
+--------+-------+
| sname | score |
+--------+-------+
| 李云 | 30 |
+--------+-------+
# 35、查询所有学生的课程及分数情况;
mysql> select stu.sid,stu.sname,sc.cid,c.cname,sc.score
-> from student stu
-> left join sc on sc.sid=stu.sid
-> left join course c on c.cid=sc.cid
-> group by stu.sid,stu.sname,sc.cid,c.cname,sc.score;
+------+--------+------+--------+-------+
| sid | sname | cid | cname | score |
+------+--------+------+--------+-------+
| 01 | 赵雷 | 03 | 英语 | 99 |
| 01 | 赵雷 | 02 | 数学 | 90 |
| 01 | 赵雷 | 01 | 语文 | 80 |
| 02 | 钱电 | 03 | 英语 | 80 |
| 02 | 钱电 | 02 | 数学 | 60 |
| 02 | 钱电 | 01 | 语文 | 70 |
| 03 | 孙风 | 03 | 英语 | 80 |
| 03 | 孙风 | 02 | 数学 | 80 |
| 03 | 孙风 | 01 | 语文 | 80 |
| 04 | 李云 | 03 | 英语 | 20 |
| 04 | 李云 | 02 | 数学 | 30 |
| 04 | 李云 | 01 | 语文 | 50 |
| 05 | 周梅 | 02 | 数学 | 87 |
| 05 | 周梅 | 01 | 语文 | 76 |
| 06 | 吴兰 | 03 | 英语 | 34 |
| 06 | 吴兰 | 01 | 语文 | 31 |
| 07 | 郑竹 | 03 | 英语 | 98 |
| 07 | 郑竹 | 02 | 数学 | 89 |
| 08 | 王菊 | NULL | NULL | NULL |
+------+--------+------+--------+-------+
# 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
# 涉及表 student ,sc,course
select stu.sname, c.cname, s.score
from student stu
left join sc s on stu.sid = s.sid
left join course c on s.cid = c.cid
where s.score >= 70;
# 37、查询不及格的课程
mysql> select sc.sid,sc.cid,c.cname,sc.score
-> from sc
-> left join course c on sc.cid=c.cid
-> where sc.score<60;
+------+------+--------+-------+
| sid | cid | cname | score |
+------+------+--------+-------+
| 04 | 01 | 语文 | 50 |
| 04 | 02 | 数学 | 30 |
| 04 | 03 | 英语 | 20 |
| 06 | 01 | 语文 | 31 |
| 06 | 03 | 英语 | 34 |
+------+------+--------+-------+
# 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
mysql> select stu.sid,stu.sname
-> from student stu
-> left join sc on sc.sid=stu.sid
-> where sc.cid=01 and sc.score>80;
Empty set (0.00 sec)
mysql> select stu.sid,stu.sname
-> from student stu
-> left join sc on sc.sid=stu.sid
-> where sc.cid=01 and sc.score>=80;
+------+--------+
| sid | sname |
+------+--------+
| 01 | 赵雷 |
| 03 | 孙风 |
+------+--------+
# 39、求每门课程的学生人数
mysql> select sc.cid,c.cname,count(*)
-> from sc
-> left join course c on c.cid=sc.cid
-> group by sc.cid,c.cname;
+------+--------+----------+
| cid | cname | count(*) |
+------+--------+----------+
| 01 | 语文 | 6 |
| 02 | 数学 | 6 |
| 03 | 英语 | 6 |
+------+--------+----------+
# 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
mysql> select stu.*,sc.score
-> from student stu
-> left join sc on sc.sid=stu.sid
-> left join course c on c.cid=sc.cid
-> left join teacher t on c.tid=t.tid
-> where t.tname='张三'
-> order by sc.score desc
-> limit 1;
+------+--------+---------------------+------+-------+
| sid | sname | saged | ssex | score |
+------+--------+---------------------+------+-------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 90 |
+------+--------+---------------------+------+-------+
# 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
mysql> select sc1.*,sc2.*
-> from sc sc1
-> join sc sc2
-> where sc1.score=sc2.score and sc1.cid!=sc2.cid;
+------+------+-------+------+------+-------+
| sid | cid | score | sid | cid | score |
+------+------+-------+------+------+-------+
| 03 | 03 | 80 | 01 | 01 | 80 |
| 03 | 02 | 80 | 01 | 01 | 80 |
| 02 | 03 | 80 | 01 | 01 | 80 |
| 03 | 02 | 80 | 02 | 03 | 80 |
| 03 | 01 | 80 | 02 | 03 | 80 |
| 01 | 01 | 80 | 02 | 03 | 80 |
| 03 | 03 | 80 | 03 | 01 | 80 |
| 03 | 02 | 80 | 03 | 01 | 80 |
| 02 | 03 | 80 | 03 | 01 | 80 |
| 03 | 03 | 80 | 03 | 02 | 80 |
| 03 | 01 | 80 | 03 | 02 | 80 |
| 02 | 03 | 80 | 03 | 02 | 80 |
| 01 | 01 | 80 | 03 | 02 | 80 |
| 03 | 02 | 80 | 03 | 03 | 80 |
| 03 | 01 | 80 | 03 | 03 | 80 |
| 01 | 01 | 80 | 03 | 03 | 80 |
+------+------+-------+------+------+-------+
# 42、查询每门功课成绩最好的前两名
# 涉及表 sc
select s.cid,s.score
from sc s
where (
select count(*)
from sc s2
where s.cid=s2.cid and s.score
# 43、统计每门课程的学生选修人数(超过5人的课程才统计)。
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
mysql> select sc.cid,count(*)count
-> from sc
-> group by sc.cid
-> having count>5
-> order by count(*)desc,cid asc;
+------+-------+
| cid | count |
+------+-------+
| 01 | 6 |
| 02 | 6 |
| 03 | 6 |
+------+-------+
# 44、检索至少选修两门课程的学生学号
mysql> select stu.sid,count(*)count
-> from student stu
-> left join sc on sc.sid=stu.sid
-> group by stu.sid
-> having count>=2;
+------+-------+
| sid | count |
+------+-------+
| 01 | 3 |
| 02 | 3 |
| 03 | 3 |
| 04 | 3 |
| 05 | 2 |
| 06 | 2 |
| 07 | 2 |
# 45、查询选修了全部课程的学生信息
# 涉及表 student sc
select stu.*
from student stu
join (
select s.sid
from sc s
group by s.sid
having count(s.sid) = 3
) t on stu.sid = t.sid;
# 46、查询各学生的年龄
# 涉及表 student
# 函数 year(),now()
select stu.sname, (year(now()) - year(stu.saged)) age
from student stu;
# 47、查询本周过生日的学生
# 涉及表 student
# 函数 week(),now()
select *
from student stu
where week(saged) = week(now());
# 48、查询下周过生日的学生
# 涉及表 student
# 函数 week(),now()
select *
from student stu
where week(saged)=week(now())+1;
# 49、查询本月过生日的学生
# 涉及表 student
# 函数 month(),now()
select *
from student stu
where month(now())=month(stu.saged);
# 50、查询下月过生日的学生
# 涉及表 student
# 函数 month(),date_sub(),curdate()
select
stu.*
from student stu
where month(date_sub(saged,interval 1 month)) = month(curdate());