All problems

The A-Range List

mediumSQLFilteringSorting

Fairhaven University marks work on a 0-4 scale, and anything from 3.7 upward is recorded as a distinction. The registrar is assembling the distinction register for the year — every result that reached that bar, listed as a result rather than as a student, because a student can earn several and each one is recorded separately.

A sitting still waiting on a mark has not earned a distinction. The register is read down from the best result, and two results on the same mark need a settled order so the register reads the same way every time it is produced.

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

The table already exists in the database — there is nothing to create or load.

Task: Write a query that returns student_id, course_id and grade for every sitting whose mark is 3.7 or better, the bar itself included. Best mark first; results sharing a mark are separated by student_id, smallest first, with course_id smallest first settling anything still level. Sittings with no mark entered never qualify.

Example output

Shape only — these sittings and marks are invented:

student_id course_id grade
12 7 3.95
19 8 3.88
12 9 3.76
23 11 3.71

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…