All problems

Credits Attempted

mediumSQLJoinsGroupBy

The bursar at Fairhaven University bills students by credits attempted, not credits passed. A student who signs up for a four-credit course is billed for four credits from the moment the registration closes, however the term goes and whether or not a mark has been entered yet.

The bill therefore depends on three things held in three different places: which sittings a student has, what each of those courses is worth, and the student's name to put on the invoice.

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

students — one row per enrolled student. gpa is the grade point average already on file for that student, on a 0-4 scale.

id name major gpa
1 Nora Fischer Computer Science 3.8
2 Omar Haddad Statistics 3.4
3 Petra Novakova Computer Science 3.9
4 Quinn Walsh Mathematics 3.1
5 Ravi Shah Statistics 3.6

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

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

Task: Write a query that returns one row per student who has at least one sitting on record, with their name and the total credits they are being billed for as total_credits. Credits from a sitting with no mark entered are still billed. Largest bill first; if two students are billed the same, the one whose name comes first alphabetically goes above the other.

Example output

Shape only — these students and credit loads are invented:

name total_credits
Hugo Lam 18
Mira Sun 18
Dana Ruiz 15
Yara Blum 12
Tomas Vega 4

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…