3.22 不使用unique结构,重写下面的where子句:
where unique(select title from course)
不使用unique的话,SQL应该如下:
where 1 >= (select count(title) from course)
3.23 考虑查询:
select course_id, semester, year, sec_id, avg(tot_cred)
from takes natural join student
where year = 2009
group by course_id, semester, year, sec_id
having count(ID) >= 2;
解释为什么在from子句中还加上与section的连接不会改变查询结果。
这个SQL查询查找的是“找出在2009年开课的课程段中选修人数达到2人以上的”。
因为takes中的主码已经包含了课程段中的主码信息,所以做不做连接已经没有必要。除非你需要section中主码外的其他属性信息。
3.24 考虑查询:
with dept_total(dept_name, value) as
(select dept_name, sum(salary)
from instructor
group by dept_name),
dept_total_avg(value) as
(select avg(value)
from dept_total)
sele