ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

数据库习题小结(一)

2020-12-31 22:04:19  阅读:1224  来源: 互联网

标签:数据库 course semester sec year 习题 小结 id select


使用大学模式,用SQL写出如下查询

a.找出Comp. Sci. 系开设的且有3个学分的课程名称。

select title

from course

where dept_name = 'Comp. Sci.' and credits=3;

 

 

 

 结果如上

 

b.找出名叫Einstein的教师所教的所有学生的标识,保证结果中没有重复。

  1. with instor_einstein_teaches(course_id, sec_id, semester, year) as  //创建一个临时的表 里面包括了课程id,学期id,学期,年份 (
  2.   select course_id, sec_id, semester, year from instructor natural join teaches where name = 'Einstein' //将 instruction teacher 自然连接 然后找einstein老师
  3.   ),
  4.   takes_einstein_teaches(ID) as (
  5.   select ID from takes natural join instor_einstein_teaches //将上表的ID给筛出来
  6.   )
  7.   select distinct ID, name from student natural join takes_einstein_teaches;

 输出结果为:

 

 

 (不知道这个为啥跑不出来?挺奇怪的)

c.找出教师的最高工资。

 1.select max(salary) from instructor; //这个就不用多说了 需要注意的是 : max()是函数 不要写成salary(max)

2.select salary 嵌套子查询的方法 FROM instructor WHERE salary>=ALL (select salary FROM instructor );

d.找出工资最高的所有教师(可能有不止一位教师具有相同的工资)

select name,ID FROM instructor WHERE salary>=ALL (select salary FROM instructor );

 

 

e.找出2009年秋季开设的每个课程段的选课人数。

 

    1. with candidate_section (course_id,sec_id, semester, year) as (
    2.   select course_id, sec_id, semester, year from section where semester='Fall' and year=2009
    3.   ),
    4.   section_count(course_id, sec_id, semester, year, student_count) as (
    5.   select course_id, sec_id, semester, year, count(distinct ID) as student_num from takes natural join candidate_section // count(distinct id)是核心,可以唯一的计数
    6.   group by course_id, sec_id, semester, year order by student_num
    7.   )
    8.   select max(student_count) from section_count;

标签:数据库,course,semester,sec,year,习题,小结,id,select
来源: https://www.cnblogs.com/Cxiaoyuan/p/14218742.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有