-- 学生表
CREATE TABLE Students(
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL,
s_birth VARCHAR(20) NOT NULL ,
s_sex VARCHAR(10) NOT NULL ,
PRIMARY KEY(s_id)
);
select * from Students;
-- 课程表
CREATE TABLE Courses(
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL ,
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id)
);
-- 教师表
CREATE TABLE Teachers(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL,
PRIMARY KEY(t_id)
);
-- 成绩表
CREATE TABLE Scores(
s_id VARCHAR(20),
c_id VARCHAR(20),
s_score number(3),
PRIMARY KEY(s_id,c_id)
);
-- 插入学生表测试数据
INSERT INTO Students VALUES('01' , '赵雷' , '1990-01-01' , '男');
INSERT INTO Students VALUES('02' , '钱电' , '1990-12-21' , '男');
INSERT INTO Students VALUES('03' , '孙风' , '1990-05-20' , '男');
INSERT INTO Students VALUES('04' , '李云' , '1990-08-06' , '男');
INSERT INTO Students VALUES('05' , '周梅' , '1991-12-01' , '女');
INSERT INTO Students VALUES('06' , '吴兰' , '1992-03-01' , '女');
INSERT INTO Students VALUES('07' , '郑竹' , '1989-07-01' , '女');
INSERT INTO Students VALUES('08' , '王菊' , '1990-01-20' , '女');
-- 课程表测试数据
INSERT INTO Courses VALUES('01' , '语文' , '02');
INSERT INTO Courses VALUES('02' , '数学' , '01');
INSERT INTO Courses VALUES('03' , '英语' , '03');
COMMIT ;
-- 教师表测试数据
INSERT INTO Teachers VALUES('01' , '张三');
INSERT INTO Teachers VALUES('02' , '李四');
INSERT INTO Teachers VALUES('03' , '王五');
-- 成绩表测试数据
INSERT INTO Scores VALUES('01' , '01' , 80);
INSERT INTO Scores VALUES('01' , '02' , 90);
INSERT INTO Scores VALUES('01' , '03' , 99);
INSERT INTO Scores VALUES('02' , '01' , 70);
INSERT INTO Scores VALUES('02' , '02' , 60);
INSERT INTO Scores VALUES('02' , '03' , 80);
INSERT INTO Scores VALUES('03' , '01' , 80);
INSERT INTO Scores VALUES('03' , '02' , 80);
INSERT INTO Scores VALUES('03' , '03' , 80);
INSERT INTO Scores VALUES('04' , '01' , 50);
INSERT INTO Scores VALUES('04' , '02' , 30);
INSERT INTO Scores VALUES('04' , '03' , 20);
INSERT INTO Scores VALUES('05' , '01' , 76);
INSERT INTO Scores VALUES('05' , '02' , 87);
INSERT INTO Scores VALUES('06' , '01' , 31);
INSERT INTO Scores VALUES('06' , '03' , 34);
INSERT INTO Scores VALUES('07' , '02' , 89);
INSERT INTO Scores VALUES('07' , '03' , 98);
-- 1、查询"01"课程比"02 "课程成绩高的学生的信息及课程分数
SELECT ST.*,S.c_id,S.S_SCORE FROM Students ST JOIN SCORES S on ST.s_id = S.S_ID
WHERE ST.s_id IN (
SELECT S1.s_id FROM SCORES S1
WHERE S1.C_ID='01' AND S1.S_SCORE>
(SELECT S2.S_SCORE FROM Scores S2 WHERE S1.S_ID=S2.S_ID AND S2.C_ID='02')
);
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT S11.*,SC1.* FROM STUDENTS S11 JOIN
(SELECT S1.s_id,S1.S_SCORE C1_SCORE,S2.s_score C2_SCORE
FROM (SELECT * FROM Scores WHERE C_ID=01 ) S1
JOIN (SELECT * FROM Scores S1 WHERE S1.C_ID=02) S2 ON S1.s_id=S2.s_id
WHERE S1.s_score<S2.s_score) SC1
ON S11.S_ID=SC1.s_id;
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT SC.s_id,(SELECT S_NAME FROM STUDENTS S1 WHERE S1.s_id=SC.s_id) 名字,ROUND(AVG(s_score),2) 平均成绩
FROM SCORES SC GROUP BY s_id HAVING AVG(s_score) >60;
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
-- (包括有成绩的和无成绩的)
SELECT s_id,(SELECT S_NAME FROM STUDENTS S1 WHERE S1.s_id=SC.s_id) 名字,
ROUND(SUM(s_score)/3,2) 平均成绩
FROM SCORES sc GROUP BY s_id HAVING SUM(s_score)/3<60;
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT DISTINCT S1.s_id,(SELECT S_NAME FROM STUDENTS S2 WHERE S2.s_id=S1.s_id) 名字,
COUNT(s_id) over (PARTITION BY s_id) 选课数,
SUM(s_score) over (PARTITION BY s_id) 总成绩FROM
FROM SCORES S1;
-- 6、查询"李"姓老师的数量
SELECT COUNT(*) FROM TEACHERS WHERE t_name LIKE '李%';
-- 7、查询学过"张三"老师授课的同学的信息
SELECT * FROM Students WHERE s_id IN
(SELECT s_id FROM SCORES WHERE c_id=
(SELECT c_id FROM Courses WHERE T_id =
(SELECT t_id FROM Teachers WHERE t_name='张三')));
-- 8、查询没学过"张三"老师授课的同学的信息
SELECT * FROM Students WHERE s_id not IN
(SELECT s_id FROM SCORES WHERE c_id=
(SELECT c_id FROM Courses WHERE T_id =
(SELECT t_id FROM Teachers WHERE t_name='张三')));
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select * from Students where s_id in
(select s1.s_id from
(select * from SCORES where c_id=01) s1 join
(select * from SCORES where c_id=02) s2 on s1.s_id=s2.s_id);
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select * from Students where s_id in
(select s1.s_id from
(select * from SCORES where c_id=01) s1 left join
(select * from SCORES where c_id=02) s2 on s1.s_id=s2.s_id
where s2.s_id is null );
-- 11、查询没有学全所有课程的同学的信息
select * from Students where s_id not in
(select s1.s_id from
(select * from SCORES where c_id=01) s1 join
(select * from SCORES where c_id=02) s2 on s1.s_id=s2.s_id join
(select * from SCORES where c_id=03) s3 on s2.s_id=s3.s_id);
-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select * from Students where s_id not in
(select distinct s_id from SCORES where c_id in (select c_id from SCORES where s_id=01));
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
-- @ouyang_1993的写法
select c_id from SCORES where s_id!=01;
select s_id,c_id from SCORES group by s_id,c_id having c_id=(select c_id from SCORES where s_id=01);
-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT st.s_name
FROM STUDENTS st
WHERE st.s_id NOT IN (
SELECT sc.s_id
FROM SCORES sc
JOIN courseS c ON c.c_id=sc.c_id
JOIN TEACHERS t ON t.t_id=c.t_id AND t.t_name='张三'
);
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select s_id,(SELECT S_NAME FROM STUDENTS S2 WHERE S2.s_id=S1.s_id) 名字,avg(s_score)
from Scores s1 where s_score<60 group by s_id having count(*)>=2;
-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
select s.* from Scores join STUDENTS S on Scores.s_id = S.S_ID
where s_score<60 and c_id=01 order by s_score desc;
-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select s_id,c_id,s_score,avg(s_score) over (partition by s_id ) from Scores order by avg(s_score) over (partition by s_id ) desc;
-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select c_id,(select c_name from Courses c where c.c_id= s.c_id) 课程名称
,max(s_score) 最高分,min(s_score) 最低分,round(avg(s_score),2) 平均分,
(sum(case when s_score>=60 then 1 else 0 end )/count(*)*100)||'%' as 及格率,
(sum(case when s_score between 70 and 80 then 1 else 0 end )/count(*)*100)||'%' as 中等率,
(sum(case when s_score between 80 and 90 then 1 else 0 end )/count(*)*100)||'%' as 优良率,
(sum(case when s_score between 90 and 100 then 1 else 0 end )/count(*)*100)||'%' as 优秀率
from Scores s group by c_id;
-- 19、按各科成绩进行排序,并显示排名
select s_id,c_id,s_score,row_number() over (partition by c_id order by s_score desc) from Scores;
-- 20、查询学生的总成绩并进行排名
select s_id,sum(s_score),rank() over (order by sum(s_score) desc) from Scores group by s_id;
-- 21、查询不同老师所教不同课程平均分从高到低显示
select distinct c_id,(avg(s_score) over (partition by c_id)) a from Scores order by a desc ;
-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select s1.*, c_id, s_score
from (select s_id,c_id,s_score,(rank() over (partition by c_id order by s_score)) c from Scores) s2 join
Students s1 on s1.s_id=s2.s_id
where c between 2 and 3;
-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select c_id,(select c_name from Courses c where c.c_id= s.c_id) 课程名称,
sum(case when s_score between 85 and 100 then 1 else 0 end) "[100-85]",
sum(case when s_score < 85 and s_score>70 then 1 else 0 end) "[70-85]",
sum(case when s_score <= 70 and s_score>=60 then 1 else 0 end) "[70-60]",
sum(case when s_score < 60 then 1 else 0 end) "[0-60]"
from Scores s group by c_id;
-- 24、查询学生平均成绩及其名次
select s.*,row_number() over (order by a desc ) from (select s_id,avg(s_score) a from Scores group by s_id) s;
-- 25、查询各科成绩前三名的记录
select * from (select s_id,c_id,row_number() over (partition by c_id order by s_score desc ) a from Scores) where a<=3;
-- 26、查询每门课程被选修的学生数
select c_id,count(*) from Scores group by c_id;
-- 27、查询出只有两门课程的全部学生的学号和姓名
select s_id,(SELECT S_NAME FROM STUDENTS S2 WHERE S2.s_id=S1.s_id) 名字
from Scores s1 group by s_id having count(*)=2;
-- 28、查询男生、女生人数
select s_sex,count(1) from Students group by s_sex;
-- 29、查询名字中含有"风"字的学生信息
select * from Students where s_name like '%风%';
-- 30、查询同名同性学生名单,并统计同名人数
select s_name,count(*) from Students group by s_name having count(*)>1;
-- 31、查询1990年出生的学生名单
select * from Students where substr(s_birth,1,4)=1990;
-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select c_id,avg(s_score) avg_score from Scores group by c_id
order by avg_score desc ,c_id;
-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select s_id,(SELECT S_NAME FROM STUDENTS S2 WHERE S2.s_id=S1.s_id),
avg(s_score) avg_score from Scores s1 group by s_id having avg(s_score)>85;
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select (SELECT S_NAME FROM STUDENTS S2 WHERE S2.s_id=S1.s_id),s_score from Scores S1
where s_score<60 and c_id=(select c_id from Courses where c_name='数学');
-- 35、查询所有学生的课程及分数情况;
SELECT S1.*,C2.c_name FROM Scores S1
JOIN COURSES C2 on S1.c_id = C2.C_ID;
-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
SELECT s_id,c_id,s_score,
(SELECT S_NAME FROM STUDENTS S2 WHERE S2.s_id=S1.s_id) 名字,
(select c_name from Courses c where c.c_id= s1.c_id) 课程名称 FROM SCORES S1 WHERE s_score>70;
-- 37、查询不及格的课程
SELECT DISTINCT c_id FROM Scores WHERE s_score<60;
-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
SELECT s_id,(SELECT S_NAME FROM STUDENTS S2 WHERE S2.s_id=S1.s_id) 名字
FROM Scores S1 WHERE c_id=01 AND s_score>80;
-- 39、求每门课程的学生人数
SELECT c_id,COUNT(*) FROM Scores GROUP BY c_id;
-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT * FROM (SELECT S2.*,S.s_score,(RANK() over (ORDER BY s_score DESC )) RANK
FROM Courses C JOIN TEACHERS T on C.t_id = T.T_ID
JOIN SCORES S on C.c_id = S.C_ID
JOIN STUDENTS S2 on S.s_id = S2.S_ID
WHERE T.t_name='李四')
WHERE RANK=1;
-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT * FROM Scores S1
WHERE EXISTS(
SELECT * FROM Scores S2 WHERE S1.C_ID!=S2.c_id AND S1.s_id=S2.s_id AND S1.S_SCORE=S2.S_SCORE
);
-- 42、查询每门功成绩最好的前两名
SELECT * FROM (SELECT s_id,c_id,ROW_NUMBER() over (PARTITION BY c_id ORDER BY s_score DESC ) RO FROM Scores)
WHERE RO<=2;
-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT c_id,COUNT(*) FROM Scores GROUP BY c_id HAVING COUNT(*)>5
ORDER BY COUNT(*) DESC ,c_id;
-- 44、检索至少选修两门课程的学生学号
SELECT s_id,COUNT(*) FROM Scores GROUP BY s_id HAVING COUNT(*)>=2;
-- 45、查询选修了全部课程的学生信息
SELECT * FROM Students WHERE s_id IN (SELECT Scores.s_id FROM Scores
GROUP BY Scores.s_id HAVING COUNT(*)=(SELECT COUNT(*) FROM Courses));
-- 46、查询各学生的年龄
-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
SELECT S.*,
CASE WHEN SUBSTR(s_birth,6,2)>
SUBSTR(TO_CHAR(SYSDATE,'YYYY-MM-DD'),6,2)
THEN SUBSTR(TO_CHAR(SYSDATE,'YYYY-MM-DD'),1,4) -
SUBSTR(s_birth,1,4) -1
ELSE SUBSTR(TO_CHAR(SYSDATE,'YYYY-MM-DD'),1,4) -
SUBSTR(s_birth,1,4)
END 年龄
FROM Students S;
-- 47、查询本周过生日的学生
SELECT * FROM Students
WHERE TO_CHAR(TO_DATE(s_birth,'YYYY-MM-DD'),'ww')=TO_CHAR(SYSDATE,'ww');
-- 48、查询下周过生日的学生
SELECT * FROM Students
WHERE TO_CHAR(TO_DATE(s_birth,'YYYY-MM-DD'),'ww')=TO_CHAR(SYSDATE,'ww')+1;
-- 49、查询本月过生日的学生
SELECT * FROM Students
WHERE TO_CHAR(TO_DATE(s_birth,'YYYY-MM-DD'),'MM')=TO_CHAR(SYSDATE,'MM');
-- 50、查询下月过生日的学生
SELECT * FROM Students
WHERE TO_CHAR(ADD_MONTHS(TO_DATE(s_birth,'YYYY-MM-DD'),1),'MM')=TO_CHAR(SYSDATE,'MM');
|