当前位置:Gxlcms > 数据库问题 > 2016030208 - sql50题练习题

2016030208 - sql50题练习题

时间:2021-07-01 10:21:17 帮助过:2人阅读

  sql50题练习参看:http://blog.sina.com.cn/s/blog_6d1d0bf80100zm8l.html

  -- 创建教学系统的数据库,表,以及数据
  -- student(sno,sname,sage,ssex) 学生表
  -- course(cno,cname,tno) 课程表
  -- sc(sno,cno,score) 成绩表
  -- teacher(tno,tname) 教师表

  1、查询“001”课程比“002”课程成绩高的所有学生的学号;
    step1:查询所有学生的学号和成绩关联的表是sc表
    step2:查询课程1和课程2的成绩
    select score, sno from sc where sc.cno = 1;   //alias a
    select score, sno from sc where sc.cno = 2;   //alias b
    step3:查询同一人的课程1和2的分数进行比较
    a.score > b.score and a.sno = b.sno
  select a.sno from (select score, sno from sc where sc.cno = 1) a,
                  (select score, sno from sc where sc.cno = 2) b
              where a.score > b.score and a.sno = b.sno;
  

  2.查询平均成绩大于60分的同学的学号和平均成绩;

  step1:查询所涉及的表是sc

  step2:平均成绩 avg()方法,分组后使用聚合函数

  select sno, avg(score) from sc group by sno having avg(score) > 60;

  

  

  

  

  

  

  

2016030208 - sql50题练习题

标签:

人气教程排行