-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuiz-1.sql
133 lines (118 loc) · 3.15 KB
/
Quiz-1.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*Andy Alvarenga*/
/*1*/
create table Classes(
class varchar(32) PRIMARY KEY,
type varchar(2),
country varchar(32),
numGuns integer,
bore integer,
displacement integer
);
create table Ships(
name varchar(32),
class varchar(32),
launched integer
);
create table Battles(
name varchar(32),
date text
);
create table Outcomes(
ship varchar(32),
battle varchar(32),
result varchar(32)
);
/*2*/
insert into Classes (class, type, country, numGuns, bore, displacement)
values ('Bismarck', 'bb', 'Germany', 8, 15, 42000),
('Iowa', 'bb', 'USA', 9, 16, 46000),
('Kongo', 'bc', 'Japan', 8, 14, 32000),
('North Carolina', 'bb', 'USA', 9, 16, 37000),
('Renown', 'bc', 'Britain',6, 15, 32000),
('Revenge', 'bb', 'Britain', 8, 15, 29000),
('Tennessee', 'bb', 'USA',12,14,32000),
('Yamato', 'bb', 'Japan', 9, 18, 65000);
insert into Ships (name, class, launched)
values ('California', 'Tennessee', 1915),
('Haruna', 'Kongo', 1915),
('Hiei', 'Kongo', 1915),
('Iowa', 'Iowa', 1933),
('Kirishima', 'Kongo', 1915),
('Kongo', 'Kongo', 1913),
('Missouri', 'Iowa', 1935),
('Musashi', 'Yamato', 1942),
('New Jersey', 'Iowa', 1936),
('North Carolina', 'North Carolina',1941),
('Ramillies', 'Revenge',1917),
('Renown', 'Renown', 1916),
('Repulse', 'Renown', 1916),
('Resolution', 'Revenge', 1916),
('Revenge', 'Revenge', 1916),
('Royal Oak', 'Revenge', 1916),
('Royal Sovereign', 'Revenge', 1916),
('Tennessee', 'Tennessee', 1915),
('Washington', 'North Carolina', 1941),
('Wisconsin', 'Iowa', 1940),
('Yamato', 'Yamato', 1941);
insert into Battles (name, date)
values ('Denmark Strait', '05-24-41'),
('Guadalcanal', '11-15-42'),
('North Cape', '12-26-43'),
('Surigao Strait', '10-25-44');
insert into Outcomes (ship, battle, result)
values ('California', 'Surigao Strait', 'ok'),
('Kirishima', 'Guadalcanal', 'sunk'),
('Resolution', 'Denmark Strait', 'ok'),
('Wisconsin', 'Guadalcanal', 'damaged'),
('Tennessee', 'Surigao Strait', 'ok'),
('Washington', 'Guadalcanal', 'ok'),
('New Jersey', 'Surigao Strait', 'ok'),
('Yamato', 'Surigao Strait', 'sunk'),
('Wisconsin', 'Surigao Strait', 'damaged');
/*3*/
select class, country
from Classes
group by class
having bore >= 15;
/*4*/
select name, launched
from Ships
group by name
having launched < 1918;
/*5*/
select ship, result
from Outcomes
where battle = 'Surigao Strait';
/*6*/
select name
from Ships
where class IN (
select class
from Classes
where displacement >= 40000
);
/*7*/
select ship, displacement, numGuns
from Outcomes, Classes
where battle = 'Surigao Strait' AND class IN(
select class
from Ships
where name = ship);
/*8*/
select name
from Ships;
/*9*/
select class
from Ships
group by class
having count(class) = 2;
/*10*/
select country
from Classes
group by country
having count(country) = 2;
/*11*/
select ship, result
from Outcomes
group by ship, result
having count(*) > 1;