Ctr

Calculate Median

You're a backend engineer working on an online education platform similar to Khan Academy, called "Con Academy". You're still writing the software that grades on a curve that we started in Curving Grades 1. The grades are in the grades table, and classes are in the classes table.


classes
column_name type reference
id
VARCHAR
NULL
subject
VARCHAR
NULL
name
VARCHAR
NULL
semester
VARCHAR
NULL
teacher_id
VARCHAR
teachers.id

grades
column_name type reference
score
NUMERIC
NULL
student_id
VARCHAR
students.id
class_id
VARCHAR
classes.id

You are building a dashboard for the teachers, and the dashboard will display the median grade score for the classes. The median is the "value in the middle" when all grade score values are sorted sequentially. The "middle" is unambiguous when the number of scores is an odd number. If the number of scores is an even number, and hence there is no clear "middle" value, then the arithmetic mean of the 2 middle values is used.

Select the ID for each class and the median grade score. Sort them in descending order by median. Do this without using the percentile_cont() function! This is an example of a set-aggregate function which we'll cover later. Instead, use your knowledge of window functions to calculate it manually. No, there is no median() function or anything like that.

© 2022 Andrew Carlson. All rights reserved.

class_id
VARCHAR
median_score
NUMERIC
1
271482503
79.8100000000000000
2
857462036
71.7300000000000000
3
491953712
70.8050000000000000
4
637558000
70.6200000000000000
5
342314241
70.1300000000000000
6
561867345
60.5550000000000000
7
445566904
59.7400000000000000
8
457480601
59.6400000000000000
9
194826310
58.6000000000000000
10
500841817
56.5400000000000000
11
245890830
45.9150000000000000
12
248796923
45.9100000000000000
13
619597788
44.3100000000000000
14
862041021
44.0800000000000000
15
893899942
41.3750000000000000
16
823400370
39.9350000000000000
17
783358585
38.7300000000000000
18
423435550
38.1600000000000000
19
795599327
36.7700000000000000
20
166532862
36.6450000000000000
20 rows

You haven't solved this challenge yet!
Are you sure you want to reveal the answer?