Sailors(sid:integer, sname:string, rating:integer, age:real)
Boats(bid:integer, bname:string, color:string)
Reserves(sid:integer, bid:integer, day:date)
Sailors(水手表)
sid编号 same名字 rating等级 age年龄
22 Dustin 7 45.0
29 Brutus 1 33.0
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35.0
64 Horatio 7 35.0
71 Zorba 10 16.0
74 Horatio 9 35.0
85 Art 3 25.5
95 Bob 3 63.5
Reserves(预订情况表)
sid水手编号bid船只编号day日期
22 101 10/10/98
22 102 10/10/98
22 103 10/8/98
22 104 10/7/98
31 102 11/10/98
31 103 11/6/98
31 104 11/12/98
64 101 9/5/98
64 102 9/8/98
74 103 9/8/98
Boats(船只表)
bid编号 bname名字 color颜色
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red1.找出预订了103船的水手的名字
2.找出预订了红色船的所有水手的名字
3.找出Lubber预定的船的所有颜色
4.找出至少预订了一艘船的水手的名字
5.找出预订了一艘红色船或者绿色船的水手名
6.找出同时预订了红色船和绿色船的水手名
7.找出预定了所有船的水手们的名字
8.找出预定了所有名为Interlake船的水手的名字
9.找出最老的水手的名字和年龄
10.找出比等级为10的最老的水手的年龄还要大的水手的名字
11.对于每个等级级别找出最年轻的水手的年龄
12.对于至少有两个水手的等级级别,求出水手们的平均年龄
13.对于至少有两个水手的等级,找出到了投票年龄(至少有18岁)的所有水手的平均年龄
14.找出在所有的等级中水手的平均年龄是最低的那些等级
15.插入95号水手,于04-03-24预定103号船的信息
16.将101号船颜色改为‘黄色’
17.从水手表中删除95号水手
Boats(bid:integer, bname:string, color:string)
Reserves(sid:integer, bid:integer, day:date)
Sailors(水手表)
sid编号 same名字 rating等级 age年龄
22 Dustin 7 45.0
29 Brutus 1 33.0
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35.0
64 Horatio 7 35.0
71 Zorba 10 16.0
74 Horatio 9 35.0
85 Art 3 25.5
95 Bob 3 63.5
Reserves(预订情况表)
sid水手编号bid船只编号day日期
22 101 10/10/98
22 102 10/10/98
22 103 10/8/98
22 104 10/7/98
31 102 11/10/98
31 103 11/6/98
31 104 11/12/98
64 101 9/5/98
64 102 9/8/98
74 103 9/8/98
Boats(船只表)
bid编号 bname名字 color颜色
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red1.找出预订了103船的水手的名字
2.找出预订了红色船的所有水手的名字
3.找出Lubber预定的船的所有颜色
4.找出至少预订了一艘船的水手的名字
5.找出预订了一艘红色船或者绿色船的水手名
6.找出同时预订了红色船和绿色船的水手名
7.找出预定了所有船的水手们的名字
8.找出预定了所有名为Interlake船的水手的名字
9.找出最老的水手的名字和年龄
10.找出比等级为10的最老的水手的年龄还要大的水手的名字
11.对于每个等级级别找出最年轻的水手的年龄
12.对于至少有两个水手的等级级别,求出水手们的平均年龄
13.对于至少有两个水手的等级,找出到了投票年龄(至少有18岁)的所有水手的平均年龄
14.找出在所有的等级中水手的平均年龄是最低的那些等级
15.插入95号水手,于04-03-24预定103号船的信息
16.将101号船颜色改为‘黄色’
17.从水手表中删除95号水手
SELECT rating,
Avg(age) avgage
FROM sailors
GROUP BY rating
HAVING Count(same) >= 2
--13\
SELECT rating,
Avg(age) avgage
FROM sailors
WHERE age >= 18
AND rating IN (SELECT rating
FROM sailors
GROUP BY rating
HAVING Count(same) >= 2)
GROUP BY rating
--14\
select TOP(1) rating,AVG(age) avgage from Sailors group by rating ORDER BY AVG(age) asc
64 Horatio 7 35.074 Horatio 9 35.0
Create TABLE Sailors (sid int,same varchar(50),rating int,age numeric(32,2))
INSERT INTO Sailors select '22', 'Dustin', '7', '45.0'
union all select '29', 'Brutus', '1', '33.0'
union all select '31', 'Lubber', '8', '55.5'
union all select '32', 'Andy', '8', '25.5'
union all select '58', 'Rusty', '10', '35.0'
union all select '64', 'Horatio', '7', '35.0'
union all select '71', 'Zorba', '10', '16.0'
union all select '74', 'Horatio', '9', '35.0'
union all select '85', 'Art', '3', '25.5'
union all select '95', 'Bob', '3', '63.5' --方案
--1.对于至少有两个水手的等级级别,求出水手们的平均年龄
select rating,AVG(age) avgage from Sailors group by rating having COUNT(same) >= 2
/*rating avgage
----------- ---------------------------------------
3 44.500000
7 40.000000
8 40.500000
10 25.500000(4 行受影响)*/
--2.对于至少有两个水手的等级,找出到了投票年龄(至少有18岁)的所有水手的平均年龄
select rating,AVG(age) avgage from Sailors
where rating in(select rating from Sailors group by rating having COUNT(same) >= 2)
and age >= 18 group by rating
/*
rating avgage
----------- ---------------------------------------
3 44.500000
7 40.000000
8 40.500000
10 35.000000(4 行受影响)
*/
--3.找出在所有的等级中水手的平均年龄是最低的那些等级
select TOP(1) rating,AVG(age) avgage from Sailors group by rating ORDER BY AVG(age)
/*rating avgage
----------- ---------------------------------------
10 25.500000(1 行受影响)*/