接着训练一
I、sql训练
21、查询不同老师所教不同课程平均分从高到低显示
select t3.t_id, t3.t_name, avg(t1.s_score) as avg_score from score t1 left join course t2 on t1.c_id = t2.c_id left join teacher t3 on t2.t_id = t3.t_id group by t3.t_id, t3.t_name order by avg_score desc;
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
这里学习到rank()函数
select * from student t1 join (select s_id,s_score,c_id, (rank() over(partition by c_id order by s_score desc)) as 排名 from score ) as t2 on t1.s_id =t2.s_id
23、
select t2.c_id,t2.c_name, (sum(case when s_score between 85 and 100 then 1 else 0 end)/count(s_score))*100 a, (sum(case when s_score <85 and s_score>70 then 1 else 0 end)/count(s_score))*100 b, (sum(case when s_score between 60 and 70 then 1 else 0 end)/count(s_score))*100 c, (sum(case when s_score between 0 and 60 then 1 else 0 end)/count(s_score))*100 d from score t1 join course t2 on t1.c_id=t2.c_id group by t1.c_id
24、
select avg(s_score), rank() over(order by avg(s_score)) from score group by s_id
25、
select * from (select c_id,s_id,dense_rank() over(partition by c_id order by s_score) as ran from score) t1 where t1.ran<4
26、
select c_id,count(s_id) from score group by c_id
27、
select s_id,s_name from student where s_id in (select s_id from score group by s_id having count(s_score) = 2)
28、
select s_sex,count(s_id) from student group by s_sex;
29、
select * from student where s_name like '%风%'
30、
select s_name,count(s_id) as count_num from student group by s_name having count(s_id)>1
31、
select * from student where s_birth like '1990%'
32、
连续两个条件排序,只需要加个条件即可
select avg(s_score) from score group by c_id order by avg(s_score) desc,c_id asc;
33、
select t1.s_id,t1.s_name,avg(t2.s_score) as avg_score from student t1 join score t2 on t1.s_id=t2.s_id group by t1.s_id having avg_score >=85
34、
select t1.s_name,t2.s_score from student t1 join score t2 on t1.s_id =t2.s_id join course t3 on t2.c_id =t3.c_id where t3.c_name='数学' and t2.s_score <60