Knowledge Guide
HomeDatabasesSQL Practice Problems

hard School Top Achievers by Subject

Problem Statement

Table: Student
Each row in this table represents a student's performance in a specific subject, including their unique ID, name, score, and the subject ID they were evaluated in.

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
| score       | int     |
| subjectId   | int     |
+-------------+---------+
id is the primary key for this table.
subjectId is a foreign key referencing the id from the Subject table.

Table: Subject
This table lists all subjects offered, each with a unique ID and name.

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
+-------------+---------+
id is the primary key for this table.

Develop a solution to identify the top achievers in each subject, defined as students with scores in the top three unique scores for that subject.

Example

Input:

Student table: +----+--------+-------+-----------+ | id | name | score | subjectId | +----+--------+-------+-----------+ | 1 | Alice | 92 | 1 | | 2 | Bob | 85 | 2 | | 3 | Carol | 87 | 2 | | 4 | Derek | 95 | 1 | | 5 | Elisa | 88 | 1 | | 6 | Fiona | 92 | 1 | | 7 | George | 90 | 1 | +----+--------+-------+-----------+ Subject table: +----+---------+ | id | name | +----+---------+ | 1 | Math | | 2 | Science | +----+---------+

Output:

+---------+---------+-------+ | Subject | Student | Score | +---------+---------+-------+ | Math | Derek | 95 | | Math | Alice | 92 | | Math | Fiona | 92 | | Math | George | 90 | | Science | Carol | 87 | | Science | Bob | 85 | +---------+---------+-------+

In this example, the top achievers in each subject are determined based on their scores, with a maximum of three students recognized per subject if there are at least three unique scores.

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To identify the top achievers in each subject, defined as students with scores in the top three unique scores for that subject, we can utilize SQL's window functions, specifically DENSE_RANK(). This approach allows us to rank students based on their scores within each subject and then filter out those who fall within the top three ranks.

SQL Query

WITH RankedScores AS ( SELECT st.name AS Student, su.name AS Subject, st.score, DENSE_RANK() OVER (PARTITION BY st.subjectId ORDER BY st.score DESC) AS s_rank FROM Student st JOIN Subject su ON st.subjectId = su.id ) SELECT Subject, Student, score AS Score FROM RankedScores WHERE s_rank <= 3 ORDER BY Subject, s_rank;

Step-by-Step Approach

Step 1: Join Student and Subject Tables and Rank Scores

Combine the Student and Subject tables to associate each student's score with the corresponding subject. Then, rank the students within each subject based on their scores in descending order.

SQL Query:

WITH RankedScores AS ( SELECT st.name AS Student, su.name AS Subject, st.score, DENSE_RANK() OVER (PARTITION BY st.subjectId ORDER BY st.score DESC) AS s_rank FROM Student st JOIN Subject su ON st.subjectId = su.id )

Explanation:

Output After Step 1:

Assuming the example input, the RankedScores CTE would produce:

+---------+---------+-------+--------+ | Student | Subject | score | s_rank | +---------+---------+-------+--------+ | Derek | Math | 95 | 1 | | Alice | Math | 92 | 2 | | Fiona | Math | 92 | 2 | | George | Math | 90 | 3 | | Elisa | Math | 88 | 4 | | Carol | Science | 87 | 1 | | Bob | Science | 85 | 2 | +---------+---------+-------+--------+

Step 2: Select Top Achievers Based on Rank

Retrieve the students who have ranks within the top three for their respective subjects, indicating they are among the top achievers.

SQL Query:

SELECT Subject, Student, score AS Score FROM RankedScores WHERE s_rank <= 3 ORDER BY Subject, s_rank;

Explanation:

Final Output:

Based on the example input, the final output would be:

+---------+---------+-------+ | Subject | Student | Score | +---------+---------+-------+ | Math | Derek | 95 | | Math | Alice | 92 | | Math | Fiona | 92 | | Math | George | 90 | | Science | Carol | 87 | | Science | Bob | 85 | +---------+---------+-------+
🤖 Don't fully get this? Learn it with Claude

Stuck on School Top Achievers by Subject? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **School Top Achievers by Subject** (Databases). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **School Top Achievers by Subject** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **School Top Achievers by Subject**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **School Top Achievers by Subject**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes