-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_queries.sql
More file actions
266 lines (237 loc) · 6.16 KB
/
sql_queries.sql
File metadata and controls
266 lines (237 loc) · 6.16 KB
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
-- SQL CLEANING/CREATING THE ENVIRONMENT
-- Updating percent columns to remove "%" and cast as floats so I can work with them as numeric data
UPDATE comma_check_9
SET percentage = REPLACE(percentage, '%', '');
ALTER TABLE comma_check_9
MODIFY COLUMN percentage FLOAT;
UPDATE comma_test_9
SET test_percent = REPLACE(test_percent, '%', '');
ALTER TABLE comma_test_9
MODIFY COLUMN test_percent FLOAT;
-- Creating joint table for demographic data and check/test overall score data for students who completed both the check and the test
CREATE VIEW all_scores AS (
SELECT
comma_check_9.student_id,
comma_check_9.period,
score AS check_score,
percentage AS check_percent,
-- creating a field with the letter grade
CASE
WHEN percentage >=90 THEN "A"
WHEN percentage >=80 THEN "B"
WHEN percentage >=70 THEN "C"
WHEN percentage >=60 THEN "D"
ELSE "F"
END AS check_grade,
test_score,
test_percent,
CASE
WHEN test_percent >=90 THEN "A"
WHEN test_percent >=80 THEN "B"
WHEN test_percent >=70 THEN "C"
WHEN test_percent >=60 THEN "D"
ELSE "F"
END AS test_grade,
-- calculating the growth/regression from the check to the test
round(test_percent - percentage, 2) AS score_change,
gender,
absence_s1_rank AS absence_s1,
absence_q3_rank AS absence_q3,
support,
support_class,
most_impact_status AS impact,
impact_with_tag AS impact_tag
FROM comma_check_9
JOIN comma_test_9 USING(student_id)
JOIN demographics USING(student_id)
);
-- Creating joint table for demographic data, check/test overall score data, and aggregated scores per comma rule for check/test for students who completed both the check and the test online. (No rule-specific data for students who completed the test on paper.)
CREATE VIEW all_rules AS (
SELECT
all_scores.student_id,
test_percent,
test_grade,
weakness_test,
weakness_tied_test,
fanboys_test_all AS fanboys,
intro_test_all AS intro,
quote_test_all AS quote,
list_test_all AS list,
date_address_test_all AS date_address,
adjective_test_all AS adjective,
extra_test_all AS extra
FROM all_scores
JOIN rule_test_9 USING(student_id)
);
-- QUESTION #1: What were the average scores for the check-in and test? What was the typical improvement from the check-in to the test?
SELECT
count(student_id) AS total,
round(avg(check_percent), 2) AS check_avg_perc,
round(avg(test_percent), 2) AS test_avg_perc,
round(avg(score_change), 2) AS growth
FROM all_scores;
WITH check_count AS (
SELECT
check_grade AS grade,
count(check_grade) AS check_count
FROM
all_scores
GROUP BY
check_grade
ORDER BY
grade
),
test_count AS (
SELECT
test_grade AS grade,
count(test_grade) AS test_count
FROM
all_scores
GROUP BY
test_grade
ORDER BY
grade
)
SELECT
test_count.grade,
check_count,
test_count
FROM
check_count
JOIN test_count ON check_count.grade=test_count.grade;
-- QUESTION #2: How did the scores/growth vary by period and demographic factors (e.g., gender, enrollment in support class, attendance, etc.)?
SELECT
period,
count(student_id) AS total,
round(avg(check_percent), 2) AS check_avg_perc,
round(avg(test_percent), 2) AS test_avg_perc,
round(avg(score_change), 2) AS growth
FROM all_scores
GROUP BY
period
ORDER BY
period;
SELECT
gender,
count(student_id) AS total,
round(avg(check_percent), 2) AS check_avg_perc,
round(avg(test_percent), 2) AS test_avg_perc,
round(avg(score_change), 2) AS growth
FROM all_scores
GROUP BY
gender;
SELECT
absence_q3,
count(student_id) AS total,
round(avg(check_percent), 2) AS check_avg_perc,
round(avg(test_percent), 2) AS test_avg_perc,
round(avg(score_change), 2) AS growth
FROM all_scores
GROUP BY
absence_q3
ORDER BY
test_avg_perc DESC;
SELECT
absence_s1,
count(student_id) AS total,
round(avg(check_percent), 2) AS check_avg_perc,
round(avg(test_percent), 2) AS test_avg_perc,
round(avg(score_change), 2) AS growth
FROM all_scores
GROUP BY
absence_s1
ORDER BY
test_avg_perc DESC;
SELECT
support_class,
count(student_id) AS total,
round(avg(check_percent), 2) AS check_avg_perc,
round(avg(test_percent), 2) AS test_avg_perc,
round(avg(score_change), 2) AS growth
FROM all_scores
GROUP BY
support_class
ORDER BY
test_avg_perc DESC;
SELECT
impact,
count(student_id) AS total,
round(avg(check_percent), 2) AS check_avg_perc,
round(avg(test_percent), 2) AS test_avg_perc,
round(avg(score_change), 2) AS growth
FROM all_scores
GROUP BY
impact
ORDER BY
test_avg_perc DESC;
SELECT
impact_tag,
count(student_id) AS total,
round(avg(check_percent), 2) AS check_avg_perc,
round(avg(test_percent), 2) AS test_avg_perc,
round(avg(score_change), 2) AS growth
FROM all_scores
GROUP BY
impact_tag
ORDER BY
test_avg_perc DESC;
-- QUESTION #3: Which rule did students struggle with most on the test?
-- Note: This coding only includes students who took the test digitally (n=65) rather than on paper (n=30) since I don't have the weakness data for paper copies
WITH weakness AS (
SELECT
weakness_test,
count(weakness_test) AS weakness_count
FROM
all_rules
-- Excluding students who had no weaknesses (i.e., got 100%)
WHERE
weakness_test <> ""
GROUP BY
weakness_test
),
weakness_2 AS (
SELECT
weakness_tied_test,
count(weakness_tied_test) AS weakness_2_count
FROM
all_rules
-- Excluding students who didn't tie for weakness
WHERE
weakness_tied_test <> ""
GROUP BY
weakness_tied_test
)
SELECT
weakness_test AS all_weakness,
-- Use coalesce to address the NULL values resulting from left joining the tied weakness to weakness
weakness_count + coalesce(weakness_2_count, 0) AS all_weakness_count
FROM
weakness
LEFT JOIN weakness_2 ON weakness.weakness_test=weakness_2.weakness_tied_test
ORDER BY
all_weakness_count DESC;
-- QUESTION #4: How many students failed the test? Did they also fail the check-in? Who are they?
SELECT
student_id,
check_percent,
test_percent
FROM
all_scores
WHERE
test_percent<60
ORDER BY
test_percent;
-- This query tells me the students names using data that is private for confidentiality reasons
SELECT
first_name,
last_name,
test_percent,
check_percent,
all_scores.student_id
FROM
all_scores
JOIN names ON all_scores.student_id=names.student_id
WHERE
test_percent<60
ORDER BY
test_percent;