select sno,sname,age
from (
select s.sno,s.sname,s.age,
count(t.cno)
over (partition by s.sno) as cnt,
count(distinct c.title) over() as total,
row_number() over
(partition by s.sno order by c.cno) as rn
from courses c
left join take t on (c.cno = t.cno)
left join student s on (t.sno = s.sno)
) x
where cnt = total
and rn = 1
3.7. Oracle
3.7.1. sql
select sno,sname,age
from (
select s.sno,s.sname,s.age,
count(t.cno)
over (partition by s.sno) as cnt,
count(distinct c.title) over() as total,
row_number() over
(partition by s.sno order by c.cno) as rn
from courses c, take t, student s
where c.cno = t.cno (+)
and t.sno = s.sno (+)
)
where cnt = total
and rn = 1
3.8. 外连接到COURSES表,而不是子查询
3.9. PostgreSQL
3.10. MySQL
3.11. 聚合函数COUNT找出选修所有课程的学生
3.11.1. sql
select s.sno,s.sname,s.age
from student s, take t
where s.sno = t.sno
group by s.sno,s.sname,s.age
having count(t.cno) = (select count(*) from courses)
3.11.2. 使用子查询返回课程总数
3.11.3. 外层查询负责筛选出选修课程数量等于子查询返回值的学生
4. 问题13
4.1. 比任何其他学生年龄都大的学生
4.2. sql
select *
from student
where age not in (select a.age
from student a, student b
where a.age < b.age)