sql练习题
1.查询" 01 “课程⽐” 02 "课程成绩⾼的学⽣的信息及课程分数
SELECT s1.sid,s1.score from
(select sid,score FROM sc where cid='01') as s1
join
(SELECT sid,score FROM sc where cid='02') as s2
on s1.sid=s2.sid
WHERE s1.score>s2.score;
2.查询同时存在" 01 “课程和” 02 "课程的情况
SELECT s1.sid,s1.cid,s1.score,s2.cid,s2.score from
(select sid,cid,score FROM sc where cid='01') as s1
join
(SELECT sid,cid,score FROM sc where cid='02') as s2
on s1.sid=s2.sid
3.查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
SELECT s1.*,s2.* from
(select sid,cid,score FROM sc where cid='01') as s1
left join
(SELECT sid,cid,score FROM sc where cid='02') as s2
on s1.sid=s2.sid
4.查询不存在" 01 “课程但存在” 02 "课程的情况
SELECT * from sc
where sid not in(select sid FROM sc where cid='01')
and cid='02';
5.查询平均成绩⼤于等于 60 分的同学的学⽣编号和学⽣姓名和平均成绩
SELECT sc.SId, sname,ROUND(AVG(score))
from sc,student
WHERE sc.SId=student.SId
GROUP BY sid,sname HAVING AVG(score)>=60
6.查询在 SC 表存在成绩的学⽣信息
SELECT DISTINCT student.* FROM student JOIN sc on sc.SId=student.SId
7.查询所有同学的学⽣编号、学⽣姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
SELECT student.SId,student.sname,count(sc.cId) ,SUM(sc.score)
from student left JOIN sc
on student.SId=sc.sid
GROUP BY student.SId,student.Sname
8.查询「李」姓⽼师的数量
SELECT COUNT(Tname) FROM teacher WHERE Tname LIKE '李%'
9.查询学过「张三」⽼师授课的同学的信息
select st.* from student st
left join score sc on sc.sid=st.sid
left join course c on c.cid=sc.cid
left join teacher t on t.tid=c.tid
where t.tname="张三"
10.查询没有学全所有课程的同学的信息
select SId, count(CId) as Cn from sc GROUP BY SId HAVING Cn<(select count(CId) as Cnum from sc)
11.查询⾄少有⼀⻔课与学号为" 01 "的同学所学相同的同学的信息
select distinct st.* from student st
left join sc on sc.sid=st.sid
where sc.cid in (
select sc2.cid from student st2
left join sc sc2 on sc2.sid=st2.sid
where st2.sid ='01'
)
12.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
select st.* from student st
left join sc sc on sc.sid=st.sid
group by st.sid
having group_concat(sc.cid) =
(
select group_concat(sc2.cid) from student st2
left join sc sc2 on sc2.sid=st2.sid
where st2.sid ='01'
)
13.查询没学过"张三"⽼师讲授的任⼀⻔课程的学⽣姓名
select st.s_name from student st
where st.s_id not in (
select sc.s_id from score sc
inner join course c on c.c_id=sc.c_id
inner join teacher t on t.t_id=c.t_id and t.t_name="张三"
14.查询两⻔及其以上不及格课程的同学的学号,姓名及其平均成绩
select st.sid,st.sname,avg(sc.score) from student st
left join sc on sc.sid=st.sid
where sc.sid in (
select sc.sid from sc
where sc.score<60 or sc.score is NULL
group by sc.sid having COUNT(1)>=2
)
group by st.sid
15.检索" 01 "课程分数⼩于 60,按分数降序排列的学⽣信息
select st.*,sc.score from student st
inner join sc on sc.sid=st.sid and sc.cid="01" and sc.score<60
order by sc.score desc
16.按平均成绩从⾼到低显示所有学⽣的所有课程的成绩以及平均成绩
SELECT sc.*, a FROM sc LEFT JOIN
( SELECT sid, AVG(score) AS a FROM sc GROUP BY SId )r
ON sc.SId = r.sid ORDER BY a DESC
17.查询各科成绩最⾼分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最⾼分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修⼈数,查询结果按⼈数降序排列,若⼈数相同,按课程号升序排列
select c.cid,c.cname,max(sc.score) "最高分",MIN(sc2.score) "最低分",avg(sc3.score) "平均分"
,((select count(sid) from sc where score>=60 and cid=c.cid )/(select count(sid) from sc where cid=c.cid)) "及格率"
,((select count(sid) from sc where score>=70 and score<80 and cid=c.cid )/(select count(sid) from sc where cid=c.cid)) "中等率"
,((select count(sid) from sc where score>=80 and score<90 and cid=c.cid )/(select count(sid) from sc where cid=c.cid)) "优良率"
,((select count(sid) from sc where score>=90 and cid=c.cid )/(select count(sid) from sc where cid=c.cid)) "优秀率"
from course c
left join sc on sc.cid=c.cid
left join sc sc2 on sc2.cid=c.cid
left join sc sc3 on sc3.cid=c.cid
group by c.cid
18.按各科平均成绩进⾏排序,并显示排名, Score 重复时保留名次空缺
SELECT s.SId, s.Sname,
avg(score) as 平均分
from student s LEFT JOIN sc ON s.sid = sc.sid GROUP BY s.sid
ORDER BY 平均分
select s2.CId,s2.avg_score,count(DISTINCT s1.avg_score) as rank
FROM
(select CId,round(avg(score),2) as avg_score from
sc group by SId) as s1
join
(select CId,round(avg(score),2) as avg_score from
sc group by SId) as s2
on s1.avg_score >= s2.avg_score
GROUP BY s2.cid,s2.avg_score
ORDER BY rank
19.按各科平均成绩进⾏排序,并显示排名, Score 重复时不保留名次空缺
select a.*,AVG(a.score) as 平均分 from sc a
left join sc b on a.cid = b.cid and a.score < b.score
group by a.sid
order by a.cid,平均分
select b.CId,b.avg_score,@i:=@i+1 as rank from (SELECT @i:=0) as a,
(select CId,round(avg(score),2) as avg_score from sc GROUP BY cid ORDER BY avg_score desc) as b
20.查询学⽣的总成绩,并进⾏排名,总分重复时保留名次空缺
SELECT s.SId, s.Sname,
sum(score) as 总分
from student s LEFT JOIN sc ON s.SId = sc.SId GROUP BY s.SId
21.查询学⽣的总成绩,并进⾏排名,总分重复时不保留名次空缺
select st.SId,st.sname
,(case when sum(sc.score) is null then 0 else sum(sc.score) end)
from student as st
left join sc on sc.sid=st.sid
group by st.sid order by sum(sc.score) desc
22.统计各科成绩各分数段⼈数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0]及所占百分⽐
select c.cid,c.cname
,((select count(1) from sc where sc.cid=c.cid and sc.score<=100 and sc.score>80)/(select count(1) from sc where sc.cid=c.cid )) "100-85"
,((select count(1) from sc where sc.cid=c.cid and sc.score<=85 and sc.score>70)/(select count(1) from sc where sc.cid=c.cid )) "85-70"
,((select count(1) from sc where sc.cid=c.cid and sc.score<=70 and sc.score>60)/(select count(1) from sc where sc.cid=c.cid )) "70-60"
,((select count(1) from sc where sc.cid=c.cid and sc.score<=60 and sc.score>=0)/(select count(1) from sc where sc.cid=c.cid )) "60-0"
from course c order by c.cid
23.查询各科成绩前三名的记录
select a.* from (
select st.sid,st.sname,c.cid,c.cname,sc.score from student st
left join sc on sc.sid=st.sid
inner join course c on c.cid=sc.cid and c.cid='01'
order by sc.score desc LIMIT 0,3) a
union all
select b.* from (
select st.sid,st.sname,c.cid,c.cname,sc.score from student st
left join sc on sc.sid=st.sid
inner join course c on c.cid=sc.cid and c.cid='02'
order by sc.score desc LIMIT 0,3) b
union all
select c.* from (
select st.sid,st.sname,c.cid,c.cname,sc.score from student st
left join sc on sc.sid=st.sid
inner join course c on c.cid=sc.cid and c.cid='03'
order by sc.score desc LIMIT 0,3) c
24.查询每⻔课程被选修的学⽣数
select c.cid,c.cname,count(1) from course c
left join sc on sc.cid=c.cid
inner join student st on st.sid=c.cid
group by st.sid
25.查询出只选修两⻔课程的学⽣学号和姓名
select st.sid,st.sname from student st
left join sc on sc.sid=st.sid
inner join course c on c.cid=sc.cid
group by st.sid having count(1)=2
26.查询男⽣、⼥⽣⼈数
select st.ssex,count(1) from student st group by st.ssex
27.查询名字中含有「⻛」字的学⽣信息
select st.* from student st where st.sname like "%风%";
28.查询同名同性学⽣名单,并统计同名⼈数
select st.*,count(1) from student st group by st.s_name,st.s_sex having count(1)>1
29.查询 1990 年出⽣的学⽣名单
select st.* from student st where st.sage like "1990%";
30.查询每⻔课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select c.cid,c.cname,avg(sc.score) from course c
inner join sc on sc.cid=c.cid
group by c.cid order by avg(sc.score) desc,c.cid asc
31.查询平均成绩⼤于等于 85 的所有学⽣的学号、姓名和平均成绩
select st.sid,st.sname,avg(sc.score) from student st
left join sc on sc.sid=st.sid
group by st.sid having avg(sc.score)>=85
32.查询课程名称为「数学」,且分数低于 60 的学⽣姓名和分数
select st.sid,st.sname,sc.score from student st
inner join sc on sc.sid=st.sid and sc.score<60
inner join course c on c.cid=sc.cid and c.cname ="数学"
33.查询所有学⽣的课程及分数情况(存在学⽣没成绩,没选课的情况)
select st.sid,st.sname,c.cname,sc.score from student st
left join sc on sc.sid=st.sid
left join course c on c.cid =sc.cid
order by st.sid,c.cname
34.查询任何⼀⻔课程成绩在 70 分以上的姓名、课程名称和分数
select st2.sid,st2.sname,c2.cname,sc2.score from student st2
left join sc sc2 on sc2.sid=st2.sid
left join course c2 on c2.cid=sc2.cid
where st2.sid in(
select st.sid from student st
left join sc on sc.sid=st.sid
group by st.sid having min(sc.score)>=70)
order by sid
35.查询不及格的课程
select st.sid,c.cname,st.sname,sc.score from student st
inner join sc on sc.sid=st.sid and sc.score<60
inner join course c on c.cid=sc.cid
36.查询课程编号为 01 且课程成绩在 80 分以上的学⽣的学号和姓名
select st.sid,st.sname,sc.score from student st
inner join sc on sc.sid=st.sid and sc.cid="01" and sc.score>=80
37.求每⻔课程的学⽣⼈数
select c.cid,c.cname,count(1) from course c
inner join score sc on sc.cid=c.cid
group by c.cid
38.成绩有重复,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
select sId, Score, Rank from (
select sId, Score, @tempRank := @tempRank + if(@preScore := Score, 0, 1) as Rank,
@preScore := Score from Sc,
(select @tempRank:= 0) r, (select @preScore:= null) p
order by Score desc, SId asc
) t
limit 0,1
39.成绩不重复的情况下,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
select st.*,c.cname,sc.score,t.tname from student st
inner join sc on sc.sid=st.sid
inner join course c on c.cid=sc.cid
inner join teacher t on t.tid=c.tid and t.tname="张三"
order by sc.score desc
limit 0,1
40.查询不同课程成绩相同的学⽣的学⽣编号、课程编号、学⽣成绩
select st.sid,st.sname,sc.cid,sc.score from student st
left join sc on sc.sid=st.sid
left join course c on c.cid=sc.cid
where (
select count(1) from student st2
left join sc sc2 on sc2.sid=st2.sid
left join course c2 on c2.cid=sc2.cid
where sc.score=sc2.score and c.cid!=c2.cid
)>1
41.查询每⻔课程成绩最好的前两名
select a.sid,a.cid,a.score from sc a
where (select COUNT(1) from sc b where b.cid=a.cid and b.score>=a.score)<=2 order by a.cid
42.统计每⻔课程的学⽣选修⼈数(超过 5 ⼈的课程才统计)
select sc.cid,count(1) from sc
left join course c on c.cid=sc.cid
group by c.cid having count(1)>5
order by count(1) desc,sc.cid asc
43.检索⾄少选修两⻔课程的学⽣学号
select st.sid from student st
left join sc on sc.sid=st.sid
group by st.sid having count(1)>=2
44.查询选修了全部课程的学⽣信息
select st.* from student st
left join sc on sc.sid=st.sid
group by st.sid having count(1)=(select count(1) from course)
45.查询各学⽣的年龄,只按年份来算
select st.*,timestampdiff(year,st.sage,now()) from student st
46.按照出⽣⽇期来算,当前⽉⽇ < 出⽣年⽉的⽉⽇则,年龄减⼀
47.查询本周过⽣⽇的学⽣
select st.* from student st
where week(now())=week(date_format(st.sage,'%Y%m%d'))
48.查询下周过⽣⽇的学⽣
select st.* from student st
where week(now())+1=week(date_format(st.sage,'%Y%m%d'))
49.查询本⽉过⽣⽇的学⽣
select st.* from student st
where month(now())=month(date_format(st.sage,'%Y%m%d'))
50.查询下⽉过⽣⽇的学⽣
select st.* from student st
where month(timestampadd(month,1,now()))=month(date_format(st.sage,'%Y%m%d'))
-- 或
select st.* from student st where (month(now()) + 1) mod 12 = month(date_format(st.sage,'%Y%m%d'))