MySQL 七十条查询语句精通MySQL面试题

MySQL查询语句练习,面试题必备

    • 创表和添加数据
    • 二十一条练手查询
    • 五十条进阶查询
    • 题目
    • 参考答案1:
    • 参考答案2:

创表和添加数据

class,computer,student表

CREATE TABLE `class` (
    `classid` int(11) NOT NULL AUTO_INCREMENT,
    `classname` varchar(20) DEFAULT NULL,
    PRIMARY KEY (`classid`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;
insert  into `class`(`classid`,`classname`) values (1,'G1T01');
insert  into `class`(`classid`,`classname`) values (2,'G1T02');
insert  into `class`(`classid`,`classname`) values (3,'G1T03');
insert  into `class`(`classid`,`classname`) values (4,'G1T04');
insert  into `class`(`classid`,`classname`) values (5,'G1T05');
insert  into `class`(`classid`,`classname`) values (6,'G1T06');
insert  into `class`(`classid`,`classname`) values (7,'G1T07');

CREATE TABLE `computer` (
    `studentid` varchar(20) DEFAULT NULL,
    `score` float DEFAULT NULL,
    KEY `studentid` (`studentid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert  into `computer`(`studentid`,`score`) values ('2010001',90);
insert  into `computer`(`studentid`,`score`) values ('2010002',80);
insert  into `computer`(`studentid`,`score`) values ('2010003',70);
insert  into `computer`(`studentid`,`score`) values ('2010004',60);
insert  into `computer`(`studentid`,`score`) values ('2010005',75);
insert  into `computer`(`studentid`,`score`) values ('2010006',85);
insert  into `computer`(`studentid`,`score`) values ('2010007',70);
insert  into `computer`(`studentid`,`score`) values ('2010008',60);
insert  into `computer`(`studentid`,`score`) values ('2010009',75);
insert  into `computer`(`studentid`,`score`) values ('2010010',85);

CREATE TABLE `student` (
    `studentid` varchar(20) NOT NULL,
    `studentname` varchar(20) DEFAULT NULL,
    `studentage` int(11) DEFAULT NULL,
    `studentsex` char(10) DEFAULT NULL,
    `studentaddress` varchar(50) DEFAULT NULL,
    `classid` int(11) DEFAULT NULL,
    PRIMARY KEY (`studentid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert  into `student`(`studentid`,`studentname`,`studentage`,`studentsex`,`studentaddress`,`classid`) values ('2010001','Jack',21,'男','湖北襄樊',1);
insert  into `student`(`studentid`,`studentname`,`studentage`,`studentsex`,`studentaddress`,`classid`) values ('2010002','Scott',22,'男','湖北武汉',2);
insert  into `student`(`studentid`,`studentname`,`studentage`,`studentsex`,`studentaddress`,`classid`) values ('2010003','Lucy',23,'女','湖北武汉',3);
insert  into `student`(`studentid`,`studentname`,`studentage`,`studentsex`,`studentaddress`,`classid`) values ('2010004','Alan',19,'女','湖北襄樊',4);
insert  into `student`(`studentid`,`studentname`,`studentage`,`studentsex`,`studentaddress`,`classid`) values ('2010005','Bill',20,'男','湖北襄樊',5);
insert  into `student`(`studentid`,`studentname`,`studentage`,`studentsex`,`studentaddress`,`classid`) values ('2010006','Bob',21,'男','湖北宜昌',6);
insert  into `student`(`studentid`,`studentname`,`studentage`,`studentsex`,`studentaddress`,`classid`) values ('2010007','Colin',22,'女','湖北襄樊',6);
insert  into `student`(`studentid`,`studentname`,`studentage`,`studentsex`,`studentaddress`,`classid`) values ('2010008','Fred',19,'男','湖北宜昌',5);
insert  into `student`(`studentid`,`studentname`,`studentage`,`studentsex`,`studentaddress`,`classid`) values ('2010009','Hunk',19,'男','湖北武汉',4);
insert  into `student`(`studentid`,`studentname`,`studentage`,`studentsex`,`studentaddress`,`classid`) values ('2010010','Jim',18,'男','湖北襄樊',3);

二十一条练手查询

1查询出学生的编号,姓名,计算机成绩
2查询参加过考试的学生信息
3查询出学生的编号、姓名、所在班级名称、计算机成绩
4查询出年龄大于19岁的学生编号、姓名、计算机成绩
5查询出姓名中包含有c的学生编号、姓名、计算机成绩
6查询出计算机成绩大于80分的学生编号、姓名、班级名称
7查询出所有学生的信息和计算机成绩信息
8查询出每个班的学生的计算机成绩的平均分,最高分,最低分
9查询显示出班级的计算机平均分大于75的班级名称、平均分信息,并按照平均分降序显示
10查询出和Jim住在同一个地方的学生的基本信息        
11查询出班级编号大于3的学生基本信息
12查询出班级编号大于3的学生的计算机平均分信息    
13查询出班级编号大于3的男生的学生信息
14查询男、女生的计算机平均成绩、最高分、最低分
15将参加过考试的学生的年龄更改为20
16查询出每个班级的学生的平均分(查询的结果中包含平均分和班级名称)
17删除姓名包含“c”字符的学生计算机成绩
18查询出G1T07班学生的编号、姓名、班级名称、计算机成绩
19查询出年龄在20-25之间的学生的编号、姓名、年龄、计算机成绩
20查询出成绩最高的学生的编号、姓名、计算机成绩、所在班级名称
21查询统计出每个班的平均分、显示平均分超过70分的信息、并按照降序显示信息

五十条进阶查询

表结构预览
–学生表 Student(SId,Sname,Sage,Ssex) --SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
–课程表 Course(CId,Cname,TId) --CId 课程编号,Cname 课程名称,TId 教师编号
–教师表 Teacher(TId,Tname) --TId 教师编号,Tname 教师姓名
–成绩表 SC(SId,CId,score) --SId 学生编号,CId 课程编号,score 分数

CREATE TABLE `course` (
  `cid` varchar(10) DEFAULT NULL,
  `cname` varchar(10) DEFAULT NULL,
  `tid` varchar(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
insert  into `course`(`cid`,`cname`,`tid`) values ('01','语文','02');
insert  into `course`(`cid`,`cname`,`tid`) values ('02','数学','01');
insert  into `course`(`cid`,`cname`,`tid`) values ('03','英语','03');

CREATE TABLE `sc` (
  `sid` varchar(10) DEFAULT NULL,
  `cid` varchar(10) DEFAULT NULL,
  `score` decimal(18,1) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
insert  into `sc`(`sid`,`cid`,`score`) values ('01','01','80.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('01','02','90.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('01','03','99.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('02','01','70.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('02','02','60.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('02','03','80.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('03','01','80.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('03','02','80.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('03','03','80.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('04','01','50.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('04','02','30.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('04','03','20.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('05','01','76.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('05','02','87.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('06','01','31.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('06','03','34.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('07','02','89.0');
insert  into `sc`(`sid`,`cid`,`score`) values ('07','03','98.0');

CREATE TABLE `student` (
  `sid` varchar(10) DEFAULT NULL,
  `sname` varchar(10) DEFAULT NULL,
  `sage` datetime DEFAULT NULL,
  `ssex` varchar(10) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
insert  into `student`(`sid`,`sname`,`sage`,`ssex`) values ('01','赵雷','1990-01-01 00:00:00','男');
insert  into `student`(`sid`,`sname`,`sage`,`ssex`) values ('02','钱电','1990-12-21 00:00:00','男');
insert  into `student`(`sid`,`sname`,`sage`,`ssex`) values ('03','孙风','1990-05-20 00:00:00','男');
insert  into `student`(`sid`,`sname`,`sage`,`ssex`) values ('04','李云','1990-08-06 00:00:00','男');
insert  into `student`(`sid`,`sname`,`sage`,`ssex`) values ('05','周梅','1991-12-01 00:00:00','女');
insert  into `student`(`sid`,`sname`,`sage`,`ssex`) values ('06','吴兰','1992-03-01 00:00:00','女');
insert  into `student`(`sid`,`sname`,`sage`,`ssex`) values ('07','郑竹','1989-07-01 00:00:00','女');
insert  into `student`(`sid`,`sname`,`sage`,`ssex`) values ('08','王菊','1990-01-20 00:00:00','女');

CREATE TABLE `teacher` (
  `tid` varchar(10) DEFAULT NULL,
  `tname` varchar(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
insert  into `teacher`(`tid`,`tname`) values ('01','张三');
insert  into `teacher`(`tid`,`tname`) values ('02','李四');
insert  into `teacher`(`tid`,`tname`) values ('03','王五');

题目

1、查询“01”课程比“02”课程成绩高的所有学生的学号;
2、查询平均成绩大于60分的同学的学号和平均成绩;
3、查询所有同学的学号、姓名、选课数、总成绩
4、查询姓“李”的老师的个数;
5、查询没学过“张三”老师课的同学的学号、姓名;
6、查询学过编号“01”并且也学过编号“02”课程的同学的学号、姓名;
7、查询学过“张三”老师所教的课的同学的学号、姓名;
8、查询课程编号“01”的成绩比课程编号“02”课程低的所有同学的学号、姓名;
9、查询所有课程成绩小于60分的同学的学号、姓名;
10、查询没有学全所有课的同学的学号、姓名;
11、查询至少有一门课与学号为“01”的同学所学相同的同学的学号和姓名;
12、查询和"01"号的同学学习的课程完全相同的其他同学的学号和姓名
13、把“SC”表中“张三”老师教的课的成绩都更改为此课程的平均成绩;
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
16、检索"01"课程分数小于60,按分数降序排列的学生信息
17、按平均成绩从高到低显示所有学生的平均成绩
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
20、查询学生的总成绩并进行排名
21、查询不同老师所教不同课程平均分从高到低显示
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
24、查询学生平均成绩及其名次
25、查询各科成绩前三名的记录
26、查询每门课程被选修的学生数
27、查询出只选修了一门课程的全部学生的学号和姓名
28、查询男生、女生人数
29、查询名字中含有"风"字的学生信息
30、查询同名同性学生名单,并统计同名人数
31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
37、查询不及格的课程,并按课程号从大到小排列
38、查询课程编号为"01"且课程成绩在60分以上的学生的学号和姓名;
40、查询选修“张三”老师所授课程的学生中,成绩最高的学生姓名及其成绩
42、查询每门功课成绩最好的前两名
43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
44、检索至少选修两门课程的学生学号
45、查询选修了全部课程的学生信息
46、查询各学生的年龄
47、查询本周过生日的学生
48、查询下周过生日的学生
49、查询本月过生日的学生
50、查询下月过生日的学生

参考答案1:

--1查询出学生的编号,姓名,计算机成绩
select student.studentid 编号,studentname 姓名,score 计算机成绩  from student inner join computer on student.studentid = computer.studentid;

select stu.studentid 学生编号,stu.studentname 学生姓名,com.score as 计算机成绩 from student as stu,computer as com where stu.studentid=com.studentid;


--2查询参加过考试的学生信息
select * from student where studentid in(select studentid from computer);


--3查询出学生的编号、姓名、所在班级名称、计算机成绩
select stu.studentid as 学生编号,stu.studentname as 姓名,cl.classname as 班级名称,com.score as 计算机成绩 from class as cl,student as stu,
computer as com where cl.classid=stu.classid and stu.studentid=com.studentid;

--4查询出年龄大于19岁的学生编号、姓名、计算机成绩
select stu.studentid as 学生编号,stu.studentname as 学生姓名,com.score as 计算机成绩 from student as stu,
computer as com where stu.studentid=com.studentid and stu.studentage>19;

--5查询出姓名中包含有c的学生编号、姓名、计算机成绩
select stu.studentid as 学生编号,stu.studentname as 学生姓名,com.score as 计算机成绩 from student as stu,
computer as com where stu.studentid=com.studentid and stu.studentname like '%c%';

--6查询出计算机成绩大于80分的学生编号、姓名、班级名称
select stu.studentid as 学生编号,stu.studentname as 学生姓名,com.score as 计算机成绩 from student as stu,
computer as com where stu.studentid=com.studentid and com.score>80;

--7查询出所有学生的信息和计算机成绩信息
select stu.studentid 编号,stu.studentname 姓名,stu.studentage 年龄,stu.studentsex 性别,stu.studentaddress 地址,stu.classid 班级编号,com.score 成绩 
from student stu 
left join computer as com on stu.studentid=com.studentid;

--8查询出每个班的学生的计算机成绩的平均分,最高分,最低分
select c.classname as 班级,avg(co.score) as 平均分,max(co.score) as 最高分,min(co.score) as 最低分 from class as c 
left join student as s on c.classid=s.classid left join computer as co on s.studentid=co.studentid group by c.classname;

--9查询显示出班级的计算机平均分大于75的班级名称、平均分信息,并按照平均分降序显示
select c.classname as 班级名称,avg(co.score) as 平均分 from class as c inner join student as s on c.classid=s.classid 
inner join computer as co on s.studentid=co.studentid group by c.classname having 平均分>75 order by 平均分 desc;

--10查询出和Jim住在同一个地方的学生的基本信息
select * from student where studentaddress=(select studentaddress from student where studentname='Jim');

--11查询出班级编号大于3的学生基本信息
select * from student where classid>3;

--12查询出班级编号大于3的学生的计算机平均分信息
select avg(co.score) as 平均分 from student as s,computer as co where co.studentid=s.studentid and s.classid>3;

--13查询出班级编号大于3的男生的学生信息
select * from student where classid>3 and studentsex='男';

--14查询男、女生的计算机平均成绩、最高分、最低分
select s.studentsex as 性别, avg(c.score) as 平均分,max(c.score) as 最高分,min(c.score) as 最低分 from student as s 
inner join computer as c on s.studentid=c.studentid group by s.studentsex;

--15将参加过考试的学生的年龄更改为20
update student set studentage=20 where studentid in(select studentid from computer);

--16查询出每个班级的学生的平均分(查询的结果中包含平均分和班级名称)
select cl.classname as 班级名称, avg(co.score) as 平均分 from computer as co,class as cl,
student as s where cl.classid=s.classid and s.studentid=co.studentid group by s.classid;

--17删除姓名包含“c”字符的学生计算机成绩
delete from computer where studentid in (select studentid from student where studentname like '%c%');

--18查询出G1T07班学生的编号、姓名、班级名称、计算机成绩
select s.studentid as 编号,s.studentname as 姓名,c.classname as 班级,co.score as 成绩 from student as s,
class as c,computer as co where s.studentid=co.studentid and c.classid=s.classid and c.classname='G1T07';

--19查询出年龄在20-25之间的学生的编号、姓名、年龄、计算机成绩
select s.studentid as 编号,s.studentname as 姓名,s.studentage as 年龄,c.score as 成绩 from student as s  
inner join computer as c on s.studentid=c.studentid where s.studentage between 20 and 25;

select s.studentid as 编号,s.studentname as 姓名,s.studentage as 年龄,c.score as 成绩 from student as s,
computer as c where s.studentid=c.studentid and s.studentage between 20 and 25;

--20查询出成绩最高的学生的编号、姓名、计算机成绩、所在班级名称
select s.studentid as 编号, s.studentname as 姓名,c.score as 成绩,cl.classname as 班级名称 from  student as s  
inner join computer as c on s.studentid=c.studentid inner join class as cl on cl.classid=s.classid order by c.score desc limit 1;

--21查询统计出每个班的平均分、显示平均分超过70分的信息、并按照降序显示信息
select classname 班级 ,AVG(score) 平均分  from class 
inner join student on class.classid = student.classid 
inner join computer on computer.studentid = student.studentid  group by classname having 平均分>70 order by 平均分 desc;

参考答案2:

1、查询“01”课程比“02”课程成绩高的所有学生的学号;
SELECT DISTINCT t1.sid AS sid FROM (SELECT * FROM sc WHERE cid='01')t1 
LEFT JOIN (SELECT * FROM sc WHERE cid='02')t2 ON t1.sid=t2.sid WHERE t1.score>t2.score

2、查询平均成绩大于60分的同学的学号和平均成绩;
SELECT sid,AVG(score)FROM sc GROUP BY sid HAVING AVG(score>60)

3、查询所有同学的学号、姓名、选课数、总成绩
SELECT student.sid AS sid,sname,COUNT(DISTINCT cid) course_cnt,SUM(score) AS total_score FROM student 
LEFT JOIN sc ON student.sid=sc.sid GROUP BY sid,sname

4、查询姓“李”的老师的个数;
SELECT COUNT(DISTINCT tid) AS teacher_cnt FROM teacher WHERE tname LIKE '李%'

5、查询没学过“张三”老师课的同学的学号、姓名;
SELECT sid,sname FROM student WHERE sid NOT IN(SELECT sc.sid FROM teacher LEFT JOIN course ON teacher.tid=course.tid 
LEFT JOIN sc ON course.cid=sc.cid WHERE teacher.tname='张三' )

6、查询学过“01”并且也学过编号“02”课程的同学的学号、姓名;
SELECT t.sid AS sid ,sname FROM ( SELECT sid ,COUNT(IF(cid='01',score,NULL)) AS count1 ,COUNT(IF(cid='02',score,NULL)) AS count2 FROM sc 
GROUP BY sid HAVING COUNT(IF(cid='01',score,NULL))>0 AND COUNT(IF(cid='02',score,NULL))>0 )t LEFT JOIN student ON t.sid=student.sid

7、查询学过“张三”老师所教的课的同学的学号、姓名;
SELECT student.sid ,sname FROM ( SELECT DISTINCT cid FROM course LEFT JOIN teacher ON course.tid=teacher.tid WHERE teacher.tname='张三' )course 
LEFT JOIN sc ON course.cid=sc.cid LEFT JOIN student ON sc.sid=student.sid GROUP BY student.sid,sname

8、查询课程编号“01”的成绩比课程编号“02”课程低的所有同学的学号、姓名;
SELECT t1.sid,sname FROM ( SELECT DISTINCT t1.sid AS sid FROM (SELECT * FROM sc WHERE cid='01')t1 
LEFT JOIN (SELECT * FROM sc WHERE cid='02')t2 ON t1.sid=t2.sid WHERE t1.score>t2.score )t1 LEFT JOIN student ON t1.sid=student.sid

9、查询所有课程成绩小于60分的同学的学号、姓名;
SELECT t1.sid,sname FROM ( SELECT sid,MAX(score) FROM sc GROUP BY sid HAVING MAX(score<60) )t1 LEFT JOIN student ON t1.sid=student.sid

10、查询没有学全所有课的同学的学号、姓名;
SELECT t1.sid,sname FROM ( SELECT COUNT(cid),sid FROM sc GROUP BY sid HAVING COUNT(cid) < (SELECT COUNT(DISTINCT cid) FROM course) )t1 
LEFT JOIN student ON t1.sid=student.sid

11、查询至少有一门课与学号为“01”的同学所学相同的同学的学号和姓名;
SELECT DISTINCT sc.sid FROM ( SELECT cid FROM sc WHERE sid='01' )t1 LEFT JOIN sc ON t1.cid=sc.cid

12、查询和"01"号的同学学习的课程完全相同的其他同学的学号和姓名
#注意是和'01'号同学课程完全相同但非学习课程数相同的,这里我用左连接解决这个问题
SELECT t1.sid,sname FROM ( SELECT sc.sid ,COUNT(DISTINCT sc.cid) FROM ( SELECT cid FROM sc WHERE sid='01' )t1 
LEFT JOIN sc ON t1.cid=sc.cid GROUP BY sc.sid HAVING COUNT(DISTINCT sc.cid)= (SELECT COUNT(DISTINCT cid) FROM sc WHERE sid = '01') )t1
LEFT JOIN student ON t1.sid=student.sid WHERE t1.sid!='01'

13、把“SC”表中“张三”老师教的课的成绩都更改为此课程的平均成绩;
#暂跳过update题目
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT sname FROM student WHERE sid NOT IN ( SELECT DISTINCT sid FROM sc LEFT JOIN course ON sc.cid=course.cid 
LEFT JOIN teacher ON course.tid=teacher.tid WHERE tname='张三' )

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT t1.sid,sname,avg_score FROM ( SELECT sid,COUNT(IF(score<60,cid,NULL)),AVG(score) AS avg_score FROM sc 
GROUP BY sid HAVING COUNT(IF(score<60,cid,NULL)) >=2 )t1 LEFT JOIN student ON t1.sid=student.sid

16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT sid,IF(cid='01',score,100) FROM sc WHERE IF(cid='01',score,100)<60 ORDER BY IF(cid='01',score,100) DESC

17、按平均成绩从高到低显示所有学生的平均成绩
SELECT sid,AVG(score) FROM sc GROUP BY sid ORDER BY AVG(score) DESC

18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率
SELECT sc.cid ,cname ,MAX(score) max_score ,MIN(score) min_score ,AVG(score) avg_score ,COUNT(IF(score>=60,sid,NULL))/COUNT(sid) pass_rate 
FROM sc 
LEFT JOIN course ON sc.cid=course.cid GROUP BY sc.cid

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
#这里先按照平均成绩排序,再按照及格百分数排序,题目有点奇怪
SELECT cid ,AVG(score) AS avg_score ,COUNT(IF(score>=60,sid,NULL))/COUNT(sid) AS pass_rate FROM sc 
GROUP BY cid ORDER BY avg_score,pass_rate DESC

20、查询学生的总成绩并进行排名
SELECT sid ,SUM(score) AS sum_score FROM sc GROUP BY sid ORDER BY sum_score DESC

21、查询不同老师所教不同课程平均分从高到低显示
SELECT tid ,AVG(score) AS avg_score FROM course LEFT JOIN sc ON course.cid=sc.cid
GROUP BY tid ORDER BY avg_score DESC

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
SELECT sid,rank_num,score,cid 
FROM ( SELECT rank() over(PARTITION BY cid ORDER BY score DESC) AS rank_num ,sid ,score ,cid FROM sc )t 
WHERE rank_num IN (2,3)

23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
SELECT sc.cid ,cname ,
COUNT(IF(score BETWEEN 85 AND 100,sid,NULL))/COUNT(sid) ,
COUNT(IF(score BETWEEN 70 AND 85,sid,NULL))/COUNT(sid) ,
COUNT(IF(score BETWEEN 60 AND 70,sid,NULL))/COUNT(sid) ,
COUNT(IF(score BETWEEN 0 AND 60,sid,NULL))/COUNT(sid)
FROM sc LEFT JOIN course ON sc.cid=course.cid GROUP BY sc.cid,cname

24、查询学生平均成绩及其名次
SELECT sid ,avg_score ,rank() over (ORDER BY avg_score DESC) 
FROM ( SELECT sid ,AVG(score) AS avg_score FROM sc GROUP BY sid )t

25、查询各科成绩前三名的记录
SELECT sid,cid,rank1 
FROM ( SELECT cid ,sid ,rank() over(PARTITION BY cid ORDER BY score DESC) AS rank1 FROM sc )t 
WHERE rank1<=3

26、查询每门课程被选修的学生数
SELECT COUNT(sid) ,cid FROM sc GROUP BY cid

27、查询出只选修了一门课程的全部学生的学号和姓名
#只查出来sid即可,后面懒得交student表
SELECT sid FROM sc GROUP BY sid HAVING COUNT(cid) =1

28、查询男生、女生人数
SELECT ssex ,COUNT(DISTINCT sid) FROM student GROUP BY ssex

29、查询名字中含有"风"字的学生信息
SELECT sid,sname FROM student WHERE sname LIKE '%风%'

30、查询同名同性学生名单,并统计同名人数
#题目有歧义,这套题的质量感觉有点差
SELECT ssex ,sname ,COUNT(sid) FROM student GROUP BY ssex,sname HAVING COUNT(sid)>=2

31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
SELECT sid,sname,sage FROM student WHERE YEAR(sage)=1990

32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
SELECT cid,AVG(score) AS avg_score FROM sc GROUP BY cid ORDER BY avg_score,cid DESC

37、查询不及格的课程,并按课程号从大到小排列
#有问题的题目
SELECT cid,sid,score FROM sc WHERE score<60 ORDER BY cid DESC,sid

38、查询课程编号为"01"且课程成绩在60分以上的学生的学号和姓名;
SELECT sid,cid,score FROM sc WHERE cid='01' AND score>60

40、查询选修“张三”老师所授课程的学生中,成绩最高的学生姓名及其成绩
SELECT sc.sid,sname,cname,score FROM sc LEFT JOIN course ON sc.cid=course.cid 
LEFT JOIN teacher ON course.tid=teacher.tid 
LEFT JOIN student ON sc.sid=student.sid WHERE tname='张三' ORDER BY score DESC LIMIT 1;

42、查询每门功课成绩最好的前两名
SELECT cid,sid,rank1 
FROM ( SELECT cid ,sid ,rank() over(PARTITION BY cid ORDER BY score DESC) AS rank1 FROM sc )t 
WHERE rank1 <=2

43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT cid ,COUNT(sid) AS cnt FROM sc GROUP BY cid HAVING cnt>=5 ORDER BY COUNT(sid) DESC,cid

44、检索至少选修两门课程的学生学号
SELECT sid ,COUNT(cid) FROM sc GROUP BY sid HAVING COUNT(cid)>=2

45、查询选修了全部课程的学生信息
#不太严谨,但实务中应该没问题,如需严谨见12题思路
SELECT sid ,COUNT(cid) FROM sc GROUP BY sid HAVING COUNT(cid)=(SELECT COUNT(DISTINCT cid) FROM sc)

46、查询各学生的年龄
SELECT sid,sname,YEAR(CURDATE())-YEAR(sage) AS sage FROM student

47、查询本周过生日的学生
SELECT sid,sname,sage FROM student WHERE WEEKOFYEAR(sage)=WEEKOFYEAR(CURDATE())

48、查询下周过生日的学生
SELECT sid,sname,sage FROM student WHERE WEEKOFYEAR(sage) = WEEKOFYEAR(DATE_ADD(CURDATE(),INTERVAL 1 WEEK))

49、查询本月过生日的学生
SELECT sid,sname,sage FROM student WHERE MONTH(sage) = MONTH(CURDATE())

50、查询下月过生日的学生
SELECT sid,sname,sage FROM student WHERE MONTH(DATE_SUB(sage,INTERVAL 1 MONTH)) = MONTH(CURDATE())

你可能感兴趣的:(MySQL,mysql,数据库,database)