All problems

Which Courses Are Hardest?

mediumSQLJoinsGroupBy

Students at Fairhaven University have been complaining that some courses are marked far harder than others, and the curriculum committee wants evidence before it decides whether anything needs changing. The measure it has agreed on is the mean mark each course awards.

The marks live on the sittings, which identify a course only by number, so the committee's report has to carry titles — the committee will not read a table of course ids. Sittings with no mark entered yet contribute nothing to a course's mean.

enrollments — one row per sitting: one student taking one course in one term. student_id points at a row in students and course_id at a row in courses. grade is the mark for that sitting, on the same 0-4 scale, and is NULL when no mark has been entered yet. semester is the term the sitting belongs to.

student_id course_id grade semester
1 1 3.7 Fall2023
1 2 3.9 Spring2024
2 1 3.2 Fall2023
2 3 3.5 Fall2023
3 2 4.0 Spring2024
3 4 3.8 Spring2024
4 3 2.9 Fall2023
5 1 3.6 Fall2023
5 2 NULL Spring2024

courses — one row per course the university runs. credits is how much the course counts towards a degree.

id title credits
1 Intro to Databases 3
2 Machine Learning 4
3 Linear Algebra 3
4 Data Visualization 2

Both tables already exist in the database — there is nothing to create or load.

Task: Write a query that returns one row per course that has at least one sitting on record, with the course's title and its mean mark rounded to two decimal places as avg_grade. Toughest course first — that is, lowest mean at the top — and if two courses share a mean, the one whose title comes first alphabetically goes above the other.

Example output

Shape only — these courses and means are invented:

title avg_grade
Cell Biology 2.4
Discrete Maths 2.95
Ethics in Data 3.4
Signal Processing 3.72

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…