-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject1.sql
214 lines (184 loc) · 5.05 KB
/
project1.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
SELECT name
FROM Pokemon
WHERE type = 'Grass'
ORDER BY name;
SELECT name
FROM Trainer
WHERE hometown = 'Brown City'
OR hometown = 'Rainbow City'
ORDER BY name;
SELECT DISTINCT type
FROM Pokemon
ORDER BY type;
SELECT name
FROM City
WHERE name LIKE 'B%'
ORDER BY name;
SELECT hometown
FROM Trainer
WHERE name NOT LIKE 'M%'
ORDER BY hometown;
SELECT nickname
FROM CatchedPokemon
WHERE level = (SELECT MAX(level)
FROM CatchedPokemon)
ORDER BY nickname;
SELECT name
FROM Pokemon
WHERE name LIKE 'A%'
OR name LIKE 'E%'
OR name LIKE 'I%'
OR name LIKE 'O%'
OR name LIKE 'U%'
ORDER BY name;
SELECT AVG(level)
FROM CatchedPokemon;
SELECT MAX(cp.level)
FROM CatchedPokemon cp
JOIN Trainer t ON cp.owner_id = t.id
AND t.name = 'Yellow';
SELECT DISTINCT hometown
FROM Trainer
ORDER BY hometown;
SELECT t.name, cp.nickname
FROM Trainer t
JOIN CatchedPokemon cp ON t.id = cp.owner_id
AND cp.nickname LIKE 'A%'
ORDER BY t.name;
SELECT t.name
FROM Trainer t
JOIN Gym g ON t.id = g.leader_id
JOIN City c ON g.city = c.name
AND c.description = 'Amazon';
SELECT cp.owner_id, COUNT(cp.owner_id) AS cnt
FROM CatchedPokemon cp
JOIN Pokemon p ON cp.pid = p.id
AND p.type = 'Fire'
GROUP BY cp.owner_id
HAVING cnt = (SELECT MAX(cnt)
FROM (SELECT COUNT(cp.owner_id) AS cnt
FROM CatchedPokemon cp
JOIN Pokemon p ON cp.pid = p.id
AND p.type = 'Fire'
GROUP BY cp.owner_id) sub);
SELECT DISTINCT type
FROM Pokemon
WHERE id < 10
ORDER BY id DESC;
SELECT COUNT(*)
FROM Pokemon
WHERE NOT type = 'FIRE';
SELECT p.name
FROM Pokemon p
JOIN Evolution ev ON p.id = ev.before_id
AND ev.before_id > ev.after_id
ORDER BY p.name;
SELECT AVG(cp.level)
FROM CatchedPokemon cp
JOIN Pokemon p ON cp.pid = p.id
AND p.type = 'Water';
SELECT cp.nickname
FROM CatchedPokemon cp
JOIN Gym g ON cp.owner_id = g.leader_id
WHERE cp.level = (SELECT MAX(cp.level)
FROM CatchedPokemon cp, Gym g
WHERE cp.owner_id = g.leader_id);
SELECT t.name
FROM Trainer t
JOIN CatchedPokemon cp ON t.id = cp.owner_id
WHERE t.hometown = 'Blue City'
GROUP BY t.name
HAVING AVG(level) = (SELECT MAX(avglev)
FROM (SELECT AVG(level) AS avglev
FROM Trainer t
JOIN CatchedPokemon cp ON t.id = cp.owner_id
WHERE t.hometown = 'Blue City'
GROUP BY t.name) sub)
ORDER BY t.name;
SELECT p.name
FROM Pokemon p
JOIN CatchedPokemon cp ON p.id = cp.pid
WHERE p. id IN (SELECT before_id
FROM Evolution)
AND p.type = 'Electric'
AND cp.owner_id IN (SELECT id
FROM Trainer
GROUP BY hometown
HAVING COUNT(*) = 1);
SELECT t.name, SUM(cp.level) AS levelsum
FROM Trainer t
JOIN Gym g ON t.id = g.leader_id
JOIN CatchedPokemon cp ON g.leader_id = cp.owner_id
GROUP BY t.name
ORDER BY levelsum DESC;
SELECT hometown
FROM Trainer
GROUP BY hometown
HAVING COUNT(*) = (SELECT MAX(cnt)
FROM (SELECT hometown, COUNT(*) AS cnt
FROM Trainer
GROUP BY hometown) sub);
SELECT DISTINCT p.name
FROM Pokemon p
JOIN (SELECT cp.pid, COUNT(DISTINCT t.hometown) As cnt
FROM CatchedPokemon cp
JOIN Trainer t ON cp.owner_id = t.id
AND (t.hometown = 'Sangnok City'
OR t.hometown = 'Brown City')
GROUP BY cp.pid) sub ON p.id = sub.pid
AND sub.cnt = 2
ORDER BY p.name;
SELECT DISTINCT t.name
FROM Trainer t
JOIN CatchedPokemon cp ON t.id = cp.owner_id
JOIN Pokemon p ON cp.pid = p.id
AND p.name LIKE 'P%'
WHERE t.hometown = 'Sangnok City'
ORDER BY t.name;
SELECT t.name, p.name
FROM Trainer t
JOIN CatchedPokemon cp ON t.id = cp.owner_id
JOIN Pokemon p ON cp.pid = p.id
ORDER BY t.name, p.name;
SELECT p.name
FROM Pokemon p
JOIN Evolution ev ON p.id = ev.before_id
AND ev.before_id NOT IN (SELECT after_id
FROM Evolution)
AND ev.after_id NOT IN (SELECT before_id
FROM Evolution)
ORDER BY p.name;
SELECT cp.nickname
FROM CatchedPokemon cp
JOIN Gym g ON cp.owner_id = g.leader_id
AND g.city = 'Sangnok City'
JOIN Pokemon p ON cp.pid = p.id
AND p.type = 'Water'
ORDER BY nickname;
SELECT name
FROM Trainer
WHERE id IN (SELECT owner_id
FROM CatchedPokemon
WHERE pid IN (SELECT after_id
FROM Evolution)
GROUP BY owner_id
HAVING COUNT(*) >= 3)
ORDER BY name;
SELECT name
FROM Pokemon
WHERE id NOT IN (SELECT DISTINCT pid
FROM CatchedPokemon)
ORDER BY name;
SELECT MAX(cp.level) AS maxlev
FROM CatchedPokemon cp
JOIN Trainer t ON cp.owner_id = t.id
GROUP BY t.hometown
ORDER BY maxlev DESC;
SELECT p1.id, p1.name, p2.name, p3.name
FROM (SELECT ev1.before_id AS id1, ev1.after_id AS id2, ev2.after_id AS id3
FROM Evolution ev1, Evolution ev2
WHERE ev1.after_id = ev2.before_id) sub
JOIN Pokemon p1 ON sub.id1 = p1.id
JOIN Pokemon p2 ON sub.id2 = p2.id
JOIN Pokemon p3 ON sub.id3 = p3.id
ORDER BY p1.id;