数据库SQL面试题2(关于学生、课程、成绩、教师四张表)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

数据库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.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 AME 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
'教师名字';
View Code
*注:别的数据库将备注修改成相应的格式
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,'男'); View Code
课程表
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(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); View Code
成绩表
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,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(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);
View Code
教师表
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,'刘备');
View Code
*插⼊后记得提交数据
5、开始题⽬
/*====================================*/
/*=========== 习题开始 =============*/
/*====================================*/
--1、查询“111”课程⽐“112”课程成绩⾼的所有学⽣的学号;
/* EXISTS 版本 */
SELECT t1.s#
FROM sc t1
WHERE t1.c# =111
AND EXISTS (
SELECT1
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,
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 (
SELECT1
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 Course t3
)
/*exists版*/
SELECT DISTINCT t1.s#,t1.sname
FROM student t1
INNER JOIN SC t2
ON t1.s# = t2.s#
WHERE EXISTS (
SELECT1
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 (
SELECT1
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 (
SELECT1
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 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 (
SELECT1
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)>=60THEN
1
ELSE
END
)/COUNT(*) AS pass_pct
FROM sc t1
GROUP BY t1.c#
ORDER BY avg_score ASC,pass_pct DESC
SUM(CASE WHEN t1.c# =112THEN score ELSE0END)/SUM(CASE t1.c# WHEN112THEN1ELSE0END) AS语⽂2 ,
100*SUM(CASE WHEN t1.c# =112AND score >=60THEN1ELSE0END)/SUM(CASE WHEN t1.c# =112THEN1ELSE0END) AS语⽂2及格百分⽐ , SUM(CASE WHEN t1.c# =113THEN score ELSE0END)/SUM(CASE t1.c# WHEN113THEN1ELSE0END) AS数学1 ,
100*SUM(CASE WHEN t1.c# =113AND score >=60THEN1ELSE0END)/SUM(CASE WHEN t1.c# =113THEN1ELSE0END) AS数学1及格百分⽐ , SUM(CASE WHEN t1.c# =114THEN score ELSE0END)/SUM(CASE t1.c# WHEN114THEN1ELSE0END) AS数学2 ,
100*SUM(CASE WHEN t1.c# =114AND score >=60THEN1ELSE0END)/SUM(CASE WHEN t1.c# =114THEN1ELSE0END) AS数学2及格百分⽐FROM SC t1
--21、查询不同⽼师所教不同课程平均分从⾼到低显⽰
SELECT t3.tname,
ame,
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,ame
ORDER BY avg_score DESC
--22、查询如下课程成绩第 3 名到第 6 名的学⽣成绩单:企业管理(111),马克思(112),UML (113),数据库(114)
SELECT sub_ame,
sub_query.rank
FROM(
SELECT ame,
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 BETWEEN3AND6;
--23、统计列印各科成绩,各分数段⼈数:课程ID,课程名称,[100-85] 优秀⼈数,[85-70] 良好⼈数,[70-60] ⼀般⼈数,[ <60] 刚及格⼈数
SELECT t1.c#,
ame,
SUM(CASE WHEN t1.score >85AND t1.score <=100THEN1ELSE0END) AS优秀⼈数,
SUM(CASE WHEN t1.score >70AND t1.score <=85THEN1ELSE0END) AS良好⼈数,
SUM(CASE WHEN t1.score >60AND t1.score <=70THEN1ELSE0END) AS⼀般⼈数,
SUM(CASE WHEN t1.score <=60THEN1ELSE0END) AS刚及格⼈数
FROM sc t1
INNER JOIN course t2
ON t1.c# = t2.c#
GROUP BY t1.c#,ame
--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_ame,
sub_query.s#,
sub_query.sname,
sub_query.score,
sub_query.rank
FROM (
SELECT ame,
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 ame,
COUNT(t1.s#) AS sum_stu
FROM sc t1
INNER JOIN course t2
ON t1.c# = t2.c#
GROUP BY ame
--27、查询出只选修了⼀门课程的全部学⽣的学号和姓名
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 ame,
t2.c#,
AVG(t1.score) AS avg_score
FROM sc t1
INNER JOIN course t2
ON t1.c# = t2.c#
GROUP BY ame,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 ame ='语⽂1'
AND t2.score <60
--35、查询所有学⽣的选课情况;
/* 可能不是这样写,有更好的写法 */
SELECT t1.s#,
t1.sname,
ame
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,
ame,
t2.score
FROM student t1
INNER JOIN sc t2
ON t1.s# = t2.s#
INNER JOIN course t3
SELECT t1.c#,
ame,
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 (
SELECT1
FROM sc t1
GROUP BY t1.s#
)
--40、查询选修“叶平”⽼师所授课程的学⽣中,成绩最⾼的学⽣姓名及其成绩SELECT t4.s#,
t4.sname,
t5.score,
ame
FROM student t4
INNER JOIN sc t5
ON t4.s# = t5.s#
INNER JOIN course t6
ON t5.c# = t6.c#
WHERE (t5.score,ame) IN (
SELECT MAX(t1.score),
ame
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 ame
)
--41、查询各个课程及相应的选修⼈数
SELECT t1.c#,
ame,
COUNT(t1.s#)
FROM sc t1
INNER JOIN course t2
ON t1.c# = t2.c#
GROUP BY t1.c#,ame
--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_ame,
sub_query.score,
sub_query.rank
FROM (
SELECT t1.s#,
t3.sname,
ame,
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#,
ame,
COUNT(t1.s#)
FROM sc t1
INNER JOIN course t2
ON t1.c# = t2.c#
GROUP BY t1.c#,ame
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;。

相关文档
最新文档