摘:SQL 常见题练习

--1.学生表
Student(SId,Sname,Sage,Ssex)
--SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别

--2.课程表
Course(CId,Cname,TId)
--CId 课程编号,Cname 课程名称,TId 教师编号

--3.教师表
Teacher(TId,Tname)
--TId 教师编号,Tname 教师姓名

--4.成绩表
SC(SId,CId,score)
--SId 学生编号,CId 课程编号,score 分数

SQL 常见题练习

1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
解:因为需要全部的学生信息,则需要在sc表中得到符合条件的SId后与student表进行join
select * from Student RIGHT JOIN (
select t1.SId,class1,class2 from
(select SId ,score as class1 from SC where SC.CId = '01')as t1,
(select SId ,score as class2 from SC where SC.CId = '02')as t2 where t1.SId = t2.SId and t1.class1 > t2.class2 )r
on Student.SId = r.SId


1.1查询同时存在" 01 "课程和" 02 "课程的情况
select * from
(select * from SC where SC.CId = '01') as t1,(select * from SC where SC.CId = '02') as t2
where t1.SId = t2.SId

1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select * from
(select * from SC where SC.CId = '01') as t1
left join
(select * from SC where SC.CId = '02') as t2
on t1.SId = t2.SId

1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
select * from sc
where sc.SId not in(select SId from where sc.CId = '01')
and sc.CId = '02';


2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select Student.SId,Sname,ss from Student,(
select SId,avg(score) as ss from sc group by SId having avg(score)>60)r
where Student.SId = r.SId;

3.查询在 SC 表存在成绩的学生信息
select DISTINCT student.* from Student,sc where Student.SId = sc.SId
ps:DISTINCT 用于返回唯一不同的值

4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select s.sid,s.sname,r.coursenumber,r.scoresum from(
(select student.sid,student.sname from student)s
left join
(select sc.sid,sum(sc.score)as scoresum ,count(sc.cid)as coursenumber from sc group by sc.sid )r
on s.sid = r.sid
);

4.1 查有成绩的学生信息
select *from student where student.sid in (select sc.sid from sc);


5.查询「李」姓老师的数量
select count(*) from teacher where tname like '李%';

6.查询学过「张三」老师授课的同学的信息
select student.* from student,teacher,course,sc
where student.sid = sc.sid and course.cid = sc.cid and course.tid = teacher.tid and tname = '张三';


7.查询没有学全所有课程的同学的信息
select * from student where student.sid not in(
select sc.sid from sc group by sc.sid having count(sc.cid) = (select count(cid) from course)
)

8.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信


9.查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from student where student.sid not in(
select sc.sid from sc,course,teacher where
sc.cid = course.cid
and course.tid = teacher.tid
and teacher.tname = "张三"
);


10.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select student.sid,student.sname,avg(sc.score) from student,sc
where
student.sid = sc.sid and sc.score<60 group by sc.sid having count(*)>1;

11.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select student.* ,sc.score from student,sc
where student.sid = sc.sid and sc.score < 60 and cid = '01' order by sc.score desc;


12.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select * from sc
left join(
select sid,avg(score) as avscore from sc group by sid)r
)
on sc.sid = r.sid order by avscore desc;

13.查询各科成绩最高分、最低分和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。
及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90。
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select sc.CId ,max(sc.score) as 最高分,min(sc.score) as 最低分, avg(sc.score) as 平均分,count(*)as 选修人数,
sum(case when sc.score>=60 then 1 else 0 end)/count(*) as 及格率
from sc group by sc.CId order by count(*) desc,sc.CId asc

ps:case when sc.score>=60 then 1 else 0 end,即符合sc.score>=60加1,否则加0

14.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
用sc中的score和自己进行对比,来计算“比当前分数高的分数有几个”。
select a.cid,a.sid,a.score,count(b.score)+1 as rank from sc as a
left join sc as b
on a.score < b.score and a.cid = b.cid
group by a.cid,a.sid,a.score order by a.cid,rank asc;


15.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select course.cname,course.cid,
sum(case when sc.score<=100 and sc.score>85 then 1 else 0 end) as "[100-85]",
sum(case when sc.score<=85 and sc.score>70 then 1 else 0 end) as "[85-70]",
sum(case when sc.score<=70 and sc.score>60 then 1 else 0 end) as "[70-60]",
sum(case when sc.score<=60 and sc.score>0 then 1 else 0 end) as "[0-60]",
from sc left join course on sc.cid = course.cid group by sc.cid;

16.查询每门课程被选修的学生数
select cid,count(sid) from sc group by cid;

17.查询出只选修两门课程的学生学号和姓名
select student.sid,student.sname from sc,student where student.sid =sc.sid
group by sc.sid having count(*) = 2

18.查询男生、女生人数
select ssex,cout(*) from student group by ssex

原文地址:https://www.cnblogs.com/Ryana/p/10036434.html