Compute the Rank as a Percentage - Problem
Compute Student Rank as Percentage
Imagine you're a university administrator tasked with creating a percentile ranking system for students within their departments. You need to rank each student based on their exam performance and convert that rank into a meaningful percentage score.
Given a
๐ Calculate each student's rank within their department (highest mark = rank 1)
๐ Convert the rank to a percentage using the formula:
๐ฏ Handle ties properly - students with identical marks get the same rank
๐ข Round to 2 decimal places for clean presentation
The percentage formula creates a scale where the top student gets 0% (representing the best performance) and the bottom student gets 100% (representing the worst performance relative to peers).
Table Structure:
Imagine you're a university administrator tasked with creating a percentile ranking system for students within their departments. You need to rank each student based on their exam performance and convert that rank into a meaningful percentage score.
Given a
Students table with student information including their department and exam marks, your goal is to:๐ Calculate each student's rank within their department (highest mark = rank 1)
๐ Convert the rank to a percentage using the formula:
(rank - 1) ร 100 / (total_students_in_dept - 1)๐ฏ Handle ties properly - students with identical marks get the same rank
๐ข Round to 2 decimal places for clean presentation
The percentage formula creates a scale where the top student gets 0% (representing the best performance) and the bottom student gets 100% (representing the worst performance relative to peers).
Table Structure:
Students: student_id (int), department_id (int), mark (int) Input & Output
example_1.sql โ Basic Case
$
Input:
Students table:
| student_id | department_id | mark |
|------------|---------------|------|
| 1 | 1 | 85 |
| 2 | 1 | 95 |
| 3 | 1 | 80 |
| 4 | 2 | 92 |
| 5 | 2 | 78 |
โบ
Output:
| student_id | department_id | mark | percentage |
|------------|---------------|------|------------|
| 1 | 1 | 85 | 50.00 |
| 2 | 1 | 95 | 0.00 |
| 3 | 1 | 80 | 100.00 |
| 4 | 2 | 92 | 0.00 |
| 5 | 2 | 78 | 100.00 |
๐ก Note:
In department 1: Student 2 (95) ranks 1st โ 0%, Student 1 (85) ranks 2nd โ 50%, Student 3 (80) ranks 3rd โ 100%. In department 2: Student 4 (92) ranks 1st โ 0%, Student 5 (78) ranks 2nd โ 100%.
example_2.sql โ Tied Scores
$
Input:
Students table:
| student_id | department_id | mark |
|------------|---------------|------|
| 1 | 1 | 85 |
| 2 | 1 | 95 |
| 3 | 1 | 85 |
| 4 | 1 | 90 |
โบ
Output:
| student_id | department_id | mark | percentage |
|------------|---------------|------|------------|
| 1 | 1 | 85 | 66.67 |
| 2 | 1 | 95 | 0.00 |
| 3 | 1 | 85 | 66.67 |
| 4 | 1 | 90 | 33.33 |
๐ก Note:
Students 1 and 3 both scored 85 and tie for rank 3. Using formula: rank 1 โ 0%, rank 2 โ 33.33%, tied rank 3 โ 66.67%. Total students = 4, so denominator is 3.
example_3.sql โ Single Student Department
$
Input:
Students table:
| student_id | department_id | mark |
|------------|---------------|------|
| 1 | 1 | 85 |
| 2 | 2 | 95 |
โบ
Output:
| student_id | department_id | mark | percentage |
|------------|---------------|------|------------|
| 1 | 1 | 85 | 0.00 |
| 2 | 2 | 95 | 0.00 |
๐ก Note:
When a department has only one student, the percentage is always 0.00 since there's no comparison possible (division by zero avoided with CASE statement).
Constraints
- 1 โค student_id โค 105
- 1 โค department_id โค 103
- 0 โค mark โค 100
- Each student_id is unique
- At least 1 student per department in test cases
Visualization
Tap to expand
Understanding the Visualization
1
Group by Department
Partition students into their respective departments
2
Rank by Performance
Sort students by marks in descending order within each department
3
Handle Ties
Students with identical marks receive the same rank
4
Calculate Percentile
Convert rank to percentage using (rank-1)*100/(total-1) formula
5
Special Cases
Single-student departments get 0% (no competition)
Key Takeaway
๐ฏ Key Insight: Window functions with PARTITION BY enable efficient per-department ranking in O(n log n) time, while the percentage formula creates a meaningful 0-100% scale where 0% represents the best performance.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code