-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLAggregation.sql
279 lines (210 loc) · 7.33 KB
/
SQLAggregation.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**************************************************************
AGGREGATION
Works for SQLite, MySQL
Postgres doesn't allow ambiguous Select columns in Group-by queries
**************************************************************/
/**************************************************************
Average GPA of all students
**************************************************************/
select avg(GPA)
from Student;
/**************************************************************
Lowest GPA of students applying to CS
**************************************************************/
select min(GPA)
from Student, Apply
where Student.sID = Apply.sID and major = 'CS';
/*** Average GPA of students applying to CS ***/
select avg(GPA)
from Student, Apply
where Student.sID = Apply.sID and major = 'CS';
/*** Fix incorrect counting of GPAs ***/
select avg(GPA)
from Student
where sID in (select sID from Apply where major = 'CS');
/**************************************************************
Number of colleges bigger than 15,000
**************************************************************/
select count(*)
from College
where enrollment > 15000;
/**************************************************************
Number of students applying to Cornell
**************************************************************/
select count(*)
from Apply
where cName = 'Cornell';
/*** Show why incorrect result, fix using Count Distinct ***/
select *
from Apply
where cName = 'Cornell';
select Count(Distinct sID)
from Apply
where cName = 'Cornell';
/**************************************************************
Students such that number of other students with same GPA is
equal to number of other students with same sizeHS
**************************************************************/
select *
from Student S1
where (select count(*) from Student S2
where S2.sID <> S1.sID and S2.GPA = S1.GPA) =
(select count(*) from Student S2
where S2.sID <> S1.sID and S2.sizeHS = S1.sizeHS);
/**************************************************************
Amount by which average GPA of students applying to CS
exceeds average of students not applying to CS
**************************************************************/
select CS.avgGPA - NonCS.avgGPA
from (select avg(GPA) as avgGPA from Student
where sID in (
select sID from Apply where major = 'CS')) as CS,
(select avg(GPA) as avgGPA from Student
where sID not in (
select sID from Apply where major = 'CS')) as NonCS;
/*** Same using subqueries in Select ***/
select (select avg(GPA) as avgGPA from Student
where sID in (
select sID from Apply where major = 'CS')) -
(select avg(GPA) as avgGPA from Student
where sID not in (
select sID from Apply where major = 'CS')) as d
from Student;
/*** Remove duplicates ***/
select distinct (select avg(GPA) as avgGPA from Student
where sID in (
select sID from Apply where major = 'CS')) -
(select avg(GPA) as avgGPA from Student
where sID not in (
select sID from Apply where major = 'CS')) as d
from Student;
/**************************************************************
Number of applications to each college
**************************************************************/
select cName, count(*)
from Apply
group by cName;
/*** First do query to picture grouping ***/
select *
from Apply
order by cName;
/*** Now back to query we want ***/
select cName, count(*)
from Apply
group by cName;
/**************************************************************
College enrollments by state
**************************************************************/
select state, sum(enrollment)
from College
group by state;
/**************************************************************
Minimum + maximum GPAs of applicants to each college & major
**************************************************************/
select cName, major, min(GPA), max(GPA)
from Student, Apply
where Student.sID = Apply.sID
group by cName, major;
/*** First do query to picture grouping ***/
select cName, major, GPA
from Student, Apply
where Student.sID = Apply.sID
order by cName, major;
/*** Now back to query we want ***/
select cName, major, min(GPA), max(GPA)
from Student, Apply
where Student.sID = Apply.sID
group by cName, major;
/*** Widest spread ***/
select max(mx-mn)
from (select cName, major, min(GPA) as mn, max(GPA) as mx
from Student, Apply
where Student.sID = Apply.sID
group by cName, major) M;
/**************************************************************
Number of colleges applied to by each student
**************************************************************/
select Student.sID, count(distinct cName)
from Student, Apply
where Student.sID = Apply.sID
group by Student.sID;
/*** First do query to picture grouping ***/
select Student.sID, cName
from Student, Apply
where Student.sID = Apply.sID
order by Student.sID;
/*** Now back to query we want ***/
select Student.sID, count(distinct cName)
from Student, Apply
where Student.sID = Apply.sID
group by Student.sID;
/*** Add student name ***/
select Student.sID, sName, count(distinct cName)
from Student, Apply
where Student.sID = Apply.sID
group by Student.sID;
/*** First do query to picture grouping ***/
select Student.sID, sName, cName
from Student, Apply
where Student.sID = Apply.sID
order by Student.sID;
/*** Now back to query we want ***/
select Student.sID, sName, count(distinct cName)
from Student, Apply
where Student.sID = Apply.sID
group by Student.sID;
/*** Add college (shouldn't work but does in some systems) ***/
select Student.sID, sName, count(distinct cName), cName
from Student, Apply
where Student.sID = Apply.sID
group by Student.sID;
/*** Back to query to picture grouping ***/
select Student.sID, sName, cName
from Student, Apply
where Student.sID = Apply.sID
order by Student.sID;
/**************************************************************
Number of colleges applied to by each student, including
0 for those who applied nowhere
**************************************************************/
select Student.sID, count(distinct cName)
from Student, Apply
where Student.sID = Apply.sID
group by Student.sID;
/*** Now add 0 counts ***/
select Student.sID, count(distinct cName)
from Student, Apply
where Student.sID = Apply.sID
group by Student.sID
union
select sID, 0
from Student
where sID not in (select sID from Apply);
/**************************************************************
Colleges with fewer than 5 applications
**************************************************************/
select cName
from Apply
group by cName
having count(*) < 5;
/*** Same query without Group-by or Having ***/
select cName
from Apply A1
where 5 > (select count(*) from Apply A2 where A2.cName = A1.cName);
/*** Remove duplicates ***/
select distinct cName
from Apply A1
where 5 > (select count(*) from Apply A2 where A2.cName = A1.cName);
/*** Back to original Group-by form, fewer than 5 applicants ***/
select cName
from Apply
group by cName
having count(distinct sID) < 5;
/**************************************************************
Majors whose applicant's maximum GPA is below the average
**************************************************************/
select major
from Student, Apply
where Student.sID = Apply.sID
group by major
having max(GPA) < (select avg(GPA) from Student);