1、創建表
drop table student;
create table student(
id varchar(20),-- 編號
class varchar(20),-- 年級
score int-- 分數
);
2、創建測試數據
delete from student;
insert student values('1','一年級',82);
insert student values('2','一年級',95);
insert student values('3','一年級',82);
insert student values('4','一年級',40);
insert student values('5','一年級',20);
insert student values('6','二年級',95);
insert student values('7','二年級',40);
insert student values('8','二年級',3);
insert student values('9','二年級',60);
insert student values('10','二年級',10);
insert student values('11','三年級',70);
insert student values('12','三年級',60);
insert student values('13','三年級',40);
insert student values('14','三年級',90);
insert student values('15','三年級',20);
3、查詢每個班級的前三名
select a.class,a.score
from student a
where (select count(*) from student where class=a.class and a.score<score)<3
order by a.class,a.score desc;