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 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
Student Ranking SystemComputer Science Dept1Alice: 95 โ†’ 0.00%(Top performer)2Bob: 85 โ†’ 50.00%(Middle performance)3Carol: 80 โ†’ 100.00%(Needs improvement)Mathematics Dept1David: 92 โ†’ 0.00%(Department leader)2Eve: 78 โ†’ 100.00%(Room for growth)Ranking Formula ExplainedPercentage = (Student_Rank - 1) ร— 100 / (Total_Students_in_Dept - 1)โ€ข Rank 1 (Best): (1-1) ร— 100 / (n-1) = 0%โ€ข Rank 2: (2-1) ร— 100 / (n-1) = 100/(n-1)%โ€ข Rank n (Worst): (n-1) ร— 100 / (n-1) = 100%โ€ข Special case: Single student โ†’ 0% (no competition)๐ŸŽฏ Lower percentage = Better performance!
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.
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
21.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen