数据库第四次作业

该次作业是在课后习题的基础上,混合,修改,增加得到的题目

注意把2017改成2019

第一题

Consider the insurance database of Figure 3.17, where the primary keys are underlined. Construct the following SQL queries for this relational database.

1.1

Find the total number of people who owned cars that were involved in accidents in 2019.

select count(distinct driver_id)
from participates join accident using (report_number)
where date = "2019";

1.2

Find the number of accidents in which the cars belonging to “John Smith” were involved.

select count(report_number)
from participates
where driver_id in (
	select driver_id
	from person
	where name="John Smith");
	
	
select count(report_number)
from participates natural join person
where name="John Smith";

1.3

Add a new accident to the database; assume any values for required attributes.

insert into accident values("66666", "2019", "NewYork");

1.4

Update the damage amount for the car with the license number “AABB2000” in the accident with report number “AR2197” to $3000.

update participates
set damage_amount = 3000
where report_number="AR2197" and license="AABB2000";

1.5

Delete the Mazda belonging to “John Smith”.

delete from owns
where (owns.driver_id, owns.license) = (
	select driver_id, license
	from (person natural join owns) join car using (license)
	where name="John Smith" and model="Mazda");

第二题

Write the following queries in SQL, using the university schema.

2.1

Find the names of all students who have taken at least one Comp. Sci. course; make sure there are no duplicate names in the result.

select distinct name, ID
from (takes natural join student) join course using(course_id)
where course.dept_name="Comp. Sci."
group by name,ID
having count(*) > 1
order by name;

2.2

Find the IDs and names of all students who have not taken any course offering before Spring 2019.

select name, ID
from (takes natural join student) 
group by name, ID
having min(year)>2017;

2.3

For each department, find the maximum salary of instructors in that department. You may assume that every department has at least one instructor.

select dept_name, max(salary)
from instructor
group by dept_name;

2.4

Find the lowest, across all departments, of the per-department maximum salary computed by the preceding query

select dept_name, min(max_salary)
from (select dept_name, max(salary)
  	from instructor
  	group by dept_name)
  	as dept_salary(dept_name, max_salary);

相关推荐

  1. 数据库作业

    2024-04-07 01:54:01       21 阅读
  2. 数据库作业

    2024-04-07 01:54:01       11 阅读
  3. 数据库作业

    2024-04-07 01:54:01       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-07 01:54:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-07 01:54:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-07 01:54:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-07 01:54:01       18 阅读

热门阅读

  1. linux命令大全(涵盖所有命令)

    2024-04-07 01:54:01       17 阅读
  2. ffplay用硬件进行编解码的命令的探索:

    2024-04-07 01:54:01       14 阅读
  3. 通过 ffmpeg命令行 调节视频播放速度

    2024-04-07 01:54:01       50 阅读
  4. Linux高级IO——多路转接之select

    2024-04-07 01:54:01       44 阅读
  5. 子集(迭代)(leetcode 78)

    2024-04-07 01:54:01       19 阅读
  6. 我的创作纪念日

    2024-04-07 01:54:01       20 阅读
  7. Redis7的10大应用场景和案例解析

    2024-04-07 01:54:01       40 阅读
  8. [深度学习]yolox训练参数含义

    2024-04-07 01:54:01       20 阅读
  9. LeetCode //C - 278. First Bad Version

    2024-04-07 01:54:01       16 阅读
  10. C++ vector

    2024-04-07 01:54:01       15 阅读
  11. UD浏览器多线程支持的设置

    2024-04-07 01:54:01       15 阅读
  12. vuex和pinia

    2024-04-07 01:54:01       19 阅读
  13. OpenJudge - 22:紧急措施

    2024-04-07 01:54:01       20 阅读