1.表结构如下:

数据脚本2.查询:
- 查询学习课程"python"比课程 "java" 成绩高的学生的学号;
123456789
#先查询"python"课程和"java"课程的学生成绩,临时表#让两个临时表进行比较-- select * from course c,score s where c.c_id = s.c_id and c.c_name='python';-- select * from course c,score s where c.c_id = s.c_id and c.c_name='java';selectA.s_idfrom(selects.s_id,s.numfromcourse c,score swherec.c_id = s.c_idandc.c_name='python')ASA ,(selects.s_id,s.numfromcourse c,score swherec.c_id = s.c_idandc.c_name='java')ASBwhereA.s_id = B.s_idandA.num > B.num; - 查询平均成绩大于65分的同学的姓名和平均成绩(保留两位小数);
12
selectround(avg(num),2)asnum,student.s_namefromscore sLEFTJOINstudentONs.s_id = student.s_idgroupbys.s_idhavingnum > 65;
- 查询所有同学的姓名、选课数、总成绩;
1234
#先来分析需要哪些表:学生表/成绩表#然后进行多表查询即可selects_name,count(*)'选课数',sum(num)as'总成绩'fromstudent st,score swherest.s_id = s.s_idGROUPBYs.s_id; - 查询所有的课程的名称以及对应的任课老师姓名;
1
selectc_name,t_namefromcourse,teacherwherecourse.t_id = teacher.t_id; - 查询没学过“alex”老师课的同学的姓名;
123456789
#先看看alex教什么课程#看看谁学了alex的课程#最后把学了的人过滤掉就是没学过的学生-- select c_id from teacher t, course c where t.t_id = c.t_id and t.t_name ='alex';-- select s_id from score where c_id in(2,4);selects_namefromstudentwheres_idnotin(selects_idfromscorewherec_idin(2,4)); - 查询学过'python'并且也学过编号'java'课程的同学的姓名;
123456
-- select * from score where score.c_id='1' and score.c_id='2'#查询python和java课程号-- select c_id from course where course.c_name in('python','java');SELECTst.s_namefromscore s ,student stwheres.s_id = st.s_idANDs.c_idin(1,2)GROUPBYs.s_idHAVINGCOUNT(*) = 2; - 查询学过“alex”老师所教的全部课程的同学的姓名;
12345678910
#先知道alex老师教什么课程#然后来看看学了alex课程的学生有哪些人#按学生分组,看看谁学的课程数 = alex老师教授的课程数-- select c_id from teacher t, course c where t.t_id = c.t_id and t.t_name ='alex';selectstudent.s_namefromscore,studentwherescore.s_id =student.s_idandscore.c_idin(selectc_idfromteacher t, course cwheret.t_id = c.t_idandt.t_name ='alex')GROUPBYscore.s_idHAVINGcount(*) = (selectcount(*)fromteacher t, course cwheret.t_id = c.t_idandt.t_name ='alex'); - 查询挂科超过两门(包括两门)的学生姓名;
12
SELECTstudent.s_namefromscore,studentwherescore.s_id = student.s_idandscore.num <60GROUPBYstudent.s_idHAVINGcount(*)>=2; - 查询有课程成绩小于60分的同学的姓名;
12
SELECTDISTINCTstudent.s_namefromscore,studentwherescore.s_id = student.s_idandscore.num <60; - 查询选修了全部课程的学生姓名;
1234
-- select count(*) from course;selectstudent.s_namefromscore,studentwherescore.s_id = student.s_idGROUPBYscore.s_idHAVINGcount(*) = (selectcount(*)fromcourse) - 查询至少有一门课程与“貂蝉”同学所学课程相同的同学姓名;
123456
-- SELECT c_id from score,student where score.s_id =student.s_id and student.s_name='貂蝉'selectstudent.s_namefromscore,studentwherescore.s_id = student.s_idandscore.c_idin(SELECTc_idfromscore,studentwherescore.s_id =student.s_idandstudent.s_name='貂蝉')andstudent.s_name <>'貂蝉'GROUPBYstudent.s_id; - 查询学过'貂蝉'同学全部课程 的其他同学姓名;
123456
-- SELECT c_id from score,student where score.s_id =student.s_id and student.s_name='貂蝉'selectstudent.s_name,count(*)fromscore,studentwherescore.s_id = student.s_idandscore.c_idin(SELECTc_idfromscore,studentwherescore.s_id =student.s_idandstudent.s_name='貂蝉')andstudent.s_name <>'貂蝉'GROUPBYstudent.s_idHAVINGcount(*) = (SELECTcount(*)fromscore,studentwherescore.s_id =student.s_idandstudent.s_name='貂蝉'); - 查询和'貂蝉'同学学习的课程完全相同的,其他同学姓名;
123456789101112131415161718192021222324252627282930
解题思路:#1. 找出与'貂蝉'学习课程数 相同的学生s_id (你学两门,我也学两门)#2. 再找出学过'貂蝉'课程的学生,剩下的一定是至少学过一门'貂蝉'课程的学生#3. 再根据学生ID进行分组,剩下学生数count(1) = 貂蝉学生所学课程数#1.找出与'貂蝉'学习课程数 相同的学生s_id (你学两门,我也学两门)select*FROMscorewherescore.s_idin(selects_idfromscoreGROUPBYscore.s_idHAVINGcount(*) = (selectcount(*)fromstudent,scorewherestudent.s_id = score.s_idandstudent.s_name='貂蝉'))#2.然后再找出学过'貂蝉'课程的学生,剩下的一定是至少学过一门'貂蝉'课程的学生select*FROMscorewherescore.s_idin(selects_idfromscoreGROUPBYscore.s_idHAVINGcount(*) = (selectcount(*)fromstudent,scorewherestudent.s_id = score.s_idandstudent.s_name='貂蝉'))andscore.c_idin(selectc_idfromstudent,scorewherestudent.s_id = score.s_idandstudent.s_name='貂蝉')#3.再根据学生ID进行分组,剩下学生数count(1) = 貂蝉学生所学课程数select*FROMscorewherescore.s_idin(selects_idfromscoreGROUPBYscore.s_idHAVINGcount(*) = (selectcount(*)fromstudent,scorewherestudent.s_id = score.s_idandstudent.s_name='貂蝉'))andscore.c_idin(selectc_idfromstudent,scorewherestudent.s_id = score.s_idandstudent.s_name='貂蝉')GROUPBYscore.s_idHAVINGcount(*) =(selectcount(*)fromstudent,scorewherestudent.s_id = score.s_idandstudent.s_name='貂蝉')andscore.s_id !=2; - 按平均成绩倒序显示所有学生的“python”、“java”、“linux”三门的课程成绩,按如下形式显示: 学生ID,python,java,linux,课程数,平均分
123456789101112
#1.先查询单一学生的python课程分数selectnumfromscore,coursewherescore.c_id = course.c_idANDcourse.c_name ='python'andscore.s_id = 1;#2.将上面查询的结果作为 列字段使用selects.s_id,(selectnumfromscore,coursewherescore.c_id = course.c_idANDcourse.c_name ='python'andscore.s_id = s.s_id )as'python',(selectnumfromscore,coursewherescore.c_id = course.c_idANDcourse.c_name ='java'andscore.s_id = s.s_id )as'java',(selectnumfromscore,coursewherescore.c_id = course.c_idANDcourse.c_name ='linux'andscore.s_id = s.s_id )as'linux',count(c_id)as'课程数',avg(num)as'平均分'fromscore sGROUPBYs.s_id; - 统计各科各分数段人数.显示格式:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
123456
selectscore.c_id,course.c_name,sum(CASEWHENnumBETWEEN85and100THEN1ELSE0END)as'[100-85]',sum(CASEWHENnumBETWEEN70and85THEN1ELSE0END)as'[85-70]',sum(CASEWHENnumBETWEEN60and70THEN1ELSE0END)as'[70-60]',sum(CASEWHENnum < 60THEN1ELSE0END)as'[ <60]'fromscore,coursewherescore.c_id=course.c_idGROUPBYscore.c_id; - 查询每门课程被选修的次数
1
selectc_name,count(*)fromcourse,scorewherecourse.c_id = score.c_idGROUPBYscore.c_id; - 查询出只选修了一门课程的学生的学号和姓名
12
selectstudent.s_id,student.s_namefromstudent,scorewherestudent.s_id = score.s_idGROUPBYscore.s_idHAVINGcount(*)=1 - 查询学生表中男生、女生各有多少人
12345
注意:不用groupby分组selectsum(CASEWHENs_sex ='男'THEN1ELSE0END)as'男生',sum(CASEWHENs_sex ='女'THEN1ELSE0END)as'女生'FROMstudent - 查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
12
selectcourse.c_name,avg(num)as'平均成绩'fromcourse,scorewherecourse.c_id = score.c_idGROUPBYscore.c_idORDERBYavg(num),score.c_iddesc; - 查询课程名称为“python”,且分数低于60的学生姓名和分数
12
selectstudent.s_name,score.numfromscore,course,studentwherescore.c_id = course.c_idandstudent.s_id = score.s_idandcourse.c_name ='python'andscore.num < 67
- 查询学习课程"python"比课程 "java" 成绩高的学生的学号;
本文提供了多种复杂的SQL查询实例,涵盖成绩对比、条件筛选、多表连接等实用场景,帮助读者掌握高级SQL查询技巧。
4381

被折叠的 条评论
为什么被折叠?



