经典sql查询

Student(sid,Sname,Sage,Ssex) 学生表
Course(Cid,Cname,Tid) 课程表
SC(Sid,Cid,score) 成绩表
Teacher(Tid,Tname) 教师表

查询各科成绩前三名的记录:(考虑成绩并列情况)(oracle数据库)

select t1.sid,t1.sname,c.cid,c.cname,t1.score,t1.rn as 名次 from course c,(
select s.sid,s.sname,t.cid,t.score,t.rn from Student s,
(
select * from (
select sid,cid,score, row_number() over(partition by Cid order by score desc) rn from sc) where rn<=3)t where s.sid=t.sid) t1
where c.cid=t1.cid
order by c.cname,t1.rn

统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

select c.cname,t.* from course c,(
select cid,
count(case when score >= 85 and score<=100 then 1 else null end)优秀生,
count(case when score >= 70 and score<85 then 1 else null end)优良生,
count(case when score >= 60 and score<70 then 1 else null end)中等生,
count(case when score<60 then 1 else null end)差生
 from sc group by cid) t where c.cid=t.cid

猜你喜欢

转载自blog.csdn.net/liyingying111111/article/details/16883975