create table student(
sno varchar2(10) primary key,
sname varchar2(20),
sage number(2),
ssex varchar2(5)
);
create table teacher(
tno varchar2(10) primary key,
tname varchar2(20)
);
create table course(
cno varchar2(10),
cname varchar2(20),
tno varchar2(20),
constraint pk_course primary key (cno,tno)
);
create table sc(
sno varchar2(10),
cno varchar2(10),
score number(4,2),
constraint pk_sc primary key (sno,cno)
);
/*******初始化学生表的数据******/
insert into student values ('s001','张三',23,'男');
insert into student values ('s002','李四',23,'男');
insert into student values ('s003','吴鹏',25,'男');
insert into student values ('s004','琴沁',20,'女');
insert into student values ('s005','王丽',20,'女');
insert into student values ('s006','李波',21,'男');
insert into student values ('s007','刘玉',21,'男');
insert into student values ('s008','萧蓉',21,'女');
insert into student values ('s009','陈萧晓',23,'女');
insert into student values ('s010','陈美',22,'女');
commit;
/******************初始化教师表***********************/
insert into teacher values ('t001', '刘阳');
insert into teacher values ('t002', '谌燕');
insert into teacher values ('t003', '胡明星');
commit;
/***************初始化课程表****************************/
insert into course values ('c001','J2SE','t002');
insert into course values ('c002','Java Web','t002');
insert into course values ('c003','SSH','t001');
insert into course values ('c004','Oracle','t001');
insert into course values ('c005','SQL SERVER 2005','t003');
insert into course values ('c006','C#','t003');
insert into course values ('c007','JavaScript','t002');
insert into course values ('c008','DIV+CSS','t001');
insert into course values ('c009','PHP','t003');
insert into course values ('c010','EJB3.0','t002');
commit;
/***************初始化成绩表***********************/
insert into sc values ('s001','c001',78.9);
insert into sc values ('s002','c001',80.9);
insert into sc values ('s003','c001',81.9);
insert into sc values ('s004','c001',60.9);
insert into sc values ('s001','c002',82.9);
insert into sc values ('s002','c002',72.9);
insert into sc values ('s003','c002',81.9);
insert into sc values ('s001','c003','59');
commit;
练习:
注意:以下练习中的数据是根据初始化到数据库中的数据来写的SQL 语句,请大家务必注意。
1、查询“c001”课程比“c002”课程成绩高的所有学生的学号; 关联,子查询,LAG/LEAD,行列转换,排序(表名不区分大小写,但是表数据里面的字段是区分大小写的),
两张表共同的条件尽量写在前面,内关联的从表不用+号
SELECT A.SNO FROM SC A JOIN SC B ON A.SNO=B.SNO AND A.SCORE>B.SCORE AND A.CNO='c001' AND B.CNO='c002';
SELECT A.SNO FROM SC A,SC B WHERE A.SNO=B.SNO AND A.SCORE>B.SCORE AND A.CNO='c001' AND B.CNO='c002';
SELECT A.SNO FROM SC A WHERE A.CNO='c001' AND A.SCORE>(SELECT B.SCORE FROM SC B WHERE A.SNO=B.SNO AND B.CNO='c002');
SELECT B.SNO FROM (SELECT A.*,LEAD(A.SCORE,1) OVER (PARTITION BY A.SNO ORDER BY A.CNO) LEAD FROM SC A WHERE A.CNO IN ('c001','c002')) B WHERE B.SCORE>B.LEAD;
SELECT B.SNO FROM (SELECT A.*,LAG(A.SCORE,1) OVER (PARTITION BY A.SNO ORDER BY A.CNO) LAG FROM SC A WHERE A.CNO IN ('c001','c002')) B WHERE B.SCORE
SUM(DECODE(A.CNO,'c001',A.SCORE)) C1,
SUM(DECODE(A.CNO,'c002',A.SCORE)) C2
FROM SC A GROUP BY A.SNO) B WHERE B.C1>B.C2;
--排序
SELECT A.SNO
FROM (SELECT SC.*,
RANK() OVER(PARTITION BY SC.SNO ORDER BY SC.SCORE DESC) RN
FROM SC
WHERE SC.CNO IN ('c001', 'c002')) A
WHERE A.RN = 2
AND A.CNO = 'c002';
2、查询平均成绩大于60分的同学的学号和平均成绩;
用having简单
SELECT A.SNO,AVG(A.SCORE) AS AVG FROM SC A GROUP BY A.SNO HAVING AVG(A.SCORE)>60;
SELECT B.SNO,B.AVG FROM (SELECT A.*,AVG(A.SCORE) OVER (PARTITION BY A.SNO) AS AVG FROM SC A) B WHERE B.AVG>60;
SELECT B.SNO,B.AVG FROM (SELECT A.SNO,AVG(A.SCORE) AS AVG FROM SC A GROUP BY A.SNO) B WHERE B.AVG>60;
3、查询所有同学的学号、姓名、选课数、总成绩;
SELECT S.SNO,S.SNAME,NVL(B.CT,0),NVL(B.SFEN,0) FROM STUDENT S LEFT JOIN (SELECT A.SNO,COUNT(A.CNO) AS CT,SUM(A.SCORE) AS SFEN FROM SC A GROUP BY A.SNO) B ON S.SNO=B.SNO;
4、查询姓“刘”的老师的个数; 汉字的字符长度为1,聚合函数的参数最好用主键或者常量,不能先GROUP BY T.TNO因为会将数据按照TNO的分组分别求出计数;
SELECT COUNT(1) FROM TEACHER T WHERE T.TNAME LIKE'刘%';
SELECT COUNT(T.TNO) FROM TEACHER T WHERE T.TNAME LIKE'刘%' GROUP BY T.TNO;
SELECT COUNT(T.TNO) FROM TEACHER T WHERE LTRIM(T.TNAME,'刘')<>T.TNAME GROUP BY T.TNO;
SELECT COUNT(T.TNO) FROM TEACHER T WHERE INSTR(T.TNAME,'刘',1,1)=1 GROUP BY T.TNO;
SELECT COUNT(T.TNO) FROM TEACHER T WHERE SUBSTR(T.TNAME,1,1)='刘' GROUP BY T.TNO;
5、查询没学过“谌燕”老师课的同学的学号、姓名; 修改:要用内关联,不用MINUS,MINUS性能差,除非需要对很多个字段进行操作
SELECT S.SNO,S.SNAME FROM STUDENT S
MINUS
SELECT S.SNO,S.SNAME FROM STUDENT S LEFT JOIN SC ON S.SNO=SC.SNO LEFT JOIN COURSE C ON SC.CNO=C.CNO LEFT JOIN TEACHER T ON T.TNO=C.TNO WHERE TNAME='谌燕';
--外关联,统计 学的科数 为0的
SELECT E.SNO, E.SNAME
FROM (SELECT A.SNO, A.SNAME, COUNT(B.CNO) CT
FROM STUDENT A
LEFT JOIN SC B
ON A.SNO = B.SNO
LEFT JOIN COURSE C
ON B.CNO = C.CNO
LEFT JOIN TEACHER D
ON C.TNO = D.TNO
AND D.TNAME = '谌燕'
GROUP BY A.SNO, A.SNAME) E
WHERE E.CT = 0;--复杂
SELECT *
FROM STUDENT A
LEFT JOIN (SELECT B.SNO
FROM SC B
JOIN COURSE C
ON B.CNO = C.CNO
JOIN TEACHER D
ON C.TNO = D.TNO
AND D.TNAME = '谌燕') E
ON A.SNO = E.SNO
WHERE E.SNO IS NULL;
SELECT A.SNO,A.SNAME FROM STUDENT A WHERE A.SNO NOT IN (SELECT B.SNO FROM SC B JOIN COURSE C ON B.CNO=C.CNO JOIN TEACHER D
ON C.TNO=D.TNO AND D.TNAME='谌燕');
SELECT A.*
FROM STUDENT A
WHERE NOT EXISTS (SELECT 1
FROM SC B
JOIN COURSE C
ON B.CNO = C.CNO
JOIN TEACHER D
ON C.TNO = D.TNO
AND D.TNAME = '谌燕'
WHERE A.SNO = B.SNO)
6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
--成绩表限制C1,C2,统计科目数,等于2的两门都学过,关联
SELECT S.SNO, S.SNAME
FROM STUDENT S,
(SELECT SC.SNO, COUNT(SC.CNO) CT
FROM SC
WHERE SC.CNO IN ('c001', 'c002')
GROUP BY SC.SNO) A
WHERE S.SNO = A.SNO
AND A.CT = 2;
--成绩表限制C1,C2,统计科目数,等于2的两门都学过,子查询
SELECT S.SNO, S.SNAME
FROM STUDENT S
WHERE (SELECT COUNT(SC.CNO)
FROM SC
WHERE S.SNO = SC.SNO
AND SC.CNO IN ('c001', 'c002')
GROUP BY SC.SNO) = 2;
SELECT S.SNO,S.SNAME FROM STUDENT S WHERE S.SNO IN (SELECT A.SNO FROM SC A,SC B WHERE A.SNO=B.SNO AND A.CNO='c001' AND B.CNO='c002');
SELECT S.SNO,S.SNAME FROM STUDENT S JOIN (SELECT A.SNO FROM SC A,SC B WHERE A.SNO=B.SNO AND A.CNO='c001' AND B.CNO='c002') C ON S.SNO=C.SNO;
SELECT S.SNO,S.SNAME FROM STUDENT S WHERE EXISTS (SELECT 1 FROM SC A,SC B WHERE A.SNO=B.SNO AND A.SNO=S.SNO AND A.CNO='c001' AND B.CNO='c002');
--求既有ANALYST又有CLERK的部门
SELECT E.DEPTNO,COUNT(DISTINCT E.JOB) FROM EMP E WHERE E.JOB IN ('ANALYST','CLERK') GROUP BY E.DEPTNO;
SELECT S.SNO,S.SNAME FROM STUEDENT S WHERE S.SNO=SC.SNO AND (SELECT SC.CNO FROM SC) IN ('c001') AND (SELECT SC.CNO FROM SC) IN ('c002');--错误
SELECT S.SNO,S.SNAME FROM STUDENT S JOIN SC ON S.SNO=SC.SNO WHERE SC.CNO ='c001'
INTERSECT
SELECT S.SNO,S.SNAME FROM STUDENT S JOIN SC ON S.SNO=SC.SNO WHERE SC.CNO ='c002';--复杂
SELECT S.SNO,S.SNAME FROM STUDENT S WHERE S.SNO IN (SELECT A.SNO FROM SC A WHERE A.CNO='c001' INTERSECT SELECT B.SNO FROM SC B WHERE B.CNO='c002');--其他写法?
7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;
用count不用sum
SELECT *
FROM STUDENT ST,
(SELECT S.SNO, COUNT(1) CT1
FROM SC S, COURSE C, TEACHER T
WHERE S.CNO = C.CNO
AND C.TNO = T.TNO
AND T.TNAME = '谌燕'
GROUP BY S.SNO) A,
(SELECT COUNT(1) AS CT2
FROM COURSE C,TEACHER T
WHERE C.TNO = T.TNO
AND T.TNAME = '谌燕' GROUP BY C.TNO) B
WHERE ST.SNO = A.SNO
AND A.CT1 = B.CT2;
SELECT A.SNO, A.SNAME
FROM STUDENT A
WHERE (SELECT COUNT(1)
FROM SC B
JOIN COURSE C
ON B.CNO = C.CNO
JOIN TEACHER D
ON C.TNO = D.TNO
AND D.TNAME = '谌燕'
WHERE A.SNO = B.SNO
GROUP BY B.SNO) = (SELECT COUNT(1)
FROM COURSE C
JOIN TEACHER T
ON C.TNO = T.TNO
AND TNAME = '谌燕');
SELECT S.SNO,S.SNAME FROM STUDENT S JOIN SC ON S.SNO=SC.SNO JOIN COURSE C ON SC.CNO=C.CNO JOIN TEACHER T ON T.TNO=C.TNO WHERE TNAME ='谌燕' GROUP BY S.SNO,S.SNAME;
8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名; 与第一题重复?
9、查询所有课程成绩小于60 分的同学的学号、姓名; 该名学生每一门课程的成绩都小于60分
SELECT A.SNO,A.SNAME FROM STUDENT A WHERE (SELECT MAX(SC.SCORE) FROM SC WHERE A.SNO=SC.SNO GROUP BY SC.SNO)<60;
SELECT A.SNO,A.SNAME FROM STUDENT A JOIN (SELECT SC.SNO,MAX(SC.SCORE) MAX FROM SC GROUP BY SC.SNO) B ON A.SNO=B.SNO AND B.MAX<60;
SELECT S.SNO,S.SNAME FROM STUDENT S JOIN SC ON S.SNO=SC.SNO WHERE SC.SCORE<60;
10、查询没有学全所有课的同学的学号、姓名;
SELECT *
FROM STUDENT ST,
(SELECT S.SNO, COUNT(1) CT1 FROM SC S GROUP BY S.SNO) A,
(SELECT COUNT(1) AS CT2 FROM COURSE C) B
WHERE ST.SNO = A.SNO
AND NVL(CT1, 0) < CT2
SELECT A.SNO,A.SNAME FROM (SELECT S.SNO,S.SNAME,NVL(COUNT(SC.CNO),0) CT1 FROM STUDENT S LEFT JOIN SC ON S.SNO=SC.SNO GROUP BY S.SNO,S.SNAME) A WHERE CT1<(SELECT COUNT(C.CNO) FROM COURSE C);
11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名;
SELECT S.SNO,S.SNAME FROM STUDENT S JOIN SC ON S.SNO=SC.SNO WHERE SC.CNO IN (SELECT SC.CNO FROM SC WHERE SC.SNO='s001');
12、查询学过学号为“s002”同学所有门课的其他同学学号和姓名;
--用关联
SELECT * FROM STUDENT ;
SELECT * FROM SC ;
/* SELECT A.SNO
,A.SNAME
,B.CNO
FROM STUDENT A
LEFT JOIN SC B
ON B.SNO = A.SNO
WHERE A.SNO = 's001';
*/
SELECT
A1.SNO
,A1.SNAME
,COUNT(A1.SNAME)
FROM STUDENT A1
LEFT JOIN SC C
ON C.SNO = A1.SNO
WHERE C.CNO IN ( SELECT B.CNO
FROM SC B
WHERE B.SNO = 's002')
AND A1.SNO !='s002'
GROUP BY A1.SNO
,A1.SNAME
HAVING COUNT(A1.SNAME) =(SELECT COUNT(1) FROM SC A WHERE A.SNO = 's002')
13、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;
UPDATE SC S
SET S.SCORE =
(SELECT AVG(A.SCORE) FROM SC A WHERE A.CNO = S.CNO GROUP BY A.CNO)
WHERE S.CNO IN (SELECT C.CNO
FROM COURSE C, TEACHER T
WHERE C.TNO = T.TNO
AND T.TNAME = '谌燕')
MERGE INTO SC A
USING(
WITH B AS (SELECT S.CNO, AVG(S.SCORE) AS A_SCORE
FROM SC S
JOIN COURSE C
ON C.CNO = S.CNO
JOIN TEACHER T
ON T.TNO = C.TNO
WHERE T.TNAME = '谌燕'
GROUP BY S.CNO)
SELECT B.CNO, b.A_SCORE FROM B
) B ON (A.CNO = B.CNO)
WHEN MATCHED THEN
UPDATE SET
A.SCORE=B.A_SCORE;
--在WHERE 条件中已经限定了“谌燕”老师教的课,前半段语句条件重复;
UPDATE SC SET SCORE=
(SELECT AVG(B.SCORE) FROM SC B JOIN COURSE C ON B.CNO=C.CNO JOIN TEACHER D ON C.TNO=D.TNO AND D.TNAME='谌燕' WHERE
SC.CNO=B.CNO GROUP BY B.CNO)
WHERE SC.CNO IN (SELECT E.CNO FROM COURSE E JOIN TEACHER F ON E.TNO=F.TNO AND F.TNAME='谌燕');
14、查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名;
--与其他同学的交集科目数,与S001学的科目数一样,同时与该同学自己的学的科目数一样
SELECT S.SNO, S.SNAME
FROM STUDENT S,
(SELECT SC.SNO, COUNT(SC.CNO) CT1, COUNT(A.CNO) CT2
FROM SC
LEFT JOIN (SELECT SC.CNO FROM SC WHERE SC.SNO = 's001') A
ON SC.CNO = A.CNO
GROUP BY SC.SNO) B,
(SELECT SC.SNO, COUNT(SC.SNO) CT3
FROM SC
WHERE SC.SNO = 's001'
GROUP BY SC.SNO) C
WHERE S.SNO = B.SNO
AND B.CT1 = B.CT2
AND B.CT2 = C.CT3
AND S.SNO <> 's001';
--以下写法找出的为:学过学号为“s001”同学所有门课的其他同学
SELECT S.SNO, S.SNAME
FROM STUDENT S
WHERE (SELECT COUNT(A.CNO)
FROM SC
LEFT JOIN (SELECT SC.CNO FROM SC WHERE SC.SNO = 's001') A
ON SC.CNO = A.CNO
WHERE S.SNO = SC.SNO
GROUP BY SC.SNO) =
(SELECT COUNT(SC.SNO) FROM SC WHERE SC.SNO = 's001') AND S.SNO<>'s001';
15、删除学习“谌燕”老师课的SC 表记录;
DELETE FROM SC
WHERE SC.CNO IN (SELECT C.CNO
FROM COURSE C
JOIN TEACHER D
ON C.TNO = D.TNO
AND D.TNAME = '谌燕');
--子查询里面不需要SC表参与
DELETE FROM SC
WHERE SC.CNO IN (SELECT B.CNO
FROM SC B
JOIN COURSE C
ON B.CNO = C.CNO
JOIN TEACHER D
ON C.TNO = D.TNO
AND D.TNAME = '谌燕');
16、向SC 表中插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩;
INSERT INTO SC
SELECT S.SNO, 'c002', B.AVG
FROM STUDENT S
LEFT JOIN (SELECT SC.SNO FROM SC WHERE SC.CNO = 'c002') A
ON S.SNO = A.SNO,
(SELECT AVG(SC.SCORE) AVG FROM SC WHERE SC.CNO = 'c002') B
WHERE A.SNO IS NULL;
17、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT SC.CNO 课程ID,MAX(SCORE) 最高分,MIN(SCORE)最低分 FROM SC GROUP BY SC.CNO;
18、按各科平均成绩从低到高和及格率的百分数从高到低顺序
SELECT SC.CNO,
AVG(SC.SCORE) A,
SUM(CASE
WHEN SC.SCORE >= 60 THEN
1
ELSE
0
END) / COUNT(1) B
FROM SC
GROUP BY SC.CNO
ORDER BY A ,B DESC;
19、查询不同老师所教不同课程平均分从高到低显示
SELECT SC.CNO, D.TNAME, AVG(SC.SCORE) AVG
FROM SC
JOIN COURSE C
ON SC.CNO = C.CNO
JOIN TEACHER D
ON C.TNO = D.TNO
GROUP BY SC.CNO, D.TNAME
ORDER BY AVG DESC;
20、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] 怎么取别名? 特殊符号加双引号
SELECT C.CNO,
C.CNAME,
SUM(CASE
WHEN SC.SCORE <= 100 AND SC.SCORE >= 85 THEN
1
ELSE
0
END) /*[100,85]*/,
SUM(CASE
WHEN SC.SCORE < 85 AND SC.SCORE >= 70 THEN
1
ELSE
0
END) /*(85,70]*/,
SUM(CASE
WHEN SC.SCORE < 70 AND SC.SCORE >= 60 THEN
1
ELSE
0
END) /*(70,60]*/,
SUM(CASE
WHEN SC.SCORE < 60 THEN
1
ELSE
0
END) /*(60,0]*/
FROM SC
JOIN COURSE C
ON SC.CNO = C.CNO
GROUP BY C.CNO, C.CNAME;
21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT A.* FROM (SELECT SC.*,ROW_NUMBER() OVER(PARTITION BY SC.CNO ORDER BY SC.SCORE DESC) AS RN FROM SC) A WHERE A.RN<=3;
22、查询每门课程被选修的学生数
SELECT C.CNO,COUNT(SC.SNO) FROM COURSE C LEFT JOIN SC ON C.CNO=SC.CNO GROUP BY C.CNO;
23、查询出只选修了一门课程的全部学生的学号和姓名
SELECT S.SNO,S.SNAME FROM STUDENT S WHERE
(SELECT COUNT(SC.CNO) FROM SC WHERE S.SNO=SC.SNO GROUP BY SC.SNO)=1;
24、查询男生、女生人数
SELECT S.SSEX,COUNT(S.SNO) FROM STUDENT S GROUP BY S.SSEX;
25、查询姓“张”的学生名单
SELECT * FROM STUDENT S WHERE S.SNAME LIKE '张%';
SELECT * FROM STUDENT S WHERE SUBSTR(S.SNAME,1,1)='张';
SELECT * FROM STUDENT S WHERE LTRIM(S.SNAME,'张')<>S.SNAME;
SELECT * FROM STUDENT S WHERE INSTR(S.SNAME,'张',1,1)=1;
26、查询同名同性学生名单,并统计同名人数
SELECT A.SNAME,COUNT(A.SNAME) FROM STUDENT A JOIN STUDENT B ON A.SNAME=B.SNAME AND A.SSEX=B.SSEX AND A.SNO<>B.SNO GROUP BY A.SNAME;
27、1981 年出生的学生名单(注:Student 表中Sage 列的类型是number)
SELECT * FROM STUDENT S WHERE TO_NUMBER(TO_CHAR(SYSDATE,'YYYY'))-1981=S.SAGE;
28、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列.
SELECT SC.CNO,AVG(SC.SCORE) AVG FROM SC GROUP BY SC.CNO ORDER BY AVG,SC.CNO DESC;
29、查询平均成绩大于85 的所有学生的学号、姓名和平均成绩
SELECT S.SNO, S.SNAME, A.AVG
FROM STUDENT S
JOIN (SELECT SC.SNO, AVG(SC.SCORE) AVG FROM SC GROUP BY SC.SNO) A
ON S.SNO = A.SNO
AND A.AVG > 85;
30、查询课程名称为“数据库”,且分数低于60 的学生姓名和分数
SELECT S.SNAME,SC.SCORE FROM STUDENT S,COURSE C,SC WHERE S.SNO = SC.SNO AND SC.CNO = C.CNO
AND C.CNAME = '数据库' AND SC.SCORE < 60;
31、查询所有学生的选课情况;
SELECT S.SNO,S.SNAME,COUNT(SC.CNO) 选课数量 FROM STUDENT S LEFT JOIN SC ON S.SNO = SC.SNO
GROUP BY S.SNO,S.SNAME;
32、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;
SELECT A.SNAME, A.CNAME, A.SCORE
FROM (SELECT S.SNAME,
C.CNAME,
SC.SCORE,
MIN(SC.SCORE) OVER(PARTITION BY SC.SNO) MIN
FROM STUDENT S, SC, COURSE C
WHERE S.SNO = SC.SNO
AND SC.CNO = C.CNO) A
WHERE A.MIN > 70;
33、查询不及格的课程,并按课程号从大到小排列
SELECT SC.CNO FROM SC WHERE SC.SCORE < 60 GROUP BY SC.CNO ORDER BY SC.CNO DESC;
34、查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名;
SELECT S.SNO,S.SNAME FROM STUDENT S JOIN SC ON S.SNO = SC.SNO AND SC.CNO = 'c001'
AND SC.SCORE > 80;
35、求选了课程的学生人数
SELECT COUNT(A.SNO) FROM (SELECT SC.SNO FROM SC GROUP BY SC.SNO) A;
36、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩
SELECT S.SNAME,A.SCORE FROM STUDENT S JOIN (SELECT SC.SNO,SC.SCORE,RANK() OVER (ORDER BY SC.SCORE DESC) RK FROM SC
JOIN COURSE C ON SC.CNO = C.CNO JOIN TEACHER T ON C.TNO = T.TNO AND T.TNAME = '谌燕') A ON S.SNO
= A.SNO AND A.RK = 1;
37、查询各个课程及相应的选修人数
SELECT SC.CNO,COUNT(SC.SNO) FROM SC GROUP BY SC.CNO;
38、查询不同课程成绩相同的学生的学号、课程号、学生成绩
SELECT A.SNO,A.CNO,A.SCORE FROM SC A JOIN SC B ON A.SNO = B.SNO WHERE
A.CNO <> B.CNO AND A.SCORE =B.SCORE;
39、查询每门功课成绩最好的前两名
SELECT S.SNAME,A.SNO,A.CNO,A.SCORE FROM STUDENT S JOIN
(SELECT SC.SNO,SC.CNO,SC.SCORE,ROW_NUMBER() OVER (PARTITION BY SC.CNO ORDER BY SC.SCORE DESC) RN FROM SC) A
ON S.SNO = A.SNO AND A.RN <= 2 ORDER BY A.CNO,A.SCORE DESC;
40、统计每门课程的学生选修人数(超过10 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
41、检索至少选修两门课程的学生学号
SELECT SC.SNO ,COUNT(SC.CNO) FROM SC GROUP BY SC.SNO HAVING COUNT(SC.CNO) >= 2;
42、查询全部学生都选修的课程的课程号和课程名
SELECT A.CNO, A.CNAME
FROM (SELECT SC.CNO, C.CNAME, COUNT(SC.SNO) CT
FROM SC
JOIN COURSE C
ON SC.CNO = C.CNO
GROUP BY SC.CNO, C.CNAME) A
JOIN (SELECT COUNT(S.SNO) CT FROM STUDENT S) B
ON 1 = 1
WHERE A.CT = B.CT;
43、查询没学过“谌燕”老师讲授的任一门课程的学生姓名
SELECT S.SNAME FROM STUDENT S WHERE S.SNO NOT IN (SELECT SC.SNO FROM SC JOIN COURSE C
ON SC.CNO = C.CNO JOIN TEACHER T ON C.TNO = T.TNO AND T.TNAME = '谌燕');
44、查询两门以上不及格课程的同学的学号及其平均成绩
SELECT A.SNO, AVG(A.SCORE)
FROM (SELECT SC.SNO, SC.SCORE, COUNT(SC.CNO) OVER(PARTITION BY SC.SNO) CT
FROM SC
WHERE SC.SCORE < 90) A
WHERE A.CT > 2
GROUP BY A.SNO;
45、检索“c004”课程分数小于60,按分数降序排列的同学学号
SELECT SC.SNO
FROM SC
WHERE SC.CNO = 'c004'
AND SC.SCORE < 60
ORDER BY SC.SCORE DESC;
46、删除“s002”同学的“c001”课程的成绩
DELETE FROM SC
WHERE SC.SNO = 's002'
AND SC.CNO = 'c001';