基础完成
1、查询男生、女生的人数;select gender,count(gender) as gender_num from student group by gender;2、查询姓“张”的学生名单;select sname from student where sname regexp '^张';select sname from student where sname regexp '张{1}';select sname from student where sname like '张%';3、课程平均分从高到低显示select course.cname,avg(score.num) as avg_num from score inner join course where score.course_id = course.cid group by cname order by avg_num desc;4、查询有课程成绩小于60分的同学的学号、姓名;select distinct student.sid,student.sname from score inner join student on score.student_id = student.sid where score.num < 60;5、查询至少有一门课与学号为1的同学所学课程相同的同学的学号和姓名;SELECT DISTINCT student.sid, student.snameFROM score INNER JOIN student ON score.student_id = student.sidWHERE course_id IN ( SELECT course_id FROM score WHERE student_id = 1 );6、查询出只选修了一门课程的全部学生的学号和姓名;select sid,sname from student inner join (select student_id,course_id from score group by student_id having count(score.course_id) = 1) as s_sid on student.sid = s_sid.student_id;7、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;select course_id as '课程ID',max(num) as '最高分' ,min(num) as '最低分' from score group by course_id;select course_id as '课程ID',max(num) as '最高分' ,min(num) as '最低分' from score where score.course_id in (select cid FROM course) group by course_id;select course_id as '课程ID',max(num) as '最高分' ,min(num) as '最低分' from score group by course_id having score.course_id in (select cid FROM course);select concat_ws(',',course_id,max(num),min(num)) as fp from score group by course_id having score.course_id in (select cid FROM course);select concat('课程ID: ',course_id,' 最高分:',max(num),' 最低分:',min(num)) as fp from score group by course_id having score.course_id in (select cid FROM course);8、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;SELECT sid, snameFROM student INNER JOIN (SELECT st_id1, sc1, sc2FROM ( SELECT student_id AS st_id1, num AS sc1 FROM score WHERE course_id = 1 ) AS num_1, ( SELECT student_id AS st_id2, num AS sc2 FROM score WHERE course_id = 2 ) AS num_2WHERE st_id1 = st_id2 AND sc2 < sc1 ) AS sr ON student.sid = sr.st_id1;9、查询“生物”课程比“物理”课程成绩高的所有学生的学号;SELECT sidFROM student INNER JOIN (SELECT stu_swid, sw_num, wl_numFROM (SELECT student_id AS stu_swid, num AS sw_numFROM scoreWHERE score.course_id = ( SELECT cid AS sw_cid FROM course WHERE cname = '生物' ) ) AS sw, (SELECT student_id AS stu_wlid, num AS wl_numFROM scoreWHERE score.course_id = ( SELECT cid AS sw_cid FROM course WHERE cname = '物理' ) ) AS wlWHERE stu_swid = stu_wlid AND sw_num > wl_num ) AS super ON student.sid = super.stu_swid;10、查询平均成绩大于60分的同学的学号和平均成绩;SELECT sid, convert(avg_me.avg_num,decimal(5,2)) as avg_numFROM student INNER JOIN ( SELECT student_id, avg( num ) AS avg_num FROM score GROUP BY student_id HAVING avg_num > 60 ) AS avg_me ON student.sid = avg_me.student_id;11、查询所有同学的学号、姓名、选课数、总成绩;SELECT student.sid, student.sname, count( score.course_id ) AS count_course, sum( num ) AS num_scoreFROM student LEFT JOIN score ON student.sid = score.student_idGROUP BY student.sid12、查询姓“李”的老师的个数;select count(tid) as count_tid from teacher where tname regexp '^李';select count(tid) as count_tid from teacher where tname regexp '李.*';select count(tid) as count_tid from teacher where tname like '李%';13、查询没学过“张磊老师”课的同学的学号、姓名;SELECT sid, snameFROM studentWHERE sid NOT IN (SELECT student_idFROM scoreWHERE course_id = ( SELECT cid FROM course INNER JOIN teacher ON course.teacher_id = teacher.tid WHERE tname = '张磊老师' ) );14、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;SELECT sid, snameFROM studentWHERE sid IN (SELECT cor1.student_idFROM ( ( SELECT student_id, course_id AS cid_1 FROM score WHERE course_id = 1 ) AS cor1, ( SELECT student_id, course_id AS cid_2 FROM score WHERE course_id = 2 ) AS cor2 )WHERE cor1.student_id = cor2.student_id )15、查询学过“李平老师”所教的所有课的同学的学号、姓名;SELECT sid,snameFROM student, (SELECT student_id, group_concat( course_id ORDER BY course_id ASC ) AS grp_coFROM score INNER JOIN ( SELECT cid FROM course INNER JOIN teacher ON course.teacher_id = teacher.tid WHERE tname = '李平老师' ) AS s1 ON course_id = cidGROUP BY student_id ) AS grp_sWHERE student.sid = grp_s.student_id AND grp_co = ( SELECT GROUP_CONCAT( cid ) FROM course INNER JOIN teacher ON course.teacher_id = teacher.tid WHERE tname = '李平老师' );
进阶完成
1、查询没有学全所有课的同学的学号、姓名;# 方法一select student_id,sname from score inner join student on score.student_id = student.sid group by student.sid having count(course_id) < (select count(cid) from course)# 方法二SELECT sid, snameFROM studentWHERE sid IN (SELECT student_idFROM ( SELECT student_id, GROUP_CONCAT( course_id ) AS stu_id FROM score GROUP BY student_id ) AS stu_con, ( SELECT GROUP_CONCAT( cid ORDER BY cid ASC ) AS cor_id FROM course ) AS cor_conWHERE stu_id != cor_id );2、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;SELECT sid, snameFROM studentWHERE sid = (SELECT stu_con.student_idFROM ( SELECT student_id, GROUP_CONCAT( course_id ORDER BY course_id ASC ) AS stu_id FROM score GROUP BY student_id ) AS stu_con, ( SELECT student_id, GROUP_CONCAT( course_id ORDER BY course_id ASC ) AS stu_id FROM score GROUP BY student_id HAVING student_id = 2 ) AS stu2_conWHERE stu_con.stu_id = stu2_con.stu_id AND stu_con.student_id != stu2_con.student_id );3、删除学习“叶平”老师课的SC表记录;# 数据准备一下insert into teacher(tname) values('叶平老师');insert into course(cname,teacher_id) values('心理学',6);insert into score values(53,2,5,97),(54,13,5,95);# 删除数据delete from score where course_id = (select cid from course inner join teacher where course.teacher_id = teacher.tid and teacher.tname = '叶平老师');4、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;select sid from student where sid not in (select student_id from score where course_id = 2 );select avg(num) from score group by course_id having course_id = 2; insert into score values(82,2,2,68),(83,13,2,68),(84,14,2,68),(85,15,2,68),(86,16,2,68);5、按平均成绩从低到高显示所有学生的“生物”、“物理”、“体育”三门的课程成绩,按如下形式显示: 学生ID,生物,物理,体育,有效课程数,有效平均分;SELECT concat_ws( ' ', '学生id:',student.sid, '生物/物理/体育:',group_concat( CASE WHEN score.num IS NOT NULL THEN score.num ELSE concat( 'null' ) END ORDER BY course.cname DESC ), '有效课程数:',count( score.course_id ), '有效平均数:',CONVERT ( avg( score.num ), DECIMAL ( 5, 2 ) ) )FROM student INNER JOIN course ON cname IN ( '生物', '物理', '体育' ) LEFT JOIN score ON student.sid = score.student_id AND course.cid = score.course_idGROUP BY student.sidORDER BY avg( score.num )6、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;# 方法一select concat_ws(',',course_id,max(num),min(num)) as fp from score group by course_id having score.course_id in (select cid FROM course);# 方法二select course_id as '课程ID',max(num) as '最高分' ,min(num) as '最低分' from score group by course_id having score.course_id in (select cid FROM course);# 方法三select concat('课程ID: ',course_id,' 最高分:',max(num),' 最低分:',min(num)) as fp from score group by course_id having score.course_id in (select cid FROM course);7、按各科平均成绩从低到高和及格率的百分数从高到低顺序;# 平均成绩从低到高排序select score.course_id,course.cname,avg(num) as avg_num from score inner join course on score.course_id = course.cid group by score.course_id order by avg_num asc;# 及格率百分数从高到底排序SELECT course_id, convert(sum( CASE WHEN score.num > 60 THEN 1 ELSE 0 END ) / count(1) * 100,DECIMAL(5,2)) as percent FROM score GROUP BY course_id order by percent desc;8、查询各科成绩前三名的记录:(不考虑成绩并列情况)# 方法一SELECT a.course_id,a.cname,SUBSTRING_INDEX(a.num, ',', 3) as score from (SELECT GROUP_CONCAT(num ORDER BY num desc) as num ,course_id,course.cname from score inner join course on score.course_id = course.cid GROUP BY score.course_id ) as a# 方法二select s1.course_id,s1.student_id,s1.num as score,count( DISTINCT s2.num) as sort_numfrom score as s1inner join score as s2on s1.course_id = s2.course_id and s1.num <= s2.numgroup by s1.course_id,s1.num having sort_num <= 3 order by s1.course_id,s1.num desc ;9、查询每门课程被选修的学生数;select score.course_id,course.cname,count(score.student_id) as student_count from score inner join course on score.course_id = course.cid group by score.course_id10、查询同名同姓学生名单,并统计同名人数;insert into student values(17,'女',3,'刘四');select sname,count(sname) as same_num from student group by sname having same_num > 1;11、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;select course_id,CONVERT(avg(num),decimal(5,2)) as avg_num from score group by course_id order by avg_num asc,course_id desc; 12、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;insert into score values(81,14,5,100);# 85分的同学原数据没有,所以手动插入一条,并且将85分条件暂时定为80SELECT sid, sname, avg_numFROM student INNER JOIN ( SELECT student_id, avg( num ) AS avg_num FROM score GROUP BY student_id HAVING avg_num > 80 ) AS avg_sid ON student.sid = avg_sid.student_id;13、查询课程名称为“数学”,且分数低于60的学生姓名和分数;# 没有数学用物理替代# 方法一select st.sname,score.num as score from score inner join course on score.course_id = course.cid and course.cname = '物理' inner join (select sid,sname from student) as st on st.sid = score.student_id where num < 60# 方法二SELECT sname, num AS scoreFROM student INNER JOIN (SELECT student_id, numFROM score INNER JOIN ( SELECT cid FROM course WHERE cname = '物理' ) AS c ON course_id = c.cidWHERE num < 60 ) AS cc ON student.sid = cc.student_id;14、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;# 方法一select student.sid,student.sname from score inner join student on student_id = student.sid where course_id = 3 and num > 80;# 方法二SELECT sid, snameFROM student INNER JOIN (SELECT student_id, numFROM score INNER JOIN ( SELECT cid FROM course WHERE cid = '3' ) AS c ON course_id = c.cidWHERE num > 80 ) AS score_8 ON student.sid = score_8.student_id;15、求选了课程的学生人数SELECT count( studentd.student_id ) AS same_count_stuFROM ( SELECT student_id FROM score GROUP BY student_id HAVING count( course_id ) > 0 ) AS studentd;16、查询选修“杨艳”老师所授课程的学生中,成绩最高的学生姓名及其成绩;SELECT st.sname, s.numFROM score s INNER JOIN course c ON c.cid = s.course_id INNER JOIN teacher t ON t.tid = c.teacher_id INNER JOIN student st ON st.sid = s.student_idWHERE t.tname = '刘海燕老师'ORDER BY s.num DESC LIMIT 1;17、查询各个课程及相应的选修人数;SELECT cid, cname, count( student_id ) AS count_stFROM course c INNER JOIN score s ON c.cid = s.course_idGROUP BY s.course_id;18、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;SELECT score.student_id, GROUP_CONCAT( score.course_id ) cor_id, score.numFROM score INNER JOIN score c ON score.num = c.num AND score.course_id != c.course_id AND score.student_id = c.student_idGROUP BY score.student_id;19、查询每门课程成绩最好的前两名;select SUBSTRING_INDEX(cc.st_id, ',', 2) as stid,SUBSTRING_INDEX(cc.sc, ',', 2) as sc from (select group_concat(student_id order by num desc) as st_id,group_concat(num order by num desc) as sc from score sc1 where (select count(1) from score sc2 where sc2.course_id = sc1.course_id and sc2.num > sc1.num ) <=2 group by sc1.course_id order by sc1.course_id asc,sc1.num desc) as cc;20、检索至少选修两门课程的学生学号;select student_id from score group by student_id having count(course_id) > 1;21、查询全部学生都选修的课程的课程号和课程名;select course.cid,course.cname from score inner join course on score.course_id = course.cid group by cid,cname having count(student_id) = (select count(sid) from student);22、查询没学过“叶平”老师讲授的任一门课程的学生姓名;select sname from student where sid not in (select student_id from score inner join course on score.course_id = course.cid inner join teacher on tname = '李平老师' and teacher_id = tid group by student_id);23、查询两门以上不及格课程的同学的学号及其平均成绩;# 方法一select score.student_id,avg(num) avg_num from score inner join (select student_id from score where num < 60 group by student_id having count(student_id) > 1) as sm on score.student_id = sm.student_id group by score.student_id# 方法二select student_id,avg(num) avg_num from score where student_id in (select student_id from score where num < 60 group by student_id having count(student_id) > 1) group by score.student_id ;24、检索“004”课程分数小于60,按分数降序排列的同学学号;select student_id from score where course_id = 4 and num < 60 order by num desc;25、删除“002”同学的“001”课程的成绩;delete from score where student_id = 2 and course_id = 1;