2-1
Create or replace view test2_01 as (select sid,name from pub.student) minus (select distinct sid,name from pub.student natural join pub.student_course)
2-2
Create or replace view test2_02 as
select distinct b.sid,b.name
from pub.student_course a,pub.student b
where a.sid = b.sid and a.sid<>200900130417and a.cid = some(
select cid
from pub.student_course c
where c.sid = 200900130417)
2-3
Create or replace view test2_03 as
select distinct b.sid,b.name
from pub.student_course a,pub.student b,pub.course c
where a.sid = b.sid and a.cid = c.cid and c.fcid = 300002
2-4
法1:Create or replace view test2_04 as
(select distinct b.sid,b.name
from pub.student_course a,pub.student b,pub.course c
where a.sid = b.sid and a.cid = c.cid and c.name='数据结构' )
intersect
(select distinct b.sid,b.name
from pub.student_course a,pub.student b,pub.course c
where a.sid = b.sid and a.cid = c.cid and c.name='操作系统' )
minus
(select distinct b.sid,b.name
from pub.student_course a,pub.student b,pub.course c
where a.sid = b.sid and a.cid = c.cid and c.name='程序设计语言' )
法2:嵌套子查询
Create or replace view test2_04 as
select distinct b.sid,b.name
from pub.student_course a,pub.student b,pub.course c
where a.sid = b.sid and a.cid = c.cid and c.name='数据结构' and b.sid in
(select distinct b.sid
from pub.course a,pub.student_course b
where a.cid = b.cid and a.name = '操作系统'
)
and b.sid not in(
select distinct b.sid
from pub.course a,pub.student_course b
where a.cid = b.cid and a.name='程序设计语言'
)
2-5
create or replace view test2_05 as
select a.sid,a.name,round(avg(score)) as avg_score,sum(score) as sum_score
from pub.student a,pub.student_course b
where a.sid = b.sid and a.age=20
group by a.sid, a.name
2-6
create or replace view test2_06 as
select c.cid,c.name,max(score) as max_score,(
select count(distinct s.sid)
from pub.student_course s
where s.cid = c.cid and s.score=(
select max(score)
from pub.student_course ss
where ss.cid = c.cid
group by c.cid
))
as max_score_count
from pub.course c,pub.student_course s
where c.cid = s.cid
group by c.cid,c.name
2-7
create or replace view test2_07 as
select sid,name
from pub.student
where name not like '张%' and name not like '李%' and name not like '王%'
2-8
create or replace view test2_08 as
select s_name as second_name,count(*) as p_count
from (
select substr(name,1,1) as s_name
from pub.student
)
group by s_name
2-9
create or replace view test2_09 as
select a.sid,a.name,b.score
from pub.student a,pub.student_course b
where a.sid = b.sid and b.cid = 300003
2-10
create or replace view test2_10 as
select distinct a.sid,a.name
from pub.student a,pub.student_course b,pub.course c
where a.sid = b.sid and b.cid = c.cid and b.score<60
group by a.sid,a.name,c.cid
having count(*)>=2