SQL五十题
习题来源于其他博客信息,仅供个人练习与参考
表格信息
–1.学生表
Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – --课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) --教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) --学生编号,课程编号,分数
SQL 01
1、查询"01"课程比"02"课程成绩高的学生的信息
select s_id,s_name,s_birth,s_sex
from students
where s_id = (select sc1.s_id from score sc1,score sc2
where sc1.c_id = '01'and sc2.c_id = '02'
and sc1.score > sc2.score
and sc1.s_id = sc2.s_id)
SQL 02
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
略,上题符号取反
SQL 03
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select st.s_id,st.s_name,c.avg(score)
from students st,(select s_id,avg(score) from score
group by s_id
having avg(score) >= 60 )as c
where st.s_id = c.s_id
SQL 05
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select st.s_id,st.s.name,count(sc.c_id),sum(sc.score)
from students st inner join score sc
on st.s_id = sc.s_id
group by st.s_id
SQL 06
6、查询"李"姓老师的数量
select count(t_id)from teachers
where t_name like '李%'
SQL 07
7、查询学过"张三"老师授课的同学的信息
select a.* from
student a
join score b on a.s_id=b.s_id where b.c_id in(
select c_id from course where t_id =(
select t_id from teacher where t_name = '张三'));
SQL 08
– 8、查询没学过"张三"老师授课的同学的信息
SQL 09
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select a.* from
student a,score b,score c
where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id='01' and c.c_id='02'
SQL 10
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select a.* from
student a,score b,score c
where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id='01' and c.c_id != '02'
SQL 11
11、查询没有学全所有课程的同学的信息
select st.s_id,st.s_name,st.s_birth,st.s_sex from
student st inner join score sc on st.s_id = sc.s_id
group by st.s_id
having count(sc.c_id) < (select count(distinct c_id) from course)
SQL 12
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select * from student st
where st.s_id in (select distinct s_id from score sc
where sc.c_id in (select c_id from score
where s_id = '02')
SQL 13
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
--@ouyang_1993的写法
SELECT
Student.*
FROM
Student
WHERE
s_id IN (SELECT s_id FROM Score GROUP BY s_id HAVING COUNT(c_id) = (
#下面的语句是找到'01'同学学习的课程数
SELECT COUNT(c_id) FROM Score WHERE s_id = '01'
)
)
AND s_id NOT IN (
#下面的语句是找到学过‘01’同学没学过的课程,有哪些同学。并排除他们
SELECT s_id FROM Score
WHERE c_id IN(
#下面的语句是找到‘01’同学没学过的课程
SELECT DISTINCT c_id FROM Score
WHERE c_id NOT IN (
#下面的语句是找出‘01’同学学习的课程
SELECT c_id FROM Score WHERE s_id = '01'
)
) GROUP BY s_id
) #下面的条件是排除01同学
AND s_id <> '01'
SQL 14
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select s_name from students st
where s_id not in (select sc.s_id from score sc
inner join course c on sc.c_id = c.c_id
where c.t_id in (select t_id from teachers where t_name = '张三')
)
SQL 15
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select st.s_id,st.s_name,avg(sc.score)
from students st inner join score sc on st.s_id = sc.s_id
where st.s_id in (select s_id from score
where score <60 group by s_id having count(c_id) >= 2
)
group by st.s_id
SQL 16
16、检索"01"课程分数小于60,按分数降序排列的学生信息
select st.* from students st inner join score sc on st.s_id = sc.s_id
where sc.c_id = '01' and sc.score < 60
order by score desc
SQL 17
- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select score,avg(score)
from score sc
group by s_id
order by avg(score) desc
SQL 18
18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
–及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2),
ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率,
ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 中等率,
ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优良率,
ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率
from score a left join course b on a.c_id = b.c_id
GROUP BY a.c_id,b.c_name
SQL 19
19、按各科成绩进行排序,并显示排名
select a.s_id,a.c_id,
@i:=@i +1 as i
@k:=(case when @score=a.s_score then @k else @i end) as rank
@score:=a.s_score as score
from (
select s_id,c_id,s_score from score GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s
--@惜星的写法
SELECT s1.s_id, s1.c_id, COUNT(DISTINCT s2.s_score) as rank, MIN(s2.s_score) as score
FROM score s1
INNER JOIN score s2
ON s1.s_score <= s2.s_score
GROUP BY s1.s_id, s1.c_id
ORDER BY rank asc
SQL 20
20、查询学生的总成绩并进行排名
select a.s_id,
@i:=@i+1 as i,
@k:=(case when @score=a.sum_score then @k else @i end) as rank,
@score:=a.sum_score as score
from (select s_id,SUM(s_score) as sum_score from score GROUP BY s_id ORDER BY sum_score DESC)a,
(select @k:=0,@i:=0,@score:=0)s
SQL 21
21、查询不同老师所教不同课程平均分从高到低显示
select c.t_id,avg(score)as avg_score
from course c inner join score sc on c.c_id = sc.c_id
group by c.t_id,sc.c_id
order by avg_score desc
SQL 22
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩``
select sc.s_score,st.*
from students st inner join score sc on st.s_id = sc.s_id
group by sc.c_id
order by sc.s_score desc limit 1,2
SQL 23
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分
--@启明星的指引
select distinct f.c_name,a.c_id,b.`85-100`,b.百分比,c.`70-85`,c.百分比,d.`60-70`,d.百分比,e.`0-60`,e.百分比 from score a
left join (select c_id,SUM(case when s_score >85 and s_score <=100 then 1 else 0 end) as `85-100`,
ROUND(100*(SUM(case when s_score >85 and s_score <=100 then 1 else 0 end)/count(*)),2) as 百分比
from score GROUP BY c_id)b on a.c_id=b.c_id
left join (select c_id,SUM(case when s_score >70 and s_score <=85 then 1 else 0 end) as `70-85`,
ROUND(100*(SUM(case when s_score >70 and s_score <=85 then 1 else 0 end)/count(*)),2) as 百分比
from score GROUP BY c_id)c on a.c_id=c.c_id
left join (select c_id,SUM(case when s_score >60 and s_score <=70 then 1 else 0 end) as `60-70`,
ROUND(100*(SUM(case when s_score >60 and s_score <=70 then 1 else 0 end)/count(*)),2) as 百分比
from score GROUP BY c_id)d on a.c_id=d.c_id
left join (select c_id,SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end) as `0-60`,
ROUND(100*(SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end)/count(*)),2) as 百分比
from score GROUP BY c_id)e on a.c_id=e.c_id
left join course f on a.c_id = f.c_id
SQL 24
24、查询学生平均成绩及其名次
select a.s_id,
@i:=@i+1 as i,
@k:=(case when @avg_score=a.avg_s then @k else @i end) as rank,
@avg_score:=avg_s as '平均分'
from (select s_id,ROUND(AVG(s_score),2) as avg_s from score GROUP BY s_id ORDER BY avg_s DESC)a,(select @avg_score:=0,@i:=0,@k:=0)b;
SQL 25
25、查询各科成绩前三名的记录
select a.s_id,a.c_id,a.s_score from score a
left join score b on a.c_id = b.c_id and a.s_score<b.s_score
group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3
ORDER BY a.c_id,a.s_score DESC
SQL 26
26、查询每门课程被选修的学生数
select c_id,count(s_id) from score
group by c_id
SQL 27
27、查询出只有两门课程的全部学生的学号和姓名
select s_id,s_name from students
where s_id =(
select s_id from score
group by s_id
having count(c_id) = 2
SQL 28
28、查询男生、女生人数
select count(select s_id from students where s_sex = '男')as '男生人数',count(select s_id from students where s_sex = '女')as '女生人数'
select s_sex,COUNT(s_id) as 人数 from student GROUP BY s_sex
SQL 29
29、查询名字中含有"风"字的学生信息
select st.* from students st
where s_name like '%风%'
SQL 30
30、查询同名同性学生名单,并统计同名人数
select a.s_name,a.s_sex,count(*) from student a JOIN
student b on a.s_id !=b.s_id and a.s_name = b.s_name and a.s_sex = b.s_sex
GROUP BY a.s_name,a.s_sex
SQL 31
31、查询1990年出生的学生名单
select s_id from students
where s_birth like '1990%'
SQL 32
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
--使用窗口函数
select c_id,rank() over(partition by c_id
order by avg(score) desc ,c_id ASC)
from score
SQL 33
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select st.s_id,st.s_name,avg(score)as avg
from students st left join score sc on st.s_id= sc.s_id
group by st.s_id
having avg >= 85
SQL 34
34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select st.s_name,sc.score
from students st left join score sc on st.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
where c.c_name = '数学' and sc.s_score < 60
SQL 35
35、查询所有学生的课程及分数情况
select s_id,s_score
from score
group by s_id
SQL 36
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
select st.s_name,c.c_name,sc.score
from students st left join score sc on st.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
where sc.score > 70
SQL 37
37、查询不及格的课程
select c.c_name,sc.score
from score sc
left join course c on sc.c_id = c.c_id
where sc.score < 60
SQL 38
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
select st.s_id,st.s_name
from students left join score sc on st.s_id = sc.s_id
where sc.c_id = '01' and sc.score > 80
SQL 39
39、求每门课程的学生人数
select c_id,count(s_id)
from score
group by c_id
SQL 40
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select st.*,sc.s_score
from student st inner join score sc on st.s_id = sc.s_id
inner join teacher t on sc.c_id = t.c_id
where t.t_name = '张三' and sc.score in (select max(s_score) from sc where t.t_name = '张三')
SQL 41
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select sc1.c_id,sc1.s_id,sc2.s_id.sc1.s_score
from score sc1,score sc2
where sc1.s_score = sc2.s_score and a.c_id != b.c_id
SQL 42
42、查询每门功成绩最好的前两名
select s_id,c_id
from score
group by c_id
order by s_score desc limit 2
--不采用limit函数 @启明星的指引
select a.s_id,a.c_id,a.s_score from score a
where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 ORDER BY a.c_id
SQL 43
– 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select c_id,count(s_id) as number
from score
group by c_id
having count(s_id) > 5
order by number desc,c_id asc
SQL 44
–44、检索至少选修两门课程的学生学号
select s_id
from score
group by s_id
having count(c_id) >= 2
SQL 45
45、查询选修了全部课程的学生信息
select st.* from students st
where s_id = (select s_id from score
group by s_id
having count(c_id) = (select count(distinct c_id) from course)
SQL 46
46、查询各学生的年龄
select s_id,s_birth,(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y') - (case when DATE_FORMAT(NOW(),'%m%d') > DATE_FORMAT(s_birth,'%m%d') then 0 else 1 end)) as age
from students
SQL 47
47、查询本周过生日的学生
select s_id,s_birth
from students
where week(date_format(now(),'%Y%m%d')) = week(s_birth)
SQL 48
48、查询下周过生日的学生
select st.*
from students st
where week(s_birth) = week(date_format(now(),'%Y%m%d')) + 1
SQL 49
49、查询本月过生日的学生
select s_id,s_birth
from students
where month(date_format(now(),'%Y%m%d')) = month(s_birth)
SQL 50
50、查询下月过生日的学生
select s_id,s_birth
from students
where month(date_format(now(),'%Y%m%d')) + 1 = month(s_birth)