明确题意:

统计作答SQL类别的试卷得分大于过80的人的用户等级分布,按数量降序排序


问题分解:

  • 找到每个满足条件的用户及他们的等级,生成子表t_ml_user_level:
    • 内连接试卷作答表和用户信息表:exam_record JOIN user_info using(uid)
    • 筛选作答SQL类别的试卷得分大于过80的人:
      • 筛选分数大于80:WHERE score > 80
      • 筛选试卷类别为SQL:exam_id IN (SELECT exam_id FROM examination_info WHERE tag = 'SQL')
  • 按用户等级分组:GROUP BY level
  • 统计各等级数量:count( uid ) AS level_cnt

细节问题:

  • 表头重命名:as
  • 按数量降序排序:ORDER BY level_cnt DESC

完整代码:

SELECT level, count( uid ) AS level_cnt 
FROM (
    SELECT DISTINCT exam_record.uid AS uid, level 
    FROM exam_record
    JOIN user_info using(uid) 
    WHERE score > 80 AND exam_id IN ( 
        SELECT exam_id FROM examination_info WHERE tag = 'SQL' 
    ) 
) AS t_ml_user_level 
GROUP BY level 
ORDER BY level_cnt DESC;