Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Student Per Subject SQL

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Top Student Per Subject SQL. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Affirm's quantitative analytics team is reviewing subject-level assessment results for analysts supporting Affirm financing decisions. Write a PostgreSQL query that identifies the top student or students in each subject for the current exam term.

Requirements

  1. Restrict results to the 2026-Spring exam term and exclude records where marks is NULL.
  2. Rank students independently within each subject using their marks in descending order.
  3. Return every student tied for the highest mark, along with the subject name, student ID, student name, and mark.
  4. Sort the final results alphabetically by subject name and then by student ID.

Schema

students
ColumnTypeDescription
student_idPKINTUnique student identifier
student_nameVARCHAR(100)Student's full name
subjects
ColumnTypeDescription
subject_idPKINTUnique subject identifier
subject_nameVARCHAR(100)Subject name
exam_scores
ColumnTypeDescription
score_idPKINTUnique score record identifier
student_idINTReferences students.student_id
subject_idINTReferences subjects.subject_id
marksINTMarks earned in the assessment
exam_termVARCHAR(30)Assessment term
Tablesstudentssubjectsexam_scores
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results