bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 2346. Compute the Rank as a Percentage

2346. Compute the Rank as a Percentage

Leetcode Solutions

Calculating Student Rank Percentages Using Window Functions

  1. Use the RANK() window function to calculate the rank of each student within their department based on their marks in descending order.
  2. Use the COUNT() window function to count the total number of students in each department.
  3. Calculate the percentage using the formula (rank - 1) * 100 / (total_students - 1).
  4. Round the result to 2 decimal places using the ROUND() function.
  5. Use COALESCE() to handle cases where the denominator in the percentage calculation is zero (which happens when there is only one student in the department).
  6. Select the student_id, department_id, and the calculated percentage for the final output.

erDiagram
    Students {
        int student_id
        int department_id
        int mark
    }

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...