cswork / libraryOpen practice workspace

Coding questions · Point72

Write SQL for top student per department

This question evaluates proficiency in SQL query formulation, including group-wise selection, joins, NULL handling, deterministic tie-breaking and ordering to identify a top-per-group student.

Schema:

Departments(dept_id PK, dept_name)
Students(student_id PK, student_name, dept_id FK nullable, gpa numeric(3,2) nullable, enrolled_at date)

Sample data:

Departments
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
| 1 | CS |
| 2 | Math |
| 3 | Physics |
| 4 | History |
+---------+-----------+
Students
+------------+--------------+---------+------+------------+
| student_id | student_name | dept_id | gpa | enrolled_at|
+------------+--------------+---------+------+------------+
| 101 | Alice | 1 | 3.90 | 2022-09-01 |
| 102 | Bob | 1 | 3.90 | 2023-01-15 |
| 103 | Cara | 2 | 3.85 | 2021-09-01 |
| 104 | Dan | 2 | NULL | 2024-02-01 |
| 105 | Eve | NULL | 3.70 | 2023-09-01 |
| 106 | Frank | 3 | 3.90 | 2020-09-01 |
+------------+--------------+---------+------+------------+

Write a single SQL query to list, for every department (including those with no students), the department name and the top student by GPA; break GPA ties by earliest enrolled_at, then by smallest student_id. If a department has no students with non-NULL GPA, return one row with student fields NULL. Output columns: dept_id, dept_name, student_id, student_name, gpa, enrolled_at. Show the expected result for the sample data.

Top student per department (including empty departments)

You are given two tables: Departments and Students. For every department (including departments with no students at all, or with only students whose GPA is NULL), return one row with the department and its top student by GPA. If multiple students in a department share the same highest GPA, break ties by earliest enrolled_at, then by smallest student_id. If a department has no students with a non-NULL GPA, return one row for that department with all student-related fields set to NULL.

Output columns: dept_id, dept_name, student_id, student_name, gpa, enrolled_at.

Table Departments

columntypeconstraints
dept_idINTPRIMARY KEY, NOT NULL
dept_nameVARCHAR(50)NOT NULL

Sample data:

dept_iddept_name
1CS
2Math
3Physics
4History

Table Students

columntypeconstraints
student_idINTPRIMARY KEY, NOT NULL
student_nameVARCHAR(100)NOT NULL
dept_idINTFOREIGN KEY REFERENCES Departments(dept_id)
gpaNUMERIC(3,2)
enrolled_atDATENOT NULL

Sample data:

dept_idenrolled_atgpastudent_idstudent_name
12022-09-013.9101Alice
12023-01-153.9102Bob
22021-09-013.85103Cara
22024-02-01NULL104Dan
NULL2023-09-013.7105Eve
32020-09-013.9106Frank

Expected output

One row per department, including departments with no students or no non-NULL GPAs, with the top student chosen by GPA (DESC), then earliest enrolled_at, then smallest student_id. If no qualifying student, student-related columns are NULL.

dept_iddept_nameenrolled_atgpastudent_idstudent_name
1CS2022-09-013.9101Alice
2Math2021-09-013.85103Cara
3Physics2020-09-013.9106Frank
4HistoryNULLNULLNULLNULL

Hints

  1. Start from Departments and LEFT JOIN to a derived table of Students so that departments without qualifying students still appear.
  2. Use ROW_NUMBER() partitioned by dept_id and ordered by GPA DESC, enrolled_at ASC, then student_id ASC to pick the top student per department.