sql习题:http://www.cnblogs.com/wupeiqi/articles/5729934.html

习题答案参考:https://www.cnblogs.com/wupeiqi/articles/5748496.html  (有些答案有错)

  1. -- SELECT count(*) from score WHERE num>60; -- 查找分数大于60的个数
  2. -- select count(cid),teacher_id from course group by teacher_id;
  3. -- select tid,teacher.tname,course.cname from course left join teacher on course.teacher_id = teacher.tid;
  4. -- select count(sid),gender from student GROUP BY gender -- 男女生个数
  5.  
  6. -- 2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
  7. -- 找出生物成绩,找出物理成绩,联合这两张临时表,找出B.num > P.num
  8. -- select B.student_id,B.cname,B.num as b_num,P.cname,P.num as p_num from
  9. -- (select * from score left join course on score.course_id = course.cid where cname = '生物') as B
  10. -- inner join
  11. -- (select * from score left join course on score.course_id = course.cid where cname = '物理') as P
  12. -- on B.student_id = P.student_id where B.num > P.num;;
  13. -- 3、查询平均成绩大于60分的同学的学号和平均成绩;(进阶:以及姓名)
  14. -- 首先选择出平均分大于60分的同学的学号,再和学生表join,选择出姓名
  15. -- SELECT
  16. -- student.sid,
  17. -- student.sname,
  18. -- b1.avg_num
  19. -- FROM
  20. -- ( SELECT avg( num ) AS avg_num, student_id FROM score GROUP BY student_id HAVING avg( num ) > 60 ) AS b1
  21. -- LEFT JOIN student ON b1.student_id = student.sid;
  22.  
  23. -- 4、查询所有同学的学号、姓名、选课数、总成绩;(两种解法,另一种是先把表连起来再group by
  24. -- 这里注意 count(1)的用法, 类似select age,1 from t1; 会出现列名1,属性全为1
  25. -- 首先成绩表和学生表连表,再根据学号进行分组,然后选择出学号,姓名,聚合学科数,求和num
  26. -- SELECT
  27. -- student.sid,
  28. -- student.sname,
  29. -- b2.course_num,
  30. -- b2.sum_num
  31. -- FROM
  32. -- ( SELECT student_id, count( course_id ) AS course_num, sum( num ) AS sum_num FROM score GROUP BY student_id ) AS b2
  33. -- LEFT JOIN student ON b2.student_id = student.sid;
  34.  
  35. -- select student.sid,student.sname,count(1) as course_num,sum(num) from score left join student on score.student_id = student.sid group by score.student_id
  36.  
  37. -- 5、查询姓“李”的老师的个数;
  38. -- select count(1) from teacher where tname like '李%';
  39.  
  40. -- 6、查询没学过“李平”老师课的同学的学号、姓名;
  41. -- 首先课程表和老师表join,选择出李平老师的课程id,然后在成绩表中把选择了 这些课程id的学号选出,用学号分组后,用not in 从学生表中,选择出没有选择过课程的学号和姓名
  42. -- select sid,sname from student where sid not in (select student_id from score where course_id in (select course.cid from course left join teacher on course.teacher_id=teacher.tid where tname = '李平老师') group by student_id );
  43. --
  44.  
  45. -- 7、查询学过“1”课程并且也学过编号“2”课程的同学的学号、姓名;
  46. -- 先查到既选择001又选择002课程的所有同学
  47. -- 根据学生进行分组,如果学生数量等于2表示,两门均已选择
  48. -- select student.sid,student.sname from
  49. -- (select student_id from score where course_id in (1,2) group by student_id having count(*)>1) as b4
  50. -- left join student on b4.student_id = student.sid;
  51.  
  52. -- 8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
  53. -- SELECT sname,sid from student where sid in (select student_id from score where course_id in (select course.cid from course left join teacher on course.teacher_id = teacher.tid where tname='李平老师') group by student_id having count(*) = (select count(cid) from course left join teacher on course.teacher_id = teacher.tid where tname='李平老师'));
  54.  
  55. -- 10、查询有课程成绩小于60分的同学的学号、姓名;
  56. # distinct 如果有重复项,只选择一个
  57. -- select sid,sname from student where sid in (
  58. -- select distinct student_id from score where num<60);
  59.  
  60. -- 11、查询没有学全所有课的同学的学号、姓名;
  61. -- select sid,sname from student where sid not in (select student_id from score group by student_id having count(*)=(select count(1) from course));
  62.  
  63. -- 12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
  64. -- select distinct student_id,student.sname from score left join student on score.student_id = student.sid where student_id != 1 and course_id in (select course_id from score where student_id = 1);
  65.  
  66. -- select student_id,sname, count(course_id)
  67. -- from score left join student on score.student_id = student.sid
  68. -- where student_id != 1 and course_id in (select course_id from score where student_id = 1) group by student_id
  69.  
  70. -- 13、查询至少学过学号为“001”同学所有课的其他同学学号和姓名;
  71. -- select student_id from score where student_id !=1 and course_id in (select course_id from score where student_id = 1) group by student_id having count(*) >= (select count(course_id) from score where student_id = 1);
  72.  
  73. -- 14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
  74. #1.课程包含 002,且课程数一样
  75. -- select student_id,sname from score left join student on score.student_id = student.sid where student_id in (
  76. -- select student_id from score where student_id != 2 group by student_id HAVING count(course_id) = (select count(1) from score where student_id = 2)
  77. -- ) and course_id = (select count(1) from score where student_id = 2)
  78.  
  79. -- 16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;
  80. -- 思路:
  81. -- 由于insert 支持
  82. -- inset into tb1(xx,xx) select x1,x2 from tb2;
  83. -- 所有,获取所有没上过002课的所有人,获取002的平均成绩
  84. --
  85. -- insert into score(student_id, course_id, num) select sid,2,(select avg(num) from score where course_id = 2)
  86. -- from student where sid not in (
  87. -- select student_id from score where course_id = 2)
  88.  
  89. -- 17、按平均成绩从低到高显示所有学生的“生物”、“物理”、“体育”三门的课程成绩,按如下形式显示: 学生ID,生物,物理,体育,有效课程数,有效平均分;
  90. -- SELECT
  91. -- student_id,
  92. -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 1) as 语文,
  93. -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 2) as 数学,
  94. -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 3) as 英语
  95. -- from score as s1;
  96.  
  97. -- 18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
  98. -- select course_id,max(num),min(num),cname,case when min(num) < 10 THEN 0 else min(num) end from score left join course on score.course_id = course.cid group by course_id;
  99.  
  100. -- 19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
  101. -- select course_id,avg(num),sum(case when num<60 then 0 else 1 end),sum(1),sum(case when num<60 then 0 else 1 end)/sum(1) as b from score group by course_id order by avg(num) asc,b desc;
  102.  
  103. -- 20、课程平均分从高到低显示(显示任课老师);
  104. -- select course_id,cname,tname,avg(if(isnull(score.num),0,score.num)) from score
  105. -- left join course on score.course_id = course.cid
  106. -- left join teacher on course.teacher_id = teacher.tid
  107. -- group by course_id order by avg(num) desc;
  108.  
  109. -- 21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
  110. -- select * from
  111. -- (
  112. -- select
  113. -- student_id,
  114. -- course_id,
  115. -- num,
  116. -- 1,
  117. -- (select num from score as s2 where s2.course_id = s1.course_id group by s2.num order by s2.num desc limit 0,1),
  118. -- (select num from score as s2 where s2.course_id = s1.course_id group by s2.num order by s2.num desc limit 3,1) as cc
  119. --
  120. -- from score as s1
  121. -- ) as B
  122. -- where B.num > B.cc order by B.course_id desc;
  123.  
  124. -- 26、查询同名同姓学生名单,并统计同名人数;
  125. -- select sname,count(1) from student group by sname
  126.  
  127. -- 27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
  128. -- select avg(if(isnull(score.num),0,score.num)),course_id from score group by course_id order by avg(num) asc,course_id desc;
  129.  
  130. -- 29、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
  131. -- select student.sname,score.num from score
  132. -- left join course on score.course_id = course.cid
  133. -- left join student on score.student_id = student.sid
  134. -- where course.cname = '生物' and score.num < 60;
  135.  
  136. -- 32、查询选修“李平老师”所授课程的学生中,成绩最高的学生姓名及其成绩;
  137. -- select student_id,sname,max(num) from score left join student on score.student_id = student.sid where course_id in (
  138. -- select course.cid from course left join teacher on course.teacher_id = teacher.tid where tname = '李平老师') group by student_id order by max(num) desc limit 0,1;
  139.  
  140. -- 34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
  141. -- 此处要了解笛卡儿积
  142. -- select DISTINCT s1.course_id,s2.course_id,s1.num,s2.num from score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id;
  143. --
  144. -- 38、查询没学过“李平老师”老师讲授的任一门课程的学生姓名;
  145. #找出选过李平老师的,然后在学生表 not in
  146. -- select sid,sname from student where sid not in (select student_id from score where course_id in (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname='李平老师') group by student_id)

习题题目及答案

  1. ##还可以选择查询语句,不过这个语句结果需要为一个常量 select age,name,(select count(1) from tb) from tb1; 如果不是常量,需要设定条件,否则会变为笛卡儿积了。如下所示
  2. # -- 17、按平均成绩从低到高显示所有学生的“生物”、“物理”、“体育”三门的课程成绩,按如下形式显示: 学生ID,生物,物理,体育,有效课程数,有效平均分;
  3. # -- SELECT
  4. # -- student_id,
  5. # -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 1) as 语文,
  6. # -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 2) as 数学,
  7. # -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 3) as 英语
  8. # -- from score as s1;
  9.  
  10. '''
  11. select id,(select * from tb1 where tb1.id = 1) from tb2;
  12. 此时 子查询类似一个常量, 每一行就是 id1 常量, id2 常量这样子
  13. '''
  14. '''
  15. #?????? -- 21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
  16. # select * from
  17. # (
  18. # select
  19. # student_id,
  20. # course_id,
  21. # num,
  22. # 1,
  23. # (select num from score as s2 where s2.course_id = s1.course_id group by s2.num order by s2.num desc limit 0,1),
  24. # (select num from score as s2 where s2.course_id = s1.course_id group by s2.num order by s2.num desc limit 3,1) as cc
  25. #
  26. # from score as s1
  27. # ) as B
  28. # where B.num > B.cc order by B.course_id desc;
  29. '''
  30. #重点 s2.student_id=s1.student_id
  31. # SELECT
  32. # student_id,
  33. # (select num from score as s2 where s2.student_id=s1.student_id and course_id = 1) as 语文,
  34. # (select num from score as s2 where s2.student_id=s1.student_id and course_id = 2) as 数学,
  35. # (select num from score as s2 where s2.student_id=s1.student_id and course_id = 3) as 英语
  36. # from score as s1;
  37.  
  38. # case then 条件 else 字段 END
  39. #select course_id,max(num),min(num),cname,
  40. # case when min(num) < 10 THEN 0 else min(num) end from score left join course on score.course_id = course.cid group by course_id;
  41.  
  42. #-- 19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
  43. # select course_id,avg(num),sum(case when num<60 then 0 else 1 end),sum(1),sum(case when num<60 then 0 else 1 end)/sum(1) from score group by course_id;
  44.  
  45. #-- 20、课程平均分从高到低显示(显示任课老师);
  46. # select course_id,cname,tname,avg(if(isnull(score.num),0,score.num)) from score
  47. # left join course on score.course_id = course.cid
  48. # left join teacher on course.teacher_id = teacher.tid
  49. # group by course_id order by avg(num) desc;
  50.  
  51. #if(isnull(score.num),0,score.num) 如果 score.num是空,那么就是0,否则是它本身
  52.  
  53. # -- 34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
  54. # -- 此处要了解笛卡儿积,连接两张表不加 on 条件,会进行笛卡儿积,就是一张表的首行遍历连接另一张表所有行,然后第二行继续遍历连接.....
  55. # -- select DISTINCT s1.course_id,s2.course_id,s1.num,s2.num from score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id;

答题中几道不会的题目

sql习题及答案的更多相关文章

  1. web实验指导书和课后习题参考答案

    实验指导书 :http://course.baidu.com/view/daf55bd026fff705cc170add.html 课后习题参考答案:http://wenku.baidu.com/li ...

  2. Python编程快速上手-让繁琐工作自动化-第二章习题及其答案

    Python编程快速上手-让繁琐工作自动化-第二章习题及其答案 1.布尔数据类型的两个值是什么?如何拼写? 答:True和False,使用大写的T和大写的F,其他字母是小写. 2.3个布尔操作符是什么 ...

  3. 珍藏的数据库SQL基础练习题答案

    自己珍藏的数据库SQL基础练习题答案 一,基本表的定义与删除. 题1: 用SQL语句创建如下三张表:学生(Student),课程表(Course),和学生选课表(SC),这三张表的结构如表1-1到表1 ...

  4. 50道sql练习题和答案

    最近两年的工作没有写过多少SQL,感觉水平下降十分严重,网上找了50道练习题学习和复习 原文地址:50道SQL练习题及答案与详细分析 1.0数据表介绍 --1.学生表 Student(SId,Snam ...

  5. sql查询作业答案

    sql查询作业答案   阅读目录 一 题目 二 答案 一 题目 1.查询所有的课程的名称以及对应的任课老师姓名 2.查询学生表中男女生各有多少人 3.查询物理成绩等于100的学生的姓名 4.查询平均成 ...

  6. 50道SQL练习题及答案与详细分析(MySQL)

    50道SQL练习题及答案与详细分析(MySQL) 网上的经典50到SQL题,经过一阵子的半抄带做,基于个人理解使用MySQL重新完成一遍,感觉个人比较喜欢用join,联合查询较少 希望与大家一起学习研 ...

  7. 《python编程:从入门到实践》课后习题及答案

    转载: <Python编程:从入门到实践>课后习题及答案-码农之家 (xz577.com) <Python编程:从入门到实践>课后习题及答案 - 信德维拉 - 博客园 (cnb ...

  8. sql经典习题及其答案(纠正错误版)

    --网上有好多这套题的答案,但是经过我的验证,有很多都是错的,误人子弟--这是我自己纠正以后的版本 然后呢如果我写的还有不对的欢迎批评指正!--(1)查询2006年以后(包括2006年)的投稿情况,列 ...

  9. 统计建模与R软件习题二答案

    # 习题2 # 2.1 x=c(1,2,3) y=c(4,5,6) e=c(rep(1,3)) z=2*x+y+e;z x%*%y # 若x,y如答案那样定义为矩阵,则不能用%*%,因为,维数不对应, ...

随机推荐

  1. 湘潭邀请赛+蓝桥国赛总结暨ACM退役总结

    湘潭邀请赛已经过去三个星期,蓝桥也在上个星期结束,今天也是时候写一下总结了,这应该也是我的退役总结了~ --------------------------------湘潭邀请赛----------- ...

  2. ELK学习笔记之logstash安装logstash-filter-multiline(在线离线安装)

    0x00 概述 ELK-logstash在搬运日志的时候会出现多行日志,普通的搬运会造成保存到ES中单条单条,很丑,而且不方便读取,logstash-filter-multiline可以解决该问题 g ...

  3. Django多表查询

    一.前言 1.什么是ORM? ORM,即Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,这样,我们在具体的操作业务对象的时候, ...

  4. 微信小程序复选框实现 多选一功能

    功能实现界面 data: { checkboxItems: [ { name: '全天(1-8节)', value: 'allday' }, { name: '上午(1-4节)', value: 'a ...

  5. Table组件设置文字超出宽度显示省略号,鼠标悬停以悬浮框显示

    一.设置文字超出宽度显示省略号 注意点: 1.  需要指定column的width属性,否则列头跟内容可能不对齐.需要留一列不设置宽度以适应弹性布局. 2. 列宽度width必须大于ellipsis的 ...

  6. docker run -v参数

    挂载目录(直接给例子吧) -v=[]:绑定挂载目录 宿主机绑定: -v<host>:<container>:[rw|ro] 在Docker中新建一个共享的卷: -v /< ...

  7. H5外包团队:使用HTML5播放短视频代码分享

    滑动代码 /** * 滑动处理 */ function Touch() { this.init(); } Touch.fn = Touch.prototype; Touch.fn.init = fun ...

  8. vs code 格式化vue代码

    1.安装 vetur 2.文件-首选项-设置 增加 "vetur.format.defaultFormatter.html": "js-beautify-html&quo ...

  9. c++ std::advance

    // advance example #include <iostream> // std::cout #include <iterator> // std::advance ...

  10. Ansible 脚本运行一次后,再次运行时出现报错情况,原因:ansible script 的格式不对,应改成Unix编码

    Ansible 脚本运行一次后,再次运行时出现报错情况,原因:ansible  script 的格式不对,应改成Unix编码 find . -name "*" | xargs do ...