数据库SQL面试题2(关于学生、课程、成绩、教师四张表)
下面和大家分享一个比较经典的场景,数据库的面试题目,主要的表是学生、课程、成绩、教师四张表,本示例的特点是有模拟数据,加深理解和印象,答案主要基于Oracle来实现的
1、四张表分别为:
Student(S#,Sname,Sage,Ssex) 学生表 S#:学号;Sname:学生姓名;Sage:学生年龄;Ssex:学生性别
Course(C#,Cname,T#) 课程表 C#:课程编号;Cname:课程名字;T#:教师编号
SC(S#,C#,score) 成绩表 S#:学号;C#,课程编号;score:成绩
Teacher(T#,Tname) 教师表 T#:教师编号; Tname:教师名字
2、题目要求
1、查询“111”课程比“112”课程成绩高的所有学生的学号; 2、查询平均成绩大于60分的同学的学号和平均成绩; 3、查询所有同学的学号、姓名、选课数、总成绩; 4、查询姓“李”的老师的个数; 5、查询没学过'陈奕迅'老师课的同学的学号、姓名; 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名 9、查询所有课程成绩小于60分的同学的学号、姓名 10、查询没有学全所有课的同学的学号、姓名 11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名 12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名 13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩 14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名 15、删除学习'陈奕迅'老师课的SC表记录 16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩 17、按平均成绩从高到低显示所有学生的“语文1”、“生物2”、“化学1”三门的课程成绩,按如下形式显示 18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 19、按各科平均成绩从低到高和及格率的百分数从高到低顺序 20、查询如下课程平均成绩和及格率的百分数(用"1行"显示) 21、查询不同老师所教不同课程平均分从高到低显示 22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:语文1(111),语文2(112),数学1 (113),数学2(114) 23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85] 优秀人数,[85-70] 良好人数,[70-60] 一般人数,[ <60] 刚及格人数 24、查询学生平均成绩及其名次 25、查询各科成绩前三名的记录:(不考虑成绩并列情况) 26、查询每门课程被选修的学生数 27、查询出只选修了一门课程的全部学生的学号和姓名 28、查询男生、女生人数 29、查询名字中有'黑'的学生名单 30、查询同名同性学生名单,并统计同名人数
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩 34、查询课程名称为'语文1',且分数低于60的学生姓名和分数 35、查询所有学生的选课情况 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数 37、查询不及格的课程,并按课程号从大到小排列 38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名 39、求选了课程的学生人数 40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩 41、查询各个课程及相应的选修人数 42、查询不同课程成绩相同的学生的学号、课程号、学生成绩 43、查询每门功成绩最好的前两名 44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列 45、检索至少选修两门课程的学生学号 46、查询全部学生都选修的课程的课程号和课程名 47、查询没学过'陈奕迅'老师讲授的任一门课程的学生姓名 48、查询两门以上不及格课程的同学的学号及其平均成绩 49、检索'114'课程分数小于60,按分数降序排列的同学学号 50、删除'2'同学的'111'课程的成绩
3、在数据库建表
/*====================================*/ /*===== CREATE TABLE ======*/ /*====================================*/ --学生表 CREATE TABLE STUDENT ( S# NUMBER(12,0) PRIMARY KEY, SNAME VARCHAR2(10) NOT NULL, SAGE NUMBER(3,0) NOT NULL, SSEX CHAR(2) NOT NULL ); COMMENT ON TABLE STUDENT IS '学生信息表'; COMMENT ON COLUMN STUDENT.S# IS '学号'; COMMENT ON COLUMN STUDENT.SNAME IS '学生姓名'; COMMENT ON COLUMN STUDENT.SAGE IS '学生年龄'; COMMENT ON COLUMN STUDENT.SSEX IS '学生性别'; --课程表 CREATE TABLE COURSE ( C# NUMBER(12,0) PRIMARY KEY, CNAME VARCHAR2(10) NOT NULL, T# NUMBER(12,0) NOT NULL ); COMMENT ON TABLE COURSE IS '课程表'; COMMENT ON COLUMN COURSE.C# IS '课程编号'; COMMENT ON COLUMN COURSE.CNAME IS '课程名字'; COMMENT ON COLUMN COURSE.T# IS '教师编号'; --成绩表 CREATE TABLE SC ( S# NUMBER(12,0), C# NUMBER(12,0), SCORE NUMBER(3,0) NOT NULL ); ALTER TABLE SC ADD CONSTRAINTS PK_SC PRIMARY KEY (S#,C#); COMMENT ON TABLE SC IS '成绩表'; COMMENT ON COLUMN SC.S# IS '学号'; COMMENT ON COLUMN SC.C# IS '课程编号'; COMMENT ON COLUMN SC.SCORE IS '成绩'; --教师表 CREATE TABLE TEACHER ( T# NUMBER(12,0) PRIMARY KEY, TNAME VARCHAR2(10) NOT NULL ); COMMENT ON TABLE TEACHER IS '教师表'; COMMENT ON COLUMN TEACHER.T# IS '教师编号'; COMMENT ON COLUMN TEACHER.TNAME IS '教师名字';
*注:别的数据库将备注修改成相应的格式
4、模拟数据
学生表
--学生表 INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(1,'小红',10,'女'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(2,'小明',12,'男'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(3,'小绿',13,'男'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(4,'小蓝',15,'男'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(5,'小青',11,'男'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(6,'小白',10,'女'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(7,'小紫',14,'女'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(8,'小黄',13,'男'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(9,'小粉',12,'女'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(10,'小橙',11,'女'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(11,'小黑',9,'男'); INSERT INTO STUDENT(S#,SNAME,SAGE,SSEX) VALUES(12,'小粉',14,'男');
课程表
INSERT INTO COURSE(C#,CNAME,T#) VALUES(111,'语文1',50); INSERT INTO COURSE(C#,CNAME,T#) VALUES(112,'语文2',51); INSERT INTO COURSE(C#,CNAME,T#) VALUES(113,'数学1',52); INSERT INTO COURSE(C#,CNAME,T#) VALUES(114,'数学2',53); INSERT INTO COURSE(C#,CNAME,T#) VALUES(115,'英语1',54); INSERT INTO COURSE(C#,CNAME,T#) VALUES(116,'英语2',55); INSERT INTO COURSE(C#,CNAME,T#) VALUES(117,'生物1',56); INSERT INTO COURSE(C#,CNAME,T#) VALUES(118,'生物2',57); INSERT INTO COURSE(C#,CNAME,T#) VALUES(119,'物理1',58); INSERT INTO COURSE(C#,CNAME,T#) VALUES(120,'物理2',59); INSERT INTO COURSE(C#,CNAME,T#) VALUES(121,'化学1',60); INSERT INTO COURSE(C#,CNAME,T#) VALUES(122,'化学2',61); INSERT INTO COURSE(C#,CNAME,T#) VALUES(123,'政治1',50); INSERT INTO COURSE(C#,CNAME,T#) VALUES(124,'政治2',54); INSERT INTO COURSE(C#,CNAME,T#) VALUES(125,'历史1',58); INSERT INTO COURSE(C#,CNAME,T#) VALUES(126,'历史2',53); INSERT INTO COURSE(C#,CNAME,T#) VALUES(127,'地理1',56); INSERT INTO COURSE(C#,CNAME,T#) VALUES(128,'地理2',60);
成绩表
INSERT INTO SC(S#,C#,SCORE) VALUES(1,111,69); INSERT INTO SC(S#,C#,SCORE) VALUES(1,112,80); INSERT INTO SC(S#,C#,SCORE) VALUES(1,113,75); INSERT INTO SC(S#,C#,SCORE) VALUES(1,114,79); INSERT INTO SC(S#,C#,SCORE) VALUES(1,115,63); INSERT INTO SC(S#,C#,SCORE) VALUES(1,116,90); INSERT INTO SC(S#,C#,SCORE) VALUES(1,117,95); INSERT INTO SC(S#,C#,SCORE) VALUES(1,118,77); INSERT INTO SC(S#,C#,SCORE) VALUES(1,119,60); INSERT INTO SC(S#,C#,SCORE) VALUES(1,120,83); INSERT INTO SC(S#,C#,SCORE) VALUES(1,121,88); INSERT INTO SC(S#,C#,SCORE) VALUES(1,122,75); INSERT INTO SC(S#,C#,SCORE) VALUES(1,123,50); INSERT INTO SC(S#,C#,SCORE) VALUES(1,124,44); INSERT INTO SC(S#,C#,SCORE) VALUES(1,125,77); INSERT INTO SC(S#,C#,SCORE) VALUES(1,127,79); INSERT INTO SC(S#,C#,SCORE) VALUES(1,128,77); INSERT INTO SC(S#,C#,SCORE) VALUES(2,111,77); INSERT INTO SC(S#,C#,SCORE) VALUES(2,112,68); INSERT INTO SC(S#,C#,SCORE) VALUES(2,113,76); INSERT INTO SC(S#,C#,SCORE) VALUES(2,114,95); INSERT INTO SC(S#,C#,SCORE) VALUES(2,115,90); INSERT INTO SC(S#,C#,SCORE) VALUES(2,116,57); INSERT INTO SC(S#,C#,SCORE) VALUES(2,117,99); INSERT INTO SC(S#,C#,SCORE) VALUES(2,118,86); INSERT INTO SC(S#,C#,SCORE) VALUES(2,120,73); INSERT INTO SC(S#,C#,SCORE) VALUES(2,121,73); INSERT INTO SC(S#,C#,SCORE) VALUES(2,122,82); INSERT INTO SC(S#,C#,SCORE) VALUES(2,123,67); INSERT INTO SC(S#,C#,SCORE) VALUES(2,124,69); INSERT INTO SC(S#,C#,SCORE) VALUES(2,125,53); INSERT INTO SC(S#,C#,SCORE) VALUES(2,126,64); INSERT INTO SC(S#,C#,SCORE) VALUES(2,127,75); INSERT INTO SC(S#,C#,SCORE) VALUES(2,128,71); INSERT INTO SC(S#,C#,SCORE) VALUES(3,111,82); INSERT INTO SC(S#,C#,SCORE) VALUES(3,112,67); INSERT INTO SC(S#,C#,SCORE) VALUES(3,113,69); INSERT INTO SC(S#,C#,SCORE) VALUES(3,114,44); INSERT INTO SC(S#,C#,SCORE) VALUES(3,115,77); INSERT INTO SC(S#,C#,SCORE) VALUES(3,116,98); INSERT INTO SC(S#,C#,SCORE) VALUES(3,117,79); INSERT INTO SC(S#,C#,SCORE) VALUES(3,118,77); INSERT INTO SC(S#,C#,SCORE) VALUES(3,119,68); INSERT INTO SC(S#,C#,SCORE) VALUES(3,120,76); INSERT INTO SC(S#,C#,SCORE) VALUES(3,121,95); INSERT INTO SC(S#,C#,SCORE) VALUES(3,122,73); INSERT INTO SC(S#,C#,SCORE) VALUES(3,123,82); INSERT INTO SC(S#,C#,SCORE) VALUES(3,124,63); INSERT INTO SC(S#,C#,SCORE) VALUES(3,125,90); INSERT INTO SC(S#,C#,SCORE) VALUES(3,126,95); INSERT INTO SC(S#,C#,SCORE) VALUES(3,127,77); INSERT INTO SC(S#,C#,SCORE) VALUES(3,128,60); INSERT INTO SC(S#,C#,SCORE) VALUES(4,111,77); INSERT INTO SC(S#,C#,SCORE) VALUES(4,112,68); INSERT INTO SC(S#,C#,SCORE) VALUES(4,113,76); INSERT INTO SC(S#,C#,SCORE) VALUES(4,114,95); INSERT INTO SC(S#,C#,SCORE) VALUES(4,115,73); INSERT INTO SC(S#,C#,SCORE) VALUES(4,116,82); INSERT INTO SC(S#,C#,SCORE) VALUES(4,117,76); INSERT INTO SC(S#,C#,SCORE) VALUES(4,118,95); INSERT INTO SC(S#,C#,SCORE) VALUES(4,119,90); INSERT INTO SC(S#,C#,SCORE) VALUES(4,120,57); INSERT INTO SC(S#,C#,SCORE) VALUES(4,121,69); INSERT INTO SC(S#,C#,SCORE) VALUES(4,122,80); INSERT INTO SC(S#,C#,SCORE) VALUES(4,123,75); INSERT INTO SC(S#,C#,SCORE) VALUES(4,124,79); INSERT INTO SC(S#,C#,SCORE) VALUES(4,125,68); INSERT INTO SC(S#,C#,SCORE) VALUES(4,126,54); INSERT INTO SC(S#,C#,SCORE) VALUES(4,127,77); INSERT INTO SC(S#,C#,SCORE) VALUES(4,128,90); INSERT INTO SC(S#,C#,SCORE) VALUES(5,111,77); INSERT INTO SC(S#,C#,SCORE) VALUES(5,112,98); INSERT INTO SC(S#,C#,SCORE) VALUES(5,113,79); INSERT INTO SC(S#,C#,SCORE) VALUES(5,115,68); INSERT INTO SC(S#,C#,SCORE) VALUES(5,116,85); INSERT INTO SC(S#,C#,SCORE) VALUES(5,117,83); INSERT INTO SC(S#,C#,SCORE) VALUES(5,118,81); INSERT INTO SC(S#,C#,SCORE) VALUES(5,119,95); INSERT INTO SC(S#,C#,SCORE) VALUES(5,120,90); INSERT INTO SC(S#,C#,SCORE) VALUES(5,121,57); INSERT INTO SC(S#,C#,SCORE) VALUES(5,122,69); INSERT INTO SC(S#,C#,SCORE) VALUES(5,124,75); INSERT INTO SC(S#,C#,SCORE) VALUES(5,125,75); INSERT INTO SC(S#,C#,SCORE) VALUES(5,126,70); INSERT INTO SC(S#,C#,SCORE) VALUES(5,127,53); INSERT INTO SC(S#,C#,SCORE) VALUES(5,128,50); INSERT INTO SC(S#,C#,SCORE) VALUES(6,113,90); INSERT INTO SC(S#,C#,SCORE) VALUES(6,114,57); INSERT INTO SC(S#,C#,SCORE) VALUES(6,115,69); INSERT INTO SC(S#,C#,SCORE) VALUES(6,116,80); INSERT INTO SC(S#,C#,SCORE) VALUES(6,117,60); INSERT INTO SC(S#,C#,SCORE) VALUES(6,118,73); INSERT INTO SC(S#,C#,SCORE) VALUES(6,120,73); INSERT INTO SC(S#,C#,SCORE) VALUES(6,121,76); INSERT INTO SC(S#,C#,SCORE) VALUES(6,122,95); INSERT INTO SC(S#,C#,SCORE) VALUES(6,123,90); INSERT INTO SC(S#,C#,SCORE) VALUES(6,124,57); INSERT INTO SC(S#,C#,SCORE) VALUES(6,125,69); INSERT INTO SC(S#,C#,SCORE) VALUES(6,126,80); INSERT INTO SC(S#,C#,SCORE) VALUES(6,128,74); INSERT INTO SC(S#,C#,SCORE) VALUES(7,111,57); INSERT INTO SC(S#,C#,SCORE) VALUES(7,112,69); INSERT INTO SC(S#,C#,SCORE) VALUES(7,114,60); INSERT INTO SC(S#,C#,SCORE) VALUES(7,115,73); INSERT INTO SC(S#,C#,SCORE) VALUES(7,116,90); INSERT INTO SC(S#,C#,SCORE) VALUES(7,124,57); INSERT INTO SC(S#,C#,SCORE) VALUES(7,125,43); INSERT INTO SC(S#,C#,SCORE) VALUES(7,126,65); INSERT INTO SC(S#,C#,SCORE) VALUES(7,127,73); INSERT INTO SC(S#,C#,SCORE) VALUES(7,128,80); INSERT INTO SC(S#,C#,SCORE) VALUES(8,111,71); INSERT INTO SC(S#,C#,SCORE) VALUES(8,112,69); INSERT INTO SC(S#,C#,SCORE) VALUES(8,113,77); INSERT INTO SC(S#,C#,SCORE) VALUES(8,114,81); INSERT INTO SC(S#,C#,SCORE) VALUES(8,115,95); INSERT INTO SC(S#,C#,SCORE) VALUES(8,116,90); INSERT INTO SC(S#,C#,SCORE) VALUES(8,117,57); INSERT INTO SC(S#,C#,SCORE) VALUES(8,119,65); INSERT INTO SC(S#,C#,SCORE) VALUES(8,120,73); INSERT INTO SC(S#,C#,SCORE) VALUES(8,121,80); INSERT INTO SC(S#,C#,SCORE) VALUES(8,122,66); INSERT INTO SC(S#,C#,SCORE) VALUES(8,123,80); INSERT INTO SC(S#,C#,SCORE) VALUES(8,124,60); INSERT INTO SC(S#,C#,SCORE) VALUES(8,125,73); INSERT INTO SC(S#,C#,SCORE) VALUES(8,126,90); INSERT INTO SC(S#,C#,SCORE) VALUES(8,127,73); INSERT INTO SC(S#,C#,SCORE) VALUES(8,128,76); INSERT INTO SC(S#,C#,SCORE) VALUES(9,111,68); INSERT INTO SC(S#,C#,SCORE) VALUES(9,112,72); INSERT INTO SC(S#,C#,SCORE) VALUES(9,114,45); INSERT INTO SC(S#,C#,SCORE) VALUES(9,115,73); INSERT INTO SC(S#,C#,SCORE) VALUES(9,116,80); INSERT INTO SC(S#,C#,SCORE) VALUES(9,117,66); INSERT INTO SC(S#,C#,SCORE) VALUES(9,118,80); INSERT INTO SC(S#,C#,SCORE) VALUES(9,121,92); INSERT INTO SC(S#,C#,SCORE) VALUES(9,122,57); INSERT INTO SC(S#,C#,SCORE) VALUES(9,123,69); INSERT INTO SC(S#,C#,SCORE) VALUES(9,124,80); INSERT INTO SC(S#,C#,SCORE) VALUES(9,125,60); INSERT INTO SC(S#,C#,SCORE) VALUES(9,126,73); INSERT INTO SC(S#,C#,SCORE) VALUES(9,127,79); INSERT INTO SC(S#,C#,SCORE) VALUES(10,111,71); INSERT INTO SC(S#,C#,SCORE) VALUES(10,112,69); INSERT INTO SC(S#,C#,SCORE) VALUES(10,113,77); INSERT INTO SC(S#,C#,SCORE) VALUES(10,114,81); INSERT INTO SC(S#,C#,SCORE) VALUES(10,116,96); INSERT INTO SC(S#,C#,SCORE) VALUES(10,117,50); INSERT INTO SC(S#,C#,SCORE) VALUES(10,118,40); INSERT INTO SC(S#,C#,SCORE) VALUES(10,119,63); INSERT INTO SC(S#,C#,SCORE) VALUES(10,120,92); INSERT INTO SC(S#,C#,SCORE) VALUES(10,121,57); INSERT INTO SC(S#,C#,SCORE) VALUES(10,122,69); INSERT INTO SC(S#,C#,SCORE) VALUES(10,123,80); INSERT INTO SC(S#,C#,SCORE) VALUES(10,124,62); INSERT INTO SC(S#,C#,SCORE) VALUES(10,125,73); INSERT INTO SC(S#,C#,SCORE) VALUES(10,127,92); INSERT INTO SC(S#,C#,SCORE) VALUES(10,128,73); INSERT INTO SC(S#,C#,SCORE) VALUES(11,124,99); INSERT INTO SC(S#,C#,SCORE) VALUES(12,122,100); INSERT INTO SC(S#,C#,SCORE) VALUES(12,126,99); INSERT INTO SC(S#,C#,SCORE) VALUES(12,124,97);
教师表
INSERT INTO TEACHER(T#,TNAME) VALUES(50,'李冰冰'); INSERT INTO TEACHER(T#,TNAME) VALUES(51,'黄一山'); INSERT INTO TEACHER(T#,TNAME) VALUES(52,'陈小春'); INSERT INTO TEACHER(T#,TNAME) VALUES(53,'赵铁柱'); INSERT INTO TEACHER(T#,TNAME) VALUES(54,'钱中书'); INSERT INTO TEACHER(T#,TNAME) VALUES(55,'孙悟空'); INSERT INTO TEACHER(T#,TNAME) VALUES(56,'李连杰'); INSERT INTO TEACHER(T#,TNAME) VALUES(57,'杨千嬅'); INSERT INTO TEACHER(T#,TNAME) VALUES(58,'泽日生'); INSERT INTO TEACHER(T#,TNAME) VALUES(59,'林夕'); INSERT INTO TEACHER(T#,TNAME) VALUES(60,'陈奕迅'); INSERT INTO TEACHER(T#,TNAME) VALUES(61,'刘备');
*插入后记得提交数据
5、开始题目
/*====================================*/ /*=========== 习题开始 =============*/ /*====================================*/ --1、查询“111”课程比“112”课程成绩高的所有学生的学号; /* EXISTS 版本 */ SELECT t1.s# FROM sc t1 WHERE t1.c# = 111 AND EXISTS ( SELECT 1 FROM sc t2 WHERE t2.c# = 112 AND t1.s# = t2.s# --要求同一个学生 所以有t1.s# = t2.s# AND t1.score > t2.score ); /* 子查询版本 */ SELECT a.s# FROM (SELECT t1.s#,t1.score FROM sc t1 WHERE t1.c# = 111) a INNER JOIN (SELECT t2.s#,t2.score FROM sc t2 WHERE t2.c# = 112) b ON a.s# = b.s# WHERE a.score > b.score; --2、查询平均成绩大于60分的同学的学号和平均成绩; SELECT t1.s# AS ST_CODE, AVG(t1.score) AS CU_AVG FROM sc t1 GROUP BY t1.s# HAVING AVG(t1.score) > 60 --3、查询所有同学的学号、姓名、选课数、总成绩; /* group by 的时候要注意选出来的字段是不是都是聚合函数或者分组的字段 */ SELECT t1.s# AS ST_DOCE, MIN(t1.sname) AS ST_NAME, COUNT(t2.c#) AS CU_NUM, SUM(t2.score) AS CU_SUM FROM STUDENT t1 LEFT JOIN SC t2 ON t1.s# = t2.s# GROUP BY t1.s# --4、查询姓“李”的老师的个数; SELECT COUNT(t1.t#) AS 李_NUM FROM teacher t1 WHERE t1.tname LIKE '李%' --5、查询没学过'陈奕迅'老师课的同学的学号、姓名; /* 一对多关系的就会出现比较坑爹的情况,记得用distinct并且要考虑是不是要做一个子查询 */ /* 当正面来可能比较困难的时候就要反方面来进行 */ /* EXISTS版 */ SELECT t5.s# AS ST_CODE, t5.sname AS ST_NAME FROM student t5 WHERE NOT EXISTS ( SELECT 1 FROM ( SELECT DISTINCT t1.s# AS ST_CODE, t1.sname AS ST_NAME, t4.tname FROM student t1 LEFT JOIN sc t2 ON t1.s# = t1.s# LEFT JOIN course t3 ON t2.c# = t3.c# LEFT JOIN teacher t4 ON t3.t# = t4.t# WHERE t4.tname = '陈奕迅' ) WHERE s# = t5.s# ); /* 子查询版 */ SELECT t5.s# AS ST_CODE, t5.sname AS ST_NAME FROM student t5 WHERE t5. EXISTS ( SELECT 1 FROM ( SELECT DISTINCT t1.s# AS ST_CODE, t1.sname AS ST_NAME, t4.tname FROM student t1 LEFT JOIN sc t2 ON t1.s# = t1.s# LEFT JOIN course t3 ON t2.c# = t3.c# LEFT JOIN teacher t4 ON t3.t# = t4.t# WHERE t4.tname = '陈奕迅' ) WHERE s# = t5.s# ); --6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; --不能在where条件下面写WHERE t2.c# = 111 and t2.c# = 112 因为是对应同一条记录的 SELECT DISTINCT t1.s#,t1.sname FROM student t1 LEFT JOIN sc t2 ON t1.s# = t2.s# WHERE t2.c# = 111 INTERSECT --集合的交集 SELECT DISTINCT t1.s#,t1.sname FROM student t1 LEFT JOIN sc t2 ON t1.s# = t2.s# WHERE t2.c# = 112 --7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; SELECT DISTINCT t1.s# AS ST_CODE, t1.sname AS ST_NAME, FROM student t1 LEFT JOIN sc t2 ON t1.s# = t1.s# LEFT JOIN course t3 ON t2.c# = t3.c# LEFT JOIN teacher t4 ON t3.t# = t4.t# WHERE t4.tname = '李冰冰' --8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; /* 用单纯的join版 */ /* 用了子查询 */ SELECT a.s# FROM (SELECT t1.s#,t1.score FROM sc t1 WHERE t1.c# = 111) a INNER JOIN (SELECT t2.s#,t2.score FROM sc t2 WHERE t2.c# = 112) b ON a.s# = b.s# WHERE a.score < b.score --9、查询所有课程成绩小于60分的同学的学号、姓名; SELECT T1.S#, T1.SNAME FROM STUDENT T1 INNER JOIN SC T2 ON T1.S# = T2.S# WHERE T2.C# < 60 --10、查询没有学全所有课的同学的学号、姓名; /*子查询版*/ SELECT DISTINCT t1.s#,t1.sname FROM student t1 INNER JOIN SC t2 ON t1.s# = t2.s# WHERE t2.c# IN ( SELECT t3.c# FROM Course t3 ) /*exists版*/ SELECT DISTINCT t1.s#,t1.sname FROM student t1 INNER JOIN SC t2 ON t1.s# = t2.s# WHERE EXISTS ( SELECT 1 FROM Course t3 WHERE t3.c# = t2.c# ) --11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名; /* 子查询 */ SELECT DISTINCT t1.s#,t1.sname FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# WHERE t2.c# IN ( SELECT t4.c# FROM student t3 INNER JOIN sc t4 ON t3.s# = t4.s# WHERE t3.s# = 1 ) /* exists版 */ SELECT DISTINCT t1.s#,t1.sname FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# WHERE EXISTS ( SELECT 1 FROM student t3 INNER JOIN sc t4 ON t3.s# = t4.s# WHERE t4.c# = t2.c# AND t3.s# = 1 ) --12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名; /* 子查询 */ SELECT DISTINCT t1.s#,t1.sname FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# AND t1.s# <> 1 WHERE t2.c# IN ( SELECT t4.c# FROM student t3 INNER JOIN sc t4 ON t3.s# = t4.s# WHERE t3.s# = 1 ) /* exists版 */ SELECT DISTINCT t1.s#,t1.sname FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# AND t1.s# <> 1 WHERE EXISTS ( SELECT 1 FROM student t3 INNER JOIN sc t4 ON t3.s# = t4.s# WHERE t4.c# = t2.c# AND t3.s# = 1 ) --13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩; UPDATE sc t4 SET t4.score = ( WITH tmp AS ( SELECT t1.c#, AVG(t1.score) AS avg_score FROM sc t1 INNER JOIN Course t2 ON t1.c# = t2.c# INNER JOIN teacher t3 ON t2.t# = t3.t# GROUP BY t1.c# ) SELECT t5.avg_score FROM tmp t5 WHERE t4.c# = t5.c# ) WHERE EXISTS ( SELECT 1 FROM sc t6 INNER JOIN Course t7 ON t6.c# = t7.c# INNER JOIN teacher t8 ON t7.t# = t8.t# AND t8.tname = '陈奕迅' ) 14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名; SELECT DISTINCT t1.s#, t1.sname FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# AND t2.c# = ALL ( SELECT t4.c# FROM student t3 INNER JOIN sc t4 ON t3.s# = t4.s# AND t4.s# = 7 ) AND t1.s# <> 7 --15、删除学习'陈奕迅'老师课的SC表记录; DELETE FROM sc t1 WHERE EXISTS ( SELECT 1 FROM teacher t2 INNER JOIN Course t3 ON t2.t# = t3.t# WHERE t3.c# = t1.c# AND t2.tname = '陈奕迅' ) 16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩; INSERT INTO sc(s#,c#,score) SELECT min(t5.s#),112,AVG(t4.score) FROM sc t4 INNER JOIN student t5 ON t4.s# = t5.s# WHERE t4.s# NOT IN ( SELECT t2.s# FROM student t2 INNER JOIN sc t3 ON t2.s# = t3.s# AND t3.c# = 113 ) AND t4.c# = 112 GROUP BY t4.c# --17、按平均成绩从高到低显示所有学生的“语文1”、“生物2”、“化学1”三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分 /* 行转列除了可以用poivt外还可以分别select出来,再和主表的字段关联 */ SELECT t1.S# as 学生ID, (SELECT score FROM SC t2 WHERE t2.S#=t1.S# AND t2.C#=111) AS 语文1, (SELECT score FROM SC t3 WHERE t3.S#=t1.S# AND t3.C#=118) AS 生物2, (SELECT score FROM SC t4 WHERE t4.S#=t1.S# AND t4.C#=121) AS 化学1, COUNT(*) AS 有效课程数, AVG(t1.score) AS 平均成绩 FROM SC t1 GROUP BY t1.S# ORDER BY avg(t1.score) --18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 SELECT t1.c#, MIN(t1.score) AS min_score, MAX(t1.score) AS max_score FROM sc t1 GROUP BY t1.c# --19、按各科平均成绩从低到高和及格率的百分数从高到低顺序 SELECT t1.c#, AVG(t1.score) AS avg_score, 100 * SUM( CASE WHEN nvl(t1.score,0)>=60 THEN 1 ELSE 0 END )/COUNT(*) AS pass_pct FROM sc t1 GROUP BY t1.c# ORDER BY avg_score ASC,pass_pct DESC --20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 语文1(111),语文2(112),数学1(113),数学2(114) SELECT SUM(CASE WHEN t1.c# =111 THEN score ELSE 0 END)/SUM(CASE t1.c# WHEN 111 THEN 1 ELSE 0 END) AS 语文1 , 100 * SUM(CASE WHEN t1.c# = 111 AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN t1.c# = 111 THEN 1 ELSE 0 END) AS 语文1及格百分比 , SUM(CASE WHEN t1.c# = 112 THEN score ELSE 0 END)/SUM(CASE t1.c# WHEN 112 THEN 1 ELSE 0 END) AS 语文2 , 100 * SUM(CASE WHEN t1.c# = 112 AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN t1.c# = 112 THEN 1 ELSE 0 END) AS 语文2及格百分比 , SUM(CASE WHEN t1.c# = 113 THEN score ELSE 0 END)/SUM(CASE t1.c# WHEN 113 THEN 1 ELSE 0 END) AS 数学1 , 100 * SUM(CASE WHEN t1.c# = 113 AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN t1.c# = 113 THEN 1 ELSE 0 END) AS 数学1及格百分比 , SUM(CASE WHEN t1.c# = 114 THEN score ELSE 0 END)/SUM(CASE t1.c# WHEN 114 THEN 1 ELSE 0 END) AS 数学2 , 100 * SUM(CASE WHEN t1.c# = 114 AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN t1.c# = 114 THEN 1 ELSE 0 END) AS 数学2及格百分比 FROM SC t1 --21、查询不同老师所教不同课程平均分从高到低显示 SELECT t3.tname, t2.cname, AVG(t1.score) AS avg_score FROM sc t1 INNER JOIN course t2 ON t1.c# = t2.c# INNER JOIN teacher t3 ON t2.t# = t3.t# GROUP BY t3.tname,t2.cname ORDER BY avg_score DESC --22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(111),马克思(112),UML (113),数据库(114) SELECT sub_query.cname, sub_query.rank FROM( SELECT t2.cname, t1.score, rank() over(PARTITION BY t1.c# ORDER BY t1.score DESC) AS rank FROM sc t1 INNER JOIN course t2 ON t1.c# = t2.c# WHERE t1.c# IN (111,112,113,114) ) sub_query WHERE sub_query.rank BETWEEN 3 AND 6; --23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85] 优秀人数,[85-70] 良好人数,[70-60] 一般人数,[ <60] 刚及格人数 SELECT t1.c#, t2.cname, SUM(CASE WHEN t1.score > 85 AND t1.score <= 100 THEN 1 ELSE 0 END) AS 优秀人数, SUM(CASE WHEN t1.score > 70 AND t1.score <= 85 THEN 1 ELSE 0 END) AS 良好人数, SUM(CASE WHEN t1.score > 60 AND t1.score <= 70 THEN 1 ELSE 0 END) AS 一般人数, SUM(CASE WHEN t1.score <= 60 THEN 1 ELSE 0 END) AS 刚及格人数 FROM sc t1 INNER JOIN course t2 ON t1.c# = t2.c# GROUP BY t1.c#,t2.cname --24、查询学生平均成绩及其名次 SELECT t1.sname, t1.s#, AVG(t2.score) AS avg_score, dense_rank() over(ORDER BY AVG(t2.score) DESC) AS dense_rank FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# GROUP BY t1.sname,t1.s# --25、查询各科成绩前三名的记录:(不考虑成绩并列情况) SELECT sub_query.cname, sub_query.s#, sub_query.sname, sub_query.score, sub_query.rank FROM ( SELECT t3.cname, t1.s#, t1.sname, t2.score, rank() over(PARTITION BY t2.c# ORDER BY t2.score DESC) AS rank FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# INNER JOIN course t3 ON t2.c# = t3.c# ) sub_query WHERE sub_query.rank < 4 --26、查询每门课程被选修的学生数 SELECT t2.cname, COUNT(t1.s#) AS sum_stu FROM sc t1 INNER JOIN course t2 ON t1.c# = t2.c# GROUP BY t2.cname --27、查询出只选修了一门课程的全部学生的学号和姓名 /* GROUP BY 语句中选择出来的只有备份组的字段和聚合函数 */ SELECT t1.s#, t1.sname FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# GROUP BY t1.s#,t1.sname HAVING COUNT(t2.c#) = 1 --28、查询男生、女生人数 SELECT t1.ssex, COUNT(t1.ssex) AS sex_num FROM student t1 GROUP BY t1.ssex --29、查询名字中有'黑'的学生名单 SELECT t1.s#, t1.sname FROM student t1 WHERE t1.sname LIKE '%黑%' --30、查询同名同性学生名单,并统计同名人数 /* 写了个错的 原因:student表中有两个小粉,on条件只是县限定了sname相等,所以导致了2X2的笛卡尔乘积*/ SELECT t1.sname, COUNT(t1.s#) AS same_name_count FROM student t1 INNER JOIN student t2 ON t1.sname = t2.sname GROUP BY t1.sname,t2.sname HAVING COUNT(t1.sname) > 1 /* 正确的写法 */ SELECT t1.sname, COUNT(t1.sname) AS same_name_count FROM student t1 GROUP BY t1.sname HAVING COUNT(t1.sname) > 1 --32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 SELECT t2.cname, t2.c#, AVG(t1.score) AS avg_score FROM sc t1 INNER JOIN course t2 ON t1.c# = t2.c# GROUP BY t2.cname,t2.c# ORDER BY avg_score ASC,t2.c# DESC --33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩 SELECT t1.s#, t1.sname, AVG(t2.score) AS avg_score FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# GROUP BY t1.s#,t1.sname HAVING AVG(t2.score) > 85 --34、查询课程名称为'语文1',且分数低于60的学生姓名和分数 SELECT t1.sname, t2.score FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# INNER JOIN course t3 ON t2.c# = t3.c# WHERE t3.cname = '语文1' AND t2.score < 60 --35、查询所有学生的选课情况; /* 可能不是这样写,有更好的写法 */ SELECT t1.s#, t1.sname, t3.cname FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# INNER JOIN course t3 ON t2.c# = t3.c# --36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; SELECT t1.s#, t1.sname, t3.cname, t2.score FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# INNER JOIN course t3 ON t2.c# = t3.c# WHERE t2.score > 70 --37、查询不及格的课程,并按课程号从大到小排列 SELECT t1.c#, t2.cname, t1.score FROM sc t1 INNER JOIN course t2 ON t1.c# = t2.c# WHERE t1.score < 60 ORDER BY t1.c# DESC --38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名; SELECT t1.s#, t1.sname, t2.score FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# INNER JOIN course t3 ON t2.c# = t3.c# WHERE t2.c# = 113 AND t2.score > 80 --39、求选了课程的学生人数 SELECT SUM(1) FROM ( SELECT 1 FROM sc t1 GROUP BY t1.s# ) --40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩 SELECT t4.s#, t4.sname, t5.score, t6.cname FROM student t4 INNER JOIN sc t5 ON t4.s# = t5.s# INNER JOIN course t6 ON t5.c# = t6.c# WHERE (t5.score,t6.cname) IN ( SELECT MAX(t1.score), t2.cname FROM sc t1 INNER JOIN course t2 ON t1.c# = t2.c# INNER JOIN teacher t3 ON t2.t# = t3.t# WHERE t3.tname = '陈奕迅' GROUP BY t2.cname ) --41、查询各个课程及相应的选修人数 SELECT t1.c#, t2.cname, COUNT(t1.s#) FROM sc t1 INNER JOIN course t2 ON t1.c# = t2.c# GROUP BY t1.c#,t2.cname --42、查询不同课程成绩相同的学生的学号、课程号、学生成绩 SELECT t1.s#, t1.c#, t1.score FROM sc t1 INNER JOIN sc t2 ON t1.score = t2.score AND t1.c# <> t2.c# AND t1.s# = t2.s# --43、查询每门功成绩最好的前两名 SELECT sub_query.s#, sub_query.sname, sub_query.cname, sub_query.score, sub_query.rank FROM ( SELECT t1.s#, t3.sname, t2.cname, t1.score, dense_rank() over(PARTITION BY t1.c# ORDER BY t1.score DESC) AS rank FROM sc t1 INNER JOIN course t2 ON t1.c# = t2.c# INNER JOIN student t3 ON t1.s# = t3.s# ) sub_query WHERE sub_query.rank < 3 --44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列 SELECT t1.c#, COUNT(t1.s#) AS stu_num FROM sc t1 GROUP BY t1.c# HAVING COUNT(t1.s#) > 10 ORDER BY stu_num DESC,t1.c# ASC --45、检索至少选修两门课程的学生学号 SELECT t1.s# FROM sc t1 GROUP BY t1.s# HAVING COUNT(t1.c#) > 1 --46、查询全部学生都选修的课程的课程号和课程名 SELECT t1.c#, t2.cname, COUNT(t1.s#) FROM sc t1 INNER JOIN course t2 ON t1.c# = t2.c# GROUP BY t1.c#,t2.cname HAVING COUNT(t1.s#) = ( SELECT COUNT(t3.s#) FROM student t3 ) --47、查询没学过'陈奕迅'老师讲授的任一门课程的学生姓名 SELECT t5.s#, t5.sname FROM student t5 WHERE t5.s# NOT IN ( SELECT DISTINCT t1.s# FROM student t1 INNER JOIN sc t2 ON t1.s# = t2.s# INNER JOIN course t3 ON t2.c# = t3.c# INNER JOIN teacher t4 ON t3.t# = t4.t# WHERE t4.tname = '陈奕迅' ) --48、查询两门以上不及格课程的同学的学号及其平均成绩 /* 不能直接用下面的方式,因为有where条件过滤了一部分的成绩 SELECT t1.s#, AVG(t1.score) FROM sc t1 WHERE t1.score < 60 GROUP BY t1.s# HAVING COUNT(t1.c#) > 2 */ SELECT t2.s#, AVG(t2.score) FROM sc t2 WHERE t2.s# IN ( SELECT t1.s# FROM sc t1 WHERE t1.score < 60 GROUP BY t1.s# HAVING COUNT(t1.c#) > 2 ) GROUP BY t2.s# --49、检索'114'课程分数小于60,按分数降序排列的同学学号 SELECT t1.s# FROM sc t1 WHERE t1.c# = 114 AND t1.score < 60 ORDER BY t1.s# DESC --50、删除'2'同学的'111'课程的成绩 DELETE FROM sc t1 WHERE t1.s# = 2 AND t1.c# = 111
6、删除表格
/*====================================*/ /*=========== DROP TABLE =============*/ /*====================================*/ DROP TABLE STUDENT; DROP TABLE COURSE; DROP TABLE SC; DROP TABLE TEACHER;