boat-sailor 数据库SQL语言上机实验问题

【boat-sailor】

saliors(sid,sname,rating,age),其中sid表示水手号,sname表示水手姓名,rating表示水手级别,age水

手年龄;


boats(bid,bname,color) 其中bid表示船号,bname表示船名,color表示船的颜色;

reserves(sid,bid,day) 其中sid表示水手号,bid表示船号,day表示水手预定船的日期

1. 找出预订了103船的水手的名字
SELECT distinct sname
FROM saliors
WHERE sid IN(select sid from reserves where bid='103')

Dustin
Lubber
Horatio

2. 找出预订了红色船的所有水手的名字
SELECT distinct sname
FROM saliors
WHERE sid IN(select sid from reserves where bid in
  (select bid from boats where color='red'))

Dustin
Lubber
Horatio

3. 找出Lubber预定的船的所有颜色
select distinct color
from boats
where bid in(select bid from reserves where sid in
  (select sid from saliors where sname='Lubber'))

Red
Green
Red

4. 找出至少预订了一艘船的水手的名字
select distinct sname
from saliors
where exists(select sid from reserves)

或者select sname from sailors where sid in( select sid from reserves);

Andy
Art
Bob
Brutus
Dustin
Horatio
Lubber
Rusty
Zorba

5. 找出预订了一艘红色船或者绿色船的水手名
select distinct sname
from saliors
where sid in(select sid from reserves where bid in
 (select bid from boats where color='red' or color='green'))

Dustin
Horatio
Lubber

6. 找出同时预订了红色船和绿色船的水手名
select distinct sname
from saliors
where sid in(select sid from reserves where bid in
 (select bid from boats where color='red'))
intersect
select distinct sname
from saliors
where sid in(select sid from reserves where bid in
 (select bid from boats where color='green'))

Dustin
Horatio
Lubber

7. 找出预定了所有船的水手们的名字
select distinct sname
from saliors
where not exists(select * from boats where not exists
 (select * from reserves where sid=saliors.sid and bid=boats.bid))

Dustin

8. 找出预定了所有名为Interlake船的水手的名字
select distinct sname
from saliors
where not exists(select * from boats where bname='Interlake'and not exists
 (select * from reserves where sid=saliors.sid and bid=boats.bid))

Dustin
Horatio

9. 找出最老的水手的名字和年龄
select distinct sname,age
from saliors
where age>=ALL(select age from saliors)

Bob 63.5

10. 找出比等级为10的最老的水手的年龄还要大的水手的名字
select distinct sname
from saliors
where age>ALL(select age from saliors where rating='10')

Bob
Dustin
Lubber

11. 对于每个等级级别找出最年轻的水手的年龄
select min(age)
from saliors
group by rating

33.0
25.5
35.0
25.5
35.0
16.0

12. 对于至少有两个水手的等级级别,求出水手们的平均年龄
select avg(age)
from saliors
group by rating
having count(*)>=2

25.5
40.0
40.5
44.5

13. 对于至少有两个水手的等级,找出到了投票年龄(>18岁)的所有水手的平均年龄
select avg(s.age)
from saliors s
where s.age>18
group by s.rating
having 1<(select count(*)
  from saliors s2
  where s.rating=s2.rating)

44.5
40
40.5
35


14. 找出在所有的等级中水手的平均年龄是最低的那些等级
select s.rating
from saliors s
group by s.rating
having avg(s.age)<=all(select avg(s2.age)
   from saliors s2
   group by s2.rating)

10

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值