当前位置:   article > 正文

【小5】sql server基础面试题解答之查询每门课程都及格的学生名称_sql查询出所选课程全部都及格的学生姓名?

sql查询出所选课程全部都及格的学生姓名?

1、题目假设,有一表a,保存的是学生的各个科目成绩

2、查询要求,每门课程都及格的学生名称,实现代码如下

考查知识点:group by 字段,having + 内置函数筛选条件过滤

  1. select name,min(convert(int,fenshu)) as fenshu from (
  2. select '张三' as name,'语文' as kecheng,'81' as fenshu
  3. union
  4. select '张三' as name,'数学' as kecheng,'75' as fenshu
  5. union
  6. select '李四' as name,'语文' as kecheng,'76' as fenshu
  7. union
  8. select '李四' as name,'数学' as kecheng,'90' as fenshu
  9. union
  10. select '王五' as name,'语文' as kecheng,'81' as fenshu
  11. union
  12. select '王五' as name,'数学' as kecheng,'100' as fenshu
  13. union
  14. select '王五' as name,'英语' as kecheng,'90' as fenshu
  15. ) as a group by name
  16. having min(convert(int,fenshu))>80

3、同样原理,查询每门课都大于80分的学生

  1. --用一条语句,查询出每门课都大于80的学生姓名(反向逆推:课程中分数最低大于80,则也是每门课都大于80)
  2. --用户、每门功课
  3. SELECT t.name,MIN(CONVERT(INT,t.fenshu)) fenshu FROM (
  4. SELECT '张三' AS name,'语文' AS kecheng,'81' AS fenshu
  5. UNION ALL
  6. SELECT '张三' AS name,'数学' AS kecheng,'75' AS fenshu
  7. UNION ALL
  8. SELECT '李四' AS name,'语文' AS kecheng,'76' AS fenshu
  9. UNION ALL
  10. SELECT '李四' AS name,'数学' AS kecheng,'90' AS fenshu
  11. UNION ALL
  12. SELECT '王五' AS name,'语文' AS kecheng,'81' AS fenshu
  13. UNION ALL
  14. SELECT '王五' AS name,'数学' AS kecheng,'100' AS fenshu
  15. UNION ALL
  16. SELECT '王五' AS name,'英语' AS kecheng,'90' AS fenshu
  17. --UNION ALL
  18. --SELECT '王五' AS name,'体育' AS kecheng,'79' AS fenshu
  19. ) AS t GROUP BY t.name HAVING MIN(CONVERT(INT,t.fenshu))>80

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/409815
推荐阅读
相关标签
  

闽ICP备14008679号