首页 > 数据库 >数据库系统概念第六版 第三章 1-10题

数据库系统概念第六版 第三章 1-10题

时间:2024-12-22 18:57:20浏览次数:7  
标签:10 join name company 第六版 数据库系统 where id select

在这里插入图片描述

在这里插入图片描述

// a.
select title from course where credits = 3 and dept_name = 'Comp.Sci'
// b. 我做的
select distinct ID from takes where course_id in (select tea.course_id from instructor as ins nature join teaches as tea where ins.name = 'Einstein' )
// 标准答案 使用了 using 简化连接
select distinct student.ID from (student join takes using(ID)) join (instructor join teaches using (ID)) using (course_id, sec_id, year) where instructor.name = 'Einstein'
// c. 不使用聚合
select salary from instructor where id not in (select ins1.id from instructor as ins1 join instructor as ins2 where ins1.salary < ins2.salary)
// 使用聚合 标准答案
select max(salary) from instructor
// d. 不使用聚合
select salary from instructor where id not in (select ins1.id from instructor as ins1 join instructor as ins2 where ins1.salary < ins2.salary)
// 使用聚合
select ID, name from instructor where salary in (select max(salary) from instructor)
// e.
select course_id, sec_id, count(ID) from takes join section using (course_id, sec_id, semester, year) where year = 2009 and semester = 'Fall' group by course_id, sec_id
// 或者
select course_id, sec_id, count(ID) from takes natural join section where year = 2009 and semester = 'Fall' group by course_id, sec_id
// f.
select max(maxCount) from (select count(ID) as maxCount from takes join section using (course_id, sec_id, semester, year) where year = 2009 and semester = 'Fall' group by course_id, sec_id) as T
// g.
with sec_enrollment as (select course_id, sec_id, count(ID) as maxCount from takes natural join section where year = 2009 and semester = 'Fall' group by course_id, sec_id)
select course_id, sec_id from sec_enrollment where maxCount in (select max(maxCount) from sec_enrollment)

在这里插入图片描述

// a.
select sum(c.credits* g.points) from takes as t natural join course as c natural join grade_points as g where t.ID = 12345
// 当然,上面的查询方式在没查到时会返回空而不是0,所以做以下修正
select sum(c.credits* g.points) from takes as t natural join course as c natural join grade_points as g where t.ID = 12345 union select 0 from student where not exists(select * from student where takes.ID = 12345)
// b.
select sum(c.credits* g.points)/ sum(c.creaits) as GPA from takes as t natural join course as c natural join grade_points as g where t.ID = 12345 union select null as GPA from student where not exists(select * from student where takes.ID = 12345)
// c. 使用左连接 可以使未选课的学生置空
select student.ID, student.name sum(course.credits* grade_points.points)/ sum(course.creaits) as GPA from student natural left outer join takes natural join course natural join grade_points group by student.ID

在这里插入图片描述

// a. 
update instructor set salary = salary*1.10 where dept_name = 'Comp.Sci.'
// b. 
delete from course where course_id not in(select course_id from section)
// c.
insert into instructor select ID, name, dept_name, 10000 from student where tot_cred > 100

在这里插入图片描述
在这里插入图片描述

// a. 我做的答案
select count(distinct p.driver_id) from accident as a natural join participated as p where accident.date = '2009'
// 标准答案
select count(distinct driver_id) from accident, person, participated where accident.report_number = participated.report_number and person.driver_id = participated.driver_id and accident.date = '2009'
// b. 需要同时更新accident和participated两张表
insert into accident(report_number, date, location) values (2021, '2021-01-01', 'Changchun')
insert into participated
select o.driver id, c.license, 4007, 3000
from person p, owns o, car c
where p.name = ’Jones’ and p.driver id = o.driver id and
o.license = c.license and c.model = ’Toyota’
// c. 
delete from car where model = 'Mazda' and license in (select license from person natural join owns where name = 'John Smith')

在这里插入图片描述

// a. 
select ID , case
				when score < 40 then 'F'
				when score < 60 and score >= 40 then 'C'
				when score < 80 and score >= 60 then 'B'
				else 'A'
			end from marks
// b. 
with grades as(
	select ID, level = case 
						when score < 40 then 'F'
						when score < 60 and score >= 40 then 'C'
						when score < 80 and score >= 60 then 'B'
						else 'A'
					end from marks)
select count(ID), level from grades group by level

在这里插入图片描述

select name from myTable where lower(name) like '%sci%'

在这里插入图片描述

当r1和r2都不为空的时候

在这里插入图片描述

// a 我做的答案
select d.account_number, d.customer_name from depositor as d where not exists (select * from borrower as b where b.customer_name = d.customer_name)
// 标准答案
(select customer_name from depositor) except (select customer_name from borrower) 
// b.
select distinct c2.customer_name from customer as c1 join customer as c2  using (customer_street, customer_city) where c1.customer_name = 'Smith' 
// c.
select distinct branch_name from account natural join depositor natural join customer where customer_city = 'Harrison' 

在这里插入图片描述

// a.
select distinct employee_name, city from employee natural join works where company_name = 'First Bank Corporation'
// b.
select distinct employee_name, street, city from employee natural join works where company_name = 'First Bank Corporation' and salary > 10000
// c.
select distinct employee_name from employee where employee_name not in (select employee_name from where company_name = 'First Bank Corporation')
// d.
select distinct employee_name from works where salary > all(select salary from works where company_name = 'Small Bank Corporation')
// e. 我做的答案
select distinct company_name from company where city in (select city from company where company_name = 'Small Bank Corporation')
// 标准答案1
select c1.company_name from company as c1 where not exists(select city from company where company_name = 'Small Bank Corporation' except select city from company as c2 where c1.company_name = c2.company_name)
// 标准答案2
select c1.company_name from company as c1 where not exists (select city from company as c2 where c1.company_name = c2.company_name contains select city from company as c3 where c3.company_name = 'Small Bank Corporation')
// f.
select company_name from works group by company_name having count(employee_name) > all (select count(distinct employees_name) from works group by company_name)
// g.
select company_name from works group by company_name having avg(salary) > (select avg(salary) from works where company_name = 'First Bank Corporation')

在这里插入图片描述
在这里插入图片描述

// a.
update from employee set city = 'Newtown' where employee_name = 'Jones'
// b.
update from works set salary = 
(case
	when salary <= 100000 then salary* 1.10
	else salary*1.03) where company_name = 'First Bank Corporation' and employee_name in (select employee_name from managers)
									

标签:10,join,name,company,第六版,数据库系统,where,id,select
From: https://blog.csdn.net/weixin_59659714/article/details/144630217

相关文章

  • 数据库第六版第8章部分习题
    r1和r2交集是A,对A求闭包可以推出全集,故一定是无损分解。A->B、C->B、AC->Ba.Pk(student)→Pk(instructor)、Pk(instructor)→Pk(student)b.Pk(student)→Pk(instructor)A,BC,CD和E.先判断一个字母的,A和E很容易判断出是候选码,那么包括AE的......
  • CSC1001: Introduction to Computer Science
    CSC1001:IntroductiontoComputerScienceProgrammingMethodologyAssignment4Assignmentdescription:Thisassignmentwillbeworth10%ofthefinalgrade.Foreachquestion,writeyourcodeinthecorresponding.pyfile(i.e.,forquestion1,useq1.py).Once......
  • YOLOv11/10/8算法改进【NO.158】使用一种名为 PRepBN 的新方法,在训练过程中逐步用重新
      前  言    YOLO算法改进系列出到这,很多朋友问改进如何选择是最佳的,下面我就根据个人多年的写作发文章以及指导发文章的经验来看,按照优先顺序进行排序讲解YOLO算法改进方法的顺序选择。具体有需求的同学可以私信我沟通:首推,是将两种最新推出算法的模块进行融合形......
  • 【102. 二叉树的层序遍历 中等】
    题目:给你二叉树的根节点root,返回其节点值的层序遍历。(即逐层地,从左到右访问所有节点)。示例1:输入:root=[3,9,20,null,null,15,7]输出:[[3],[9,20],[15,7]]示例2:输入:root=[1]输出:[[1]]示例3:输入:root=[]输出:[]提示:树中节点数目在范围[0,2000]内-10......
  • 10. String、StringBuffer和StrIngBuilder的区别是什么?
    String是只读字符串,它并不是基本数据类型,而是一个对象。从底层源码来看是一个final类型的字符数组,所引用的字符串不能被改变,一经定义,无法再增删改。每次对String的操作都会生成新的String对象。privatefinalcharvalue[];每次+操作:隐式在堆上new一个跟原字符串相同的StringBui......
  • 题解:P11410 闪耀之塔
    题解:P11410闪耀之塔https://www.luogu.com.cn/problem/P11410我们要想讲讲前置知识——蒙哥马利快速幂模求逆元。前置知识逆元定义何为逆元?逆元,又称数论倒数。若整数\(a\)、\(b\)满足同余方程\(a*b=1(mod\n)\),那么\(a\),\(b\)互为模\(n\)意义下的逆元。前置\(......
  • java2实用教程第六版习题答案 第3章
    第3章一.判断题1.×(应该是true)  2.√ (表达式中最高精度为int,按int算) 3.√    4.× (应该是boolean型)  5.√    6.√    7.√   8.√ 9.×  (并不是必须的,也可以没有)  10.×二.单选1.A (算术混合运算的精度问题,B应为double类......
  • 从键盘上输入10个数,再输入一个数a, 找 a 在这10个数中第一次出现的位置。
     #include<stdio.h> intmain(){  inti,x,a[10];  printf("请输入10个整数:\n");  for(i=0;i<10;i++)    scanf("%d",&a[i]);  printf("请输入要寻找的数字:\n");  scanf("%d",&x);  i......
  • Windows10如何安装OpenSSL并生成证书
    1.安装第三方软件FireDaemonOpenSSL下载地址:适用于MicrosoftWindows的OpenSSL3.4、3.3、3.0LTS、1.1.1LTS二进制文件安装过程中注意勾选“添加到环境变量”。2.进入到安装目录如C:\ProgramFiles\FireDaemonOpenSSL3\bin,确保目录下存在openssl.exe文......
  • PHP 10个最具影响力的新功能
    无论您是经验丰富的专家还是刚刚踏入编程世界的初学者,2024年的PHP更新都将为您带来极大的帮助,优化您的代码,并提升开发效率。让我们一起探索10个最具影响力的新功能,它们将彻底改变您的PHP开发之旅!1、只读属性:只能在初始化时赋值,之后不可修改。class User {  pub......