One-to-one, one-to-many

One-to-one, one-to-many

You are an analyst at the Brightpath online school. There are three tables in the database: students — one row per student; student_profiles — a profile form, exactly one per student (a one-to-one relationship); enrollments — course sign-ups: a student may have several of them, or none at all (a one-to-many relationship). The curator needs a summary: which plan each student is on and how many courses they attend. Important: the profile form must not multiply a student's rows, and the absence of course sign-ups must not throw the student out of the report. Return the columns: - full_name — the student's name; - plan — the plan from the profile form; - courses_cnt — the number of course sign-ups (an integer; 0 if there are none). Sorting: courses_cnt descending, then full_name ascending. Students with no course sign-ups must be present in the result with the value 0.

Expected output sample

This is what a correct answer looks like — its row count is its own, it doesn't have to match the schema tables. Your answer must use the same column names as this sample — alias them with AS if needed.

full_nameplancourses_cnt
Chloe Davispremium3
Alice Brownpremium2
Emma Fisherfree2
PostgreSQLv16

Your query result will appear here

Focus radio
Paused · SomaFM · Fluid