题目描述:sql语句查询用户分数大于其所在工作(job)分数的平均分的所有grade的属性,并且以id的升序排序。
个人思路:1.先找出每个工作的平均分。2.用grade表与1进行比较,找出分数大于均分的数据并按id升序排序。
代码(1.找出均分):

select job, avg(score) as avg_score
from grade
group by job

代码(2.超出分数大于均分的数据):

select t1.*
from grade t1 join
(
    select job, avg(score) as avg_score
    from grade
    group by job 
) t2
on t1.job=t2.job
where score > avg_score
order by t1.id