查询成绩最好的前两名_SQL查询整理

SQL查询相关知识整理

af4f72943942bba254efbc837c8403ab.png

习题中会用到的表

6eed3207de68cc024adf6e3a7a550241.png

efe06213f392d36f174b4d6cf81d24a4.png

一、简单查询

1.查询姓“猴”的学生名单

select 学号,姓名
from student 
where 姓名 like ‘猴%’;

2、查询姓名中最后一个字是“猴”的学生名单

select 学号,姓名
from student 
where 姓名 like ‘%猴’;

3、查询姓名中带“猴”的学生名单

select 学号,姓名
from student
where 姓名 like ‘%猴%’;

4、查询姓“孟”老师的个数

select count (教师号)
from teacher
where 教师姓名 like ‘孟%’;

二、汇总分析

1、查询课程编号为“0002”的总成绩

select sum(成绩)
from score
group by 课程号
having 课程号=‘0002’;

2、查询选了课程的学生人数

/*查询有多少人选修了课程,select 学号
成绩表里学号有重复值需要去掉
from 从课程表查询 score
/*
select sum(distinct 学号)as 学生人数
from score

3、查询各科成绩最高和最低的分

select max(成绩)as 最高分 ,min(成绩)as最低分
from score 
group by 课程号;

4、查询每门课程被选修的学生数

select 课程号,count(学号)as学生人数
from score 
group by 课程号;

5、查询男生、女生人数

/*select 查询结果:性别,对应性别人数:汇总函数count
from 从哪张表查 student
where 查询条件:没有
group by 按性别分组
order by 没有排序
/*
select 性别,count(*)
from student
group by 性别;

6、查询平均成绩大于60分学生的学号和平均成绩

select 学号,avg(成绩)
from score
group by 学号
having avg(成绩)>60;

7、查询至少选修两门课程的学生学号

select 学号,count(课程号)as选修课程数
from score
group by 学号
having count(课程号)>=2;

8、查询同名同姓学生名单并统计同名人数

/*翻译:
查询出姓名相同的学生有谁,每个姓名相同学生的人数
查询结果:姓名,人数
条件:怎么算姓名相同?按姓名分组后人数大于等于2,因为同名的人数大于等于2
*/
select 姓名 ,count(*)as 人数
from student
group by 姓名
having count(*)>=2;

9、查询不及格的课程并按课程号从大到小排序

select 课程号
from score
where 成绩<60
order by  课程号 desc;

10、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列

select 课程号,avg(成绩)as平均成绩
from score
group by 课程号
order by 平均成绩 asc,课程号 desc;

11、检索课程号为‘0004’且分数小于60的学生学号,结果按分数降序排序

select 学号
from score
where 课程号=“0004”and 分数 <60;
order by 成绩 desc;

12、统计每门课程的学生选修人数(超过2人的课程才统计)

要求输出课程号和选修人数,查询结果按人数降序排序,若人数相同,按课程号升序排序

select 课程号,count(学号)as学生人数
from score
group by 课程号
having count(学号)>2
order by count(学号) desc,课程号 asc;

13、查询两门以上不及格课程的同学的学号及其平均成绩

select 学号,avg(成绩),count(成绩)as不及格科目数
from score
where 成绩 <60
group by 学号
having count(成绩)>=2;

三、复杂查询

1、(子查询)查询所有课程成绩小于60分学生的学号、姓名

select 学号,姓名
from student 
where 学号 in(
select 学号
from score
group by 学号
having max(成绩)<60
);

2、查询没有学全所有课的学生的学号、姓名

select 学号,姓名
from score
where 学号 in(
select 学号
from score
group by 学号
having count(课程号)<(selece count(课程号)from course)
);

3、查询出只选修了两门课程的全部学生的学号和姓名

select 学号,姓名
from score
where 学号 in (select 学号
from score
group by 学号
having count(课程号)=2
);

4、查询1990年出生的学生名单

select 学号
from student
where year(出生日期)=1990;

5、按课程号分组取成绩最大值所在行的数据

select * 
from score as a
where 成绩=(
select max(成绩)
from score as b
where a.课程号=b.课程号
);

6、按课程号分组去成绩最小值所在行的数据

select * 
from score as a
where 成绩=(
select min(成绩)
from score as b
where a.课程号=b.课程号
);

7、查询各科成绩前两名的记录

(select *
from score 
where 课程号 = ‘0001’
order by 成绩 desc 
limit 2)
union all 
(select * from score where 课程号 =‘0002’order by 成绩 desc limit 2)
union all
(select * from score where 课程号 = ‘0003’order by 成绩 desc limit 2)

四、多表查询

1、查询所有学生的学号、姓名、选课数、总成绩

select a.学号,a.姓名,count(b.课程号)as选课数,sum(b.成绩)as 总成绩
from student as a inner join score as b 
on a.学号= b.学号
group by  a.学号;

2、查询平均成绩大于85的所有学生的学号、姓名和平均成绩

select a.学号,a.姓名 ,avg(b.成绩)as平均成绩
from student as a left join score as b
on a. 学号= b.学号
group by a.学号
having avg(b.成绩)>85;

3、查询学生的选课情况:学号,姓名,课程号,课程名称

select a.学号,a.姓名,c.课程号,c.课程名称
from student as a inner join score as b 
on a.学号=b.学号
inner join course as c
on b.课程号=c.课程号;

4、查询出每门课程的及格人数和不及格人数

select 课程号,
sum(case 成绩>=60 then 1
       else 0
end)as及格人数
sum(case 成绩 <60 then 1
       else 
end)as 不及格人数
from score
group by 课程号;

4、使用分段【100-85】、【85-70】、【70-60】、【<60】来统计各科成绩,分别统计:各分数段人数、课程号和课程名称

select a.课程号,b.课程名称,
sum(case when 成绩 between 100 and 85 then 1
else 0
end )as'100-85人数'
sum (case when 成绩 <85 and 成绩>=70 then 1
else 0
end)as '85-70人数'
sum(case when 成绩<70 and 成绩>=60 then 1
else 0
end)as '70-60人数'
sum(case when 成绩<60 then 1
else 0
end) as '<60人数'
from score as a right join course as b
on a.课程号=b.课程号
group by a.课程号,b.课程名称;

5、查询课程编号为0003且课程成绩在80分以上的学生的学号和姓名

select a.姓名,a.学号

from student as a inner join score as b

on a.学号=b.学号

where b.课程号=‘0003’and b.成绩>80;

6、

fe0d9ea07e80b44f64ab621524a5a41a.png

这类题目属于行列互换,解题思路如下:

1)用常量列输出目标表的结构,可以看到查询结果已经和目标很接近

select 学号,‘课程号0001’,‘课程号0002’,‘课程号0003’

from score;

ebd0dc97adb8841ecaad5c74a90141af.png

2)使用case表达式,替换常量列为对应成绩

select 学号,(
case 课程号 when‘0001’then 成绩 else 0 end)as ‘课程号0001’,
(case 课程号 when‘0002’then 成绩 else 0 end)as ‘课程号0002’,
(case 课程号 when‘0003’then 成绩 else 0 end)as ‘课程号0003’
from score;

c9aa3c9f4a32aeef377e40247a4c5101.png

这个查询结果,每一行表示了某个学生某一门课程的成绩。

3)分组,并使用最大值函数max取出最大值

select 学号,
max(case 课程号 when '0001'then 成绩 else 0 end)as '课程号0001',
max(case 课程号 when '0001'then 成绩 else 0 end)as '课程号0001',
max(case 课程号 when '0001'then 成绩 else 0 end)as '课程号0001'
from score
group by 学号;

532008b585e6222669a12821dac1eac7.png
  • 8
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值